I am developing a program that uses a neural network over images captured by webcam.
The webcam captures frames that I record in a different small videos that I afterwards analyce, I also save a full video of a recording session.
I sometimes reuse session videos as camera recordings, so VideoCapture is sometimes a webcam, and sometimes a video-path.
On this particular case I recorded session21.avi from my webcam, and then proccessed it again later which generated testsession.avi and other smaller videos, testsession.avi should contain every frame of session21.avi.
I accidentally deleted session21.avi, so I decided to run more tests over testsession.avi
at frame 98887 however (near the 1 hour mark) the frame_ok from webcam.read() goes False, and no new frame can be obtained from the video.
def camera(self, pipeargs):
webcam = cv2.VideoCapture(pipeargs.source)
pipe = pipeargs.pipe
fname = pipeargs.dirpath + pipeargs.fullvideo
while os.path.exists(f'{fname}.avi'):
fname = fname + 'x'
file = fname + '.avi'
video = VideoWriter(file, VideoWriter_fourcc(*'MP42'), 25.0, (640, 480))
try:
while True:
stream_ok, frame = webcam.read()
if stream_ok:
cv2.imshow('Webcam', frame)
video.write(frame)
pipe.insert(frame)
if cv2.waitKey(1) & 0xFF == 27: break
time.sleep(0.01)
except Exception as e:
print("Exception camera", e)
logging.critical("camera EXCEPTION", e)
cv2.destroyAllWindows()
webcam.release()
video.release()
I tried to set up the frame forward with webcam.set(cv2.CAP_PROP_POS_FRAMES, 120000), but it just produces the frame at 98887 before starting to give False on webcam.read().
I guess the video is corrupt somehow, but Windows media player seems to have no dificulty on displaying the video before, while and after the problem. There should be a way to achieve the same with cv2
I don’t really need to recover the video, what I would want is for my code to be able to handle this problem if it happens again.