Ask Your Question
0

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

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

hernancrespo gravatar image

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

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 -0600 )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 -0600 )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 -0600 )edit
1

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

hernancrespo gravatar imagehernancrespo ( 2020-04-05 10:13:08 -0600 )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 -0600 )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 -0600 )edit

So last frame is finish, then throw error msg?

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

2 answers

Sort by » oldest newest most voted
-1

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

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
-1

answered 2020-07-27 02:03:06 -0600

The error is self-explanatory. You are trying to subscript an object which you think is a list or dict, but actually is None. This means that you tried to do:

None[something]

This error means that you attempted to index an object that doesn't have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. 'NoneType' object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn't define the __getitem__ method . This is a design principle for all mutable data structures in Python.

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 3,995 times

Last updated: Apr 06 '20