Ask Your Question
-1

AttributeError: 'NoneType' object has no attribute 'shape'

asked 2019-02-26 05:37:44 -0600

Abdu gravatar image

Hi all! I need your help regarding the following code. My goal is to read and show the video with a resolution modification.


import cv2

cap = cv2.VideoCapture("C:/Users/user/Desktop/Foot_Detection/ball_tracking_example.mp4")

def rescale_frame(frame, percent=30): width = int(frame.shape[1] * percent/ 100) height = int(frame.shape[0] * percent/ 100) dim = (width, height) return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)

if (cap.isOpened() == False): print("Error opening video stream or file")

while (cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read()

frame = rescale_frame(frame, percent=30) if ret == True:

cv2.imshow('Frame', frame)

if cv2.waitKey(25) & 0xFF == ord('q'): break

else: break

cap.release()

cv2.destroyAllWindows()

After executing the above code, the video displayed on my screen till the end. However, I have got the following error:


Traceback (most recent call last):
  File "C:/Users/user/Desktop/Foot_Detection/FootDetection.py", line 24, in <module>
    frame = rescale_frame(frame, percent=30)
  File "C:/Users/user/Desktop/Foot_Detection/FootDetection.py", line 10, in rescale_frame
    width = int(frame.shape[1] * percent/ 100)
AttributeError: 'NoneType' object has no attribute 'shape'
 

I tried to install the codec for mp4 and to the video path is correct. Could you please assist me in this matter? Thank you in advance.

edit retag flag offensive close merge delete

Comments

checking the ret value is mandatory, since it will tell you, when the movie's over.

berak gravatar imageberak ( 2019-02-26 07:38:08 -0600 )edit

Nope, berak. What does the error tell you? AttributeError: 'NoneType' object has no attribute 'shape'

supra56 gravatar imagesupra56 ( 2019-02-26 08:08:16 -0600 )edit

again, the last frame will be empty (None)

(and this is one of the main differences between capturing from a webcam or a video file, please go and check)

berak gravatar imageberak ( 2019-02-26 08:11:57 -0600 )edit

Nope. The frame is not empty. I already tested it.. There are no differences between webcam and file.

supra56 gravatar imagesupra56 ( 2019-02-26 08:42:02 -0600 )edit

@Abdu, can you take another look at the code in your question, and try to repair the broken formatting ?

(whitespace matters in python ...)

berak gravatar imageberak ( 2019-02-26 13:39:17 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-02-27 04:20:33 -0600

Abdu gravatar image

I was able to solve the problem. I just need to figure out how to loop:


import numpy as np
import cv2
cap = cv2.VideoCapture('C:\Sample3.mp4')

def rescale_frame(frame, percent=30):

    width = int(frame.shape[1] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)

while True:
    ret ,frame = cap.read()
    if type(frame) == type(None):
        break
    frame25 = rescale_frame(frame, percent=25)
    cv2.imshow('frame25',frame25)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
 
edit flag offensive delete link more

Comments

1

what's about

if not ret:
    break

instead of

if type(frame) == type(None):
    break

and you don't check if video file is opened :

if not cap.isOpened():
    print("Cannot open video file")
    exit()
LBerger gravatar imageLBerger ( 2019-02-27 04:38:28 -0600 )edit

@Abdu . Following example freom @LBerger.

supra56 gravatar imagesupra56 ( 2019-02-28 06:59:21 -0600 )edit
0

answered 2019-02-26 07:35:50 -0600

supra56 gravatar image

updated 2019-02-26 08:48:19 -0600

I am using OpenCV 4.0.1, using raspberry pi 3. Do not put outside of while condition block. And do not used this else: break. Because it will disappeared window. The correct code like this:

import cv2

cap = cv2.VideoCapture("C:/Users/user/Desktop/Foot_Detection/ball_tracking_example.mp4")
def rescale_frame(frame, percent=30):
    width = int(frame.shape[1] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
if (cap.isOpened() == False):
    print("Error opening video stream or file")
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame = rescale_frame(frame, percent=30)
    if ret == True:
        cv2.imshow('Frame', frame)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break


cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Good tutorial to learn. tutorial

supra56 gravatar imagesupra56 ( 2019-02-26 07:39:04 -0600 )edit

please check ret* **before** callingrescale_frame()`

(else you run into the same problem as the original question (and nothing is resolved))

berak gravatar imageberak ( 2019-02-26 08:13:22 -0600 )edit

NOPE! Everything outside of while condition block.

supra56 gravatar imagesupra56 ( 2019-02-26 08:47:09 -0600 )edit

@supra56 your link is not up to date. Good link it's here You must check ret valeur in cap.read()

LBerger gravatar imageLBerger ( 2019-02-26 09:43:29 -0600 )edit

Thank you for your answers. @supra56 your code shows me the same error.

Abdu gravatar imageAbdu ( 2019-02-26 11:01:07 -0600 )edit

@Abdu. Are you running on python 2 or python 3?

supra56 gravatar imagesupra56 ( 2019-02-26 12:54:12 -0600 )edit

@supra56 Python 3.7

Abdu gravatar imageAbdu ( 2019-02-27 03:31:56 -0600 )edit
1

Did you try link from @LBerger? Wondering, what's your opencv version?

supra56 gravatar imagesupra56 ( 2019-02-27 03:45:01 -0600 )edit
1

Yes, I tried the code in the link of @LBerger, the error persists.

Abdu gravatar imageAbdu ( 2019-02-27 03:55:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-26 05:37:44 -0600

Seen: 19,658 times

Last updated: Feb 26 '19