300
300

Reputation: 323

Problem regarding reading a video using opencv

In this code it is showing whole video as a frame but in the end it is also returning None for the last frame ?

cap = cv2.VideoCapture("demo.mp4")
while(cap.isOpened()):
  status, frame = cap.read()
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  cv2_imshow(gray)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()



error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

This is showing as an error.

Upvotes: 0

Views: 72

Answers (1)

ashwinjoseph
ashwinjoseph

Reputation: 369

This can be solved by modifying the code slightly as follows by ensuring the frame is not empty:

cap = cv2.VideoCapture("demo.mp4")
while(cap.isOpened()):
  status, frame = cap.read()
  if status:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2_imshow(gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
  else:
    break

cap.release()
cv2.destroyAllWindows()

Upvotes: 1

Related Questions