Python Forum
AttributeError: 'NoneType' object has no attribute 'all'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'NoneType' object has no attribute 'all'
#1
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?
Reply
#2
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.
Reply
#3
How do I fix it so that it read the frame correctly?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "list object has no attribute transpose()" usercat123 4 4,187 Jan-28-2022, 12:01 PM
Last Post: usercat123
  NoneType' object is not subscriptable shane1236 9 7,126 Aug-06-2020, 05:16 AM
Last Post: irvinborder
  AttributeError: (“module 'pandas' has no attribute 'rolling_std'” Mariana136 4 7,558 Sep-23-2019, 12:56 PM
Last Post: Mariana136
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 45,278 May-29-2019, 02:48 AM
Last Post: heiner55
  Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv' NSearch 9 16,763 Apr-22-2019, 05:05 PM
Last Post: Yoriz
  Please help with AttributeError: 'Netz' object has no attribute 'conv' DerBerliner 2 3,723 Feb-27-2019, 06:01 PM
Last Post: DerBerliner
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 15,485 Nov-04-2018, 10:38 PM
Last Post: stullis
  AttributeError: Can't get attribute 'Individual' on <module 'deap.creator' DomClout 4 8,725 Jul-27-2018, 09:05 PM
Last Post: Vysero
  AttributeError: module 'mnist' has no attribute 'train_images' pythonbeginner 1 8,144 Jun-14-2018, 09:29 PM
Last Post: snippsat
  AttributeError: 'set' object has no attribute 'items hey_arnold 3 26,537 Apr-29-2018, 04:33 PM
Last Post: hey_arnold

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020