Bottom Page

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?
Quote
#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.
Quote
#3
How do I fix it so that it read the frame correctly?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 1,408 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 1,358 Apr-22-2019, 05:05 PM
Last Post: Yoriz
  Please help with AttributeError: 'Netz' object has no attribute 'conv' DerBerliner 2 388 Feb-27-2019, 06:01 PM
Last Post: DerBerliner
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 2,629 Nov-04-2018, 10:38 PM
Last Post: stullis
  AttributeError: Can't get attribute 'Individual' on <module 'deap.creator' DomClout 4 1,625 Jul-27-2018, 09:05 PM
Last Post: Vysero
  AttributeError: module 'mnist' has no attribute 'train_images' pythonbeginner 1 1,496 Jun-14-2018, 09:29 PM
Last Post: snippsat
  AttributeError: 'set' object has no attribute 'items hey_arnold 3 7,494 Apr-29-2018, 04:33 PM
Last Post: hey_arnold
  AttributeError: module 'plotly' has no attribute 'offline' charlesczc 8 5,138 Jan-21-2018, 08:34 AM
Last Post: buran
  Set attribute to a value CSR Samsar 1 894 Dec-16-2017, 03:19 PM
Last Post: stranac
  np.arrange error - no attribute pksherard 3 6,981 Aug-16-2017, 09:54 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)