Home > database >  Why am I getting AttributeError: 'list' object has no attribute 'size'?
Why am I getting AttributeError: 'list' object has no attribute 'size'?

Time:11-08

I am currently trying to build a python program to train a LSTM neural network to create a pain detection system using artificial intelligence. The data to be input is the extracted x, y, z coordinates of each facial keypoints from different images. There is a total of 3 different pain intensity level folders with 25 subfolders in each of them containing 18 numpy files each of different frames. At first, I received ValueError: zero-size array to reduction operation maximum which has no identity. Then, I changed the line sequences, labels = [], [] and received the following error: TypeError: 'list' object cannot be interpreted as an integer. Then i changed 2 lines of code from for sequence in np.array(os.listdir(os.path.join(DATA_PATH,action))).astype(int): to for sequence in np.array(os.listdir(os.path.join(DATA_PATH, action))).astype(str): and from actions = np.array(['verypain']) to actions = np.array2string(['verypain']). However I have ran into AttributeError: 'list' object has no attribute 'size'. The numpy files contain alphabets, numbers and even special characters like slashes, so could it be that I did not make the correct edit to my codes?. What needs to be changed in my code as such that it would be able to preprocess data and create labels and features before training the LSTM? I have stated my current code below:

import cv2
import numpy as np
import os
from matplotlib import pyplot as plt
import time
import mediapipe as mp
import tensorflow as tf

# Path for exported data, numpy arrays
DATA_PATH = os.path.join('C:/Users/PycharmProjects/MP_Data')

# Actions that we try to detect
actions = np.array2string(['verypain'])

# Thirty videos worth of data
no_sequences = 25

# Videos are going to be 30 frames in length
sequence_length = 18

from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical

label_map = {label:num for num, label in enumerate(actions)}
print(label_map)

sequences, labels = np.max([], [])
for action in actions:
    for sequence in np.array(os.listdir(os.path.join(DATA_PATH, action))).astype(str):
        window = []
        for frame_num in range(sequence_length):
            if os.path.exists(os.path.join(DATA_PATH, action, str(sequence), "out_{}.npy".format(frame_num))):
                res = np.load(os.path.join(DATA_PATH, action, str(sequence), "out_{}.npy".format(frame_num)))
            else:
                pass
            #res = np.load((DATA_PATH "/"  action  "/"  str(sequence) "/"  "out_{}.npy".format(frame_num 1)))
            #window.append(res)
        #sequences.append(window)
        #labels.append(label_map[action])

print(np.array(sequences).shape)
print(np.array(labels).shape)

y = to_categorical(labels).astype(int)
print(labels)

Currently, I am taking reference from a youtube video and previously done code. I am trying to modify this code as such that it would be able to give the output that I have stated above. I have provided the links to both the video and code below if required context:

https://github.com/nicknochnack/ActionDetectionforSignLanguage/blob/main/Action Detection Refined.ipynb

https://youtu.be/doDUihpj6ro

Please help me out because this code seems complex to understand!

CodePudding user response:

You are using the numpy.array2string() method, which expects a numpay.array as input. You are giving it a built-in list, this causes an error inside the numpy code.

Replacing

actions = np.array2string(['verypain'])

with

actions = np.array2string(np.array(['verypain']))

should fix the error.

Or since you are assigning a string to actions anyways

actions = '[verypain]'

should also work.

CodePudding user response:

Your code has many flaws, most of them seem to arise because of missing basics of python/programming in general. I highly advice to watch some tutorials on programming basics before you dive in and try to change someone elses code.

First off:

actions = np.array2string(['verypain'])

will lead to a bunch of problems, since you are assigning a string to actions while the rest of the code needs it to be some kind of array.

sequences, labels = np.max([], [])

cannot work for multiple reasons:

  1. numpy.max() computes the maximum number inside a (multidimensional) numpy.array() and therefor expects a numpy.array and an int index of the dimension. You are giving it 2 built-in lists.
  2. You are trying to assign two values sequences and labels, while numpy.max() only returns one value.
  • Related