Im Erkennungssystem test.py funktioniert nichtPython

Python-Programme
Anonymous
 Im Erkennungssystem test.py funktioniert nicht

Post by Anonymous »

main.py < /p>

Code: Select all

import numpy as np
import cv2
import dlib
import pickle
import os

# ✅ Open video capture
video = cv2.VideoCapture(0)

# ✅ Initialize Dlib's HOG-based face detector
detector = dlib.get_frontal_face_detector()

faces_data = []
i = 0
name = input("Enter Your Name Here: ")

# ✅ Set higher resolution
video.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

while True:
ret, frame = video.read()
if not ret:
break

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)

for face in faces:
x, y, w, h = face.left(), face.top(), face.width(), face.height()
crop_img = frame[y:y + h, x:x + w, :]
resized_img = cv2.resize(crop_img, (50, 50))

if len(faces_data) < 100 and i % 10 == 0:
faces_data.append(resized_img)
i += 1

cv2.putText(frame, str(len(faces_data)), (50, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (50, 50, 255), 1)
cv2.rectangle(frame, (x, y), (x + w, y + h), (50, 50, 255), 2)

cv2.imshow("Frame - Dlib HOG", frame)

k = cv2.waitKey(1)
if k == ord('q') or len(faces_data) == 100:  # ✅ Stop when 100 images are captured
break

# ✅ Release video
video.release()
cv2.destroyAllWindows()

# ✅ Save Data in "Data" Folder
if not os.path.exists('Data'):
os.makedirs('Data')

names_file = 'Data/names.pkl'
faces_file = 'Data/faces_data.pkl'

# ✅ Save Names
if os.path.exists(names_file):
with open(names_file, 'rb') as f:
names = pickle.load(f)
names.extend([name] * len(faces_data))

else:
names = [name] * len(faces_data)

with open(names_file, 'wb') as f:
pickle.dump(names, f)

# ✅ Save Faces
faces_data = np.asarray(faces_data)

if os.path.exists(faces_file):
with open(faces_file, 'rb') as f:
existing_faces = pickle.load(f)
combined_faces = np.concatenate((existing_faces, faces_data), axis=0)
else:
combined_faces = faces_data

with open(faces_file, 'wb') as f:
pickle.dump(combined_faces, f)

print("✅ Data saved successfully in 'Data/' folder.")
< /code>
Test.py
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
import cv2
import dlib
import pickle
import os

names_file = 'Data/names.pkl'
faces_file = 'Data/faces_data.pkl'

# ✅ Open video capture
video = cv2.VideoCapture(0)

# ✅ Initialize Dlib's HOG-based face detector
detector = dlib.get_frontal_face_detector()

# Load data
with open(names_file, 'rb') as f:
LABELS = pickle.load(f)
with open(faces_file, 'rb') as f:
FACES = pickle.load(f)

print(f"Shape of FACES: {FACES.shape}")
print(f"Shape of LABELS: {len(LABELS)}")

# ✅ Set higher resolution
video.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

# Train KNN model
Knn = KNeighborsClassifier(n_neighbors=5)
Knn.fit(FACES, LABELS)

while True:
ret, frame = video.read()
if not ret:
break

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)

for face in faces:
x, y, w, h = face.left(), face.top(), face.width(), face.height()
crop_img = frame[y:y + h, x:x + w]
resized_img = cv2.resize(crop_img, (50, 50)).flatten().reshape(1, -1)
output = Knn.predict(resized_img)
cv2.putText(frame, str(output[0]), (x, y - 15), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (50, 50, 255), 2)

cv2.imshow("Frame - Dlib HOG", frame)

k = cv2.waitKey(1)
if k == ord('q'):
break

# ✅ Release video
video.release()
cv2.destroyAllWindows()
< /code>
Das [url=viewtopic.php?t=11587]Problem[/url] ist, dass es beim Ausführen der Eingabe hier eingibt. Name Name: Und dann wird die Kamera geöffnet und das Gesicht der Person gelesen und auf der PKL -Datei [b] (faces_data. PKL und names.pkl) [/b] wie angegeben. Der test.py "[b] runTimeError < /strong>: nicht unterstütztes Bildtyp, muss 8 -Bit -Grau- oder RGB -Bild sein.  

Vollständiger Fehler: [/b]

Code: Select all

Shape of FACES: (100, 7500)
Shape of LABELS: 102
Traceback (most recent call last):
File "E:\UMT\Semester 5\DL & NN\Project\Youtube_Face_reconition system\test.py", line 32, in 
Knn.fit(FACES, LABELS)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\base.py", line 1389, in wrapper
return fit_method(estimator, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\neighbors\_classification.py", line 239, in fit
return self._fit(X, y)
^^^^^^^^^^^^^^^
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\neighbors\_base.py", line 478, in _fit
X, y = validate_data(
^^^^^^^^^^^^^^
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\utils\validation.py", line 2961, in validate_data
X, y = check_X_y(X, y, **check_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\utils\validation.py", line 1389, in check_X_y
check_consistent_length(X, y)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\utils\validation.py", line 475, in check_consistent_length
raise ValueError(
ValueError: Found input variables with inconsistent numbers of samples: [100, 102]
Stattdessen sollte es mir den Namen der Person mitteilen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post