'NoneType' object is not subscriptable error after read some fames
I've been trying to read a video (format of videos is mp4) and do some operations in every 30th frame. Firstly, It works OK. But after some frames, I see this error:
File "C:\Users\mustafa\Desktop\vidoe_deneme\opencv_object_tracker.py", line 22, in <module>
frame = frame[:, :, ::-1]
TypeError: 'NoneType' object is not subscriptable
I see this error on every video I've tried (Not only on a one video). What is the problem?
import cv2
from imutils.video import VideoStream
from imutils.video import FPS
cap = cv2.VideoCapture('k.mp4')
totalFrames = 0
# start the frames per second throughput estimator
fps = FPS().start()
print('before video')
# loop over frames from the video stream
while cap.isOpened():
ret,frame = cap.read()
frame = frame[:, :, ::-1]
h, w = frame.shape[:2]
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if totalFrames % 30 == 0:
cv2.imshow("Frame", rgb)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
totalFrames += 1
fps.update()
# stop the timer and display FPS information
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cv2.destroyAllWindows()
When k.mp4 is finished, did you get same error?
Reading a frame can obviously fail sometimes, but your code does not check for it - and it should
@mvuori NO! Can you see screenshot? At the end of video, you will see debug.
@supra56 It doesn't finish. Error throws out before the end frame
Can you do resizer?
I've tried. I see same error on the last frame of the video.
So last frame is finish, then throw error msg?