Ich habe den folgenden Code geschrieben, aber irgendwie bekomme ich die ganze Zeit eine Error Message ausgespuckt und ich habe keine Ahnung wie ich ihn fixen kann.
Code: Alles auswählen
import pickle
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
data_dict = pickle.load(open('./data.pickle', 'rb'))
data = np.asarray(data_dict['data'])
labels = np.asarray(data_dict['labels'])
model = RandomForestClassifier()
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
score = accuracy_score(y_predict, y_test)
print('{}% of samples were classified correctly !'.format(score * 100))
f = open('model.p', 'wb')
pickle.dump({'model': model}, f)
f.close()
Code: Alles auswählen
/home/PycharmProjects/SignLanguage/train_classifier.py:13: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
data = np.asarray(data_dict['data'])
TypeError: float() argument must be a string or a real number, not 'list'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/PycharmProjects/SignLanguage/train_classifier.py", line 21, in <module>
model.fit(x_train, y_train)
File "/home/PycharmProjects/SignLanguage/venv/lib/python3.10/site-packages/sklearn/base.py", line 1151, in wrapper
return fit_method(estimator, *args, **kwargs)
File "/home/PycharmProjects/SignLanguage/venv/lib/python3.10/site-packages/sklearn/ensemble/_forest.py", line 348, in fit
X, y = self._validate_data(
File "/home/PycharmProjects/SignLanguage/venv/lib/python3.10/site-packages/sklearn/base.py", line 621, in _validate_data
X, y = check_X_y(X, y, **check_params)
File "/home/PycharmProjects/SignLanguage/venv/lib/python3.10/site-packages/sklearn/utils/validation.py", line 1147, in check_X_y
X = check_array(
File "/home/PycharmProjects/SignLanguage/venv/lib/python3.10/site-packages/sklearn/utils/validation.py", line 917, in check_array
array = _asarray_with_order(array, order=order, dtype=dtype, xp=xp)
File "/home/PycharmProjects/SignLanguage/venv/lib/python3.10/site-packages/sklearn/utils/_array_api.py", line 380, in _asarray_with_order
array = numpy.asarray(array, order=order, dtype=dtype)
ValueError: setting an array element with a sequence.
Process finished with exit code 1
Code: Alles auswählen
data = np.asarray([np.asarray(d) for d in data_dict['data']]) # doesn't work
Code: Alles auswählen
data = np.asarray(data_dict['data'], dtype=float)
Code: Alles auswählen
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2912,) + inhomogeneous part.
Code: Alles auswählen
import mediapipe as mp
import cv2
import os
import pickle
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
DATA_DIR = "./data"
data = []
labels = []
for dir_ in os.listdir(DATA_DIR):
for img_path in os.listdir(os.path.join(DATA_DIR, dir_)):
data_aux = []
img = cv2.imread(os.path.join(DATA_DIR, dir_, img_path))
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(img_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x)
data_aux.append(y)
data.append(data_aux)
labels.append(dir_)
f = open('data.pickle', 'wb')
pickle.dump({'data': data, 'labels': labels}, f)
f.close()
Code: Alles auswählen
np.array(data) bevor pickle.dump()
Code: Alles auswählen
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.array(data)
Code: Alles auswählen
print([np.asarray(d).shape for d in data_dict['data']]
Code: Alles auswählen
[(42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,), (42,)]
Traceback (most recent call last):
File "/home/PycharmProjects/SignLanguage/train_classifier.py", line 16, in <module>
data = np.asarray(data_dict['data'], dtype=float)
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2912,) + inhomogeneous part.
Vielen Dank für jede Idee und Hilfe.