Ask Your Question
0

'NoneType' object is not subscriptable error after read some fames

asked 2020-04-05 05:47:24 -0500

hernancrespo gravatar image

updated 2020-04-05 05:52:25 -0500

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()

Error

edit retag flag offensive close merge delete

Comments

When k.mp4 is finished, did you get same error?

supra56 gravatar imagesupra56 ( 2020-04-05 08:01:01 -0500 )edit

Reading a frame can obviously fail sometimes, but your code does not check for it - and it should

mvuori gravatar imagemvuori ( 2020-04-05 08:08:14 -0500 )edit

@mvuori NO! Can you see screenshot? At the end of video, you will see debug.

supra56 gravatar imagesupra56 ( 2020-04-05 08:15:59 -0500 )edit
1

@supra56 It doesn't finish. Error throws out before the end frame

hernancrespo gravatar imagehernancrespo ( 2020-04-05 10:13:08 -0500 )edit

Can you do resizer?

small_frame = imutils.resize(frame, width=640)
frame = small_frame[:, :, ::-1]
supra56 gravatar imagesupra56 ( 2020-04-05 12:44:35 -0500 )edit
1

I've tried. I see same error on the last frame of the video.

hernancrespo gravatar imagehernancrespo ( 2020-04-05 15:56:38 -0500 )edit

So last frame is finish, then throw error msg?

supra56 gravatar imagesupra56 ( 2020-04-06 06:56:29 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2020-04-06 02:48:08 -0500

berak gravatar image

using opencv's VideoCapture class with video files, you will receive empty / invalid frames if it reaches the end of th e movie. (that's expected !)

you HAVE TO check the ret value when reading frames, and either bail out of the loop, or reset the frame position, to start again

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-04-05 05:47:24 -0500

Seen: 110 times

Last updated: Apr 06