The code should lead to capturing video from the camera (copied from opencv docs), however, an error occurs.

Code:

import cv2 import numpy as np cap = cv2.VideoCapture() while(True): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame', gray) if cv2.waitKey(1) % 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() 

Mistake:

 File "main.py", line 8, in <module> gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 

    1 answer 1

    You did not specify the camera number - the first (or one) camera is number 0 .

    And so instead

     cap = cv2.VideoCapture() 

    write

     cap = cv2.VideoCapture(0) 

    Note:

    You don't need brackets in the while command in Python, you can just write while True:

    Instead

     cv2.waitKey(1) % 0xFF 

    need to use

     cv2.waitKey(1) % 0x100 

    or - better - much faster bitwise & operation:

     cv2.waitKey(1) & 0xFF 

    since the result is not the same.