The problem is in this line:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
This line expects frame to be a 3 channel or 4 channel Mat object but instead it got some empty Mat and that is why you are getting this assertion failed. You need to check if the frame exists in video and need to handle end of video properly.
cap.isOpened() will just check if the video file can be opened for reading but it will not return a false when end of video file is reached.
Try this
Answer from abggcv on Stack OverflowThe problem is in this line:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
This line expects frame to be a 3 channel or 4 channel Mat object but instead it got some empty Mat and that is why you are getting this assertion failed. You need to check if the frame exists in video and need to handle end of video properly.
cap.isOpened() will just check if the video file can be opened for reading but it will not return a false when end of video file is reached.
Try this
When you put cap.isOpened() it check that, the video is read properly, So the while loop is not working there.
But when you changed to while True it will execute without proper reading that's why it's giving an error.
Make sure than you are properly reading the video file.