Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

I've been trying to read a video 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()

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

Error