Spaces:
Sleeping
Sleeping
Create trainImage.py
Browse files- trainImage.py +29 -0
trainImage.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def TrainImages():
|
| 7 |
+
print("Training images...")
|
| 8 |
+
|
| 9 |
+
# Define the paths
|
| 10 |
+
trainimage_path = "TrainingImage"
|
| 11 |
+
image_paths = [os.path.join(trainimage_path, f) for f in os.listdir(trainimage_path) if f.endswith(".jpg")]
|
| 12 |
+
|
| 13 |
+
faces = []
|
| 14 |
+
labels = []
|
| 15 |
+
|
| 16 |
+
for image_path in image_paths:
|
| 17 |
+
img = Image.open(image_path).convert("L") # Convert to grayscale
|
| 18 |
+
img_np = np.array(img)
|
| 19 |
+
enrollment = int(image_path.split('.')[1])
|
| 20 |
+
faces.append(img_np)
|
| 21 |
+
labels.append(enrollment)
|
| 22 |
+
|
| 23 |
+
# Train the model
|
| 24 |
+
recognizer = cv2.face.LBPHFaceRecognizer_create()
|
| 25 |
+
recognizer.train(faces, np.array(labels))
|
| 26 |
+
|
| 27 |
+
# Save the model
|
| 28 |
+
recognizer.save("trainer.yml")
|
| 29 |
+
print("Training complete.")
|