Ask Your Question

'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\", 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 import VideoStream
from 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 =

    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"):
    totalFrames += 1
# stop the timer and display FPS information
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))


edit retag flag offensive close merge delete


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

@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

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

2 answers

Sort by » oldest newest most voted

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

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

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:


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


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

Seen: 707 times

Last updated: Apr 06 '20