Open KanTakahiro opened 3 years ago
yeah it works, though I add a return instead of continue. Helpful. Thanks
This fix worked for me as well. Another approach, if someone doesn't want to modify imutils
as installed is to introduce an if
statement in your while loop which skips if the current frame is None
. Using the tutorial as an example, you could modify like so:
# loop over frames from the video file stream
while fvs.more():
# grab the frame from the threaded video file stream, resize
# it, and convert it to grayscale (while still retaining 3
# channels)
frame = fvs.read()
if frame is not None:
frame = imutils.resize(frame, width=450)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = np.dstack([frame, frame, frame])
# display the size of the queue on the frame
cv2.putText(frame, "Queue Size: {}".format(fvs.Q.qsize()),
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# show the frame and update the FPS counter
cv2.imshow("Frame", frame)
cv2.waitKey(1)
fps.update()
I read this article and write a code like this (almost the same code as the article showed):
I run this code with
python3 test.py -v video.mp4
and the script does playback the all video but finally an error was thrown out:It seems that a NoneType was fed to the function
imutils.resize()
instead of a available frame. Since all the frames are fetched from the queue in FileVideoStream, I found the reason that causes this error in the implementation of FileVideoStream. In functionupdate()
ofimutils/video/filevideostream.py
, ifcv2.VideoCapture().read()
faild(that basically means it reached the end of video file), althoughself.stopped
is set to be True,self.Q.put(frame)
in the below is still executed and put a NoneType into the queue. The process won't stop until the next loop start, but in this case it should be stopped immediately, at least before theself.Q.put(frame)
. The originalfilevideostream.py
, with all comments and empty lines deleted:I added a
continue
underself.stopped = True
to avoidself.Q.put(frame)
from executing and the error didn't appear any more. I don't know whether it is a bug of FileVideoStream. Maybe this solution will cause another issue. But if someone using FileVideoStream meets the same error then you can try this solution.