Python Forum
AttributeError: 'NoneType' object has no attribute 'all' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: AttributeError: 'NoneType' object has no attribute 'all' (/thread-16601.html)



AttributeError: 'NoneType' object has no attribute 'all' - synthex - Mar-06-2019

I want convert video files to binary code and load result into csv
I use Ubuntu
mport csv
import numpy as np
import cv2
import os
 
def video_to_csv(name, num_frames):
    cap = cv2.VideoCapture(name)
    i = 0
    NUMFRAMES = num_frames
    name = os.path.splitext(name)[0]
    while(True):
        if i >= NUMFRAMES:
            break
        ret, frame = cap.read()
        if frame.all():
            break
        with open(name + str(i) + '.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile, delimiter=',')
            writer.writerows(frame.tolist())
        i += 1
 
 
def video_from_csv(name, num_frames):
    i = 0
    NUMFRAMES = num_frames
    name = os.path.splitext(name)[0]
    while i < NUMFRAMES:
        with open(name + str(i) + '.csv', 'r') as f:
          reader = csv.reader(f)
          frame_data = list(reader)
 
        frame_list = []
        for row in frame_data:
            nrow = []
            for r in row:
                nrow.append(eval(r))
            frame_list.append(nrow)
        img = np.array(frame_list)
        cv2.imwrite(name + str(i) + '.jpg', img)
        i += 1
 
 
 
for root, dirs, files in os.walk("."):
    for file in files:
        if file.endswith(".mp4"):
            video_to_csv(file, 2)
in Ubuntu terminal
python Downloads/video/f.py
Error:
and i get the error Traceback (most recent call last): File "Downloads/video/f.py", line 47, in <module> video_to_csv(file, 20) File "Downloads/video/f.py", line 15, in video_to_csv if frame.all():
AttributeError: 'NoneType' object has no attribute 'all'

i have some videos in folder, but to reproduce example here one of them
video Tom&jerry.mp4

How to fix my code?


RE: AttributeError: 'NoneType' object has no attribute 'all' - Yoriz - Mar-06-2019

Looking at the documentation cap.read() returns a Boolean to show if a frame has been read correctly and a frame.
perhaps you should check ret first to see if a frame has been read correctly.
I'm guessing it will return None if it has not read a frame correctly or there is no frame.


RE: AttributeError: 'NoneType' object has no attribute 'all' - synthex - Mar-07-2019

How do I fix it so that it read the frame correctly?