Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

/__pycache__
56 changes: 28 additions & 28 deletions face_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
import sys

def face_detect(imagePath):
# HaarCascade file, to detect the face.
faceCascade = cv2.CascadeClassifier("opencv-files/haarcascade_frontalface_alt.xml")
image = cv2.imread(imagePath)
#Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
grays = []
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
# For drawing rectangles over multiple faces in the image
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Show Detected faces.
cv2.imshow("Faces found", image)
cv2.waitKey(1)
# Append the detected faces into grays list.
for i in range(0, len(faces)):
(x, y, w, h) = faces[i]
grays.append(gray[y:y+w, x:x+h])
print "------------------------------------------------------------"
print "Detecting Face -\-"
return grays, faces, len(faces)
# HaarCascade file, to detect the face.
faceCascade = cv2.CascadeClassifier("opencv-files/haarcascade_frontalface_alt.xml")
image = cv2.imread(imagePath)
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
grays = []
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
# For drawing rectangles over multiple faces in the image
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Show Detected faces.
cv2.imshow("Faces found", image)
cv2.waitKey(1)
# Append the detected faces into grays list.
for i in range(0, len(faces)):
(x, y, w, h) = faces[i]
grays.append(gray[y:y+w, x:x+h])
print("------------------------------------------------------------")
print("Detecting Face -\\-")
return grays, faces, len(faces)
23 changes: 11 additions & 12 deletions facemaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@

label = []
def predict(test_img):
img = cv2.imread(test_img).copy()
print "\n\n\n"
print "Face Prediction Running -\-"
face, rect, length = face_detect.face_detect(test_img)
print len(face), "faces detected."
for i in range(0, len(face)):
labeltemp, confidence = face_recognizer.predict(face[i])
label.append(labeltemp)
return img, label
img = cv2.imread(test_img).copy()
print("\n\n\n")
print("Face Prediction Running -\\-")
face, rect, length = face_detect.face_detect(test_img)
print(f"{len(face)} faces detected.")
for i in range(0, len(face)):
labeltemp, confidence = face_recognizer.predict(face[i])
label.append(labeltemp)
return img, label

faces, labels = training_data.training_data("training-data")
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces, np.array(labels))


# Read the test image.
test_img = "test-data/test.jpg"
predicted_img , label= predict(test_img)
predicted_img, label = predict(test_img)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()
print "Recognized faces = ", label
print("Recognized faces = ", label)
3 changes: 3 additions & 0 deletions requirement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy==2.1.2
opencv-contrib-python==4.10.0.84
opencv-python==4.10.0.84
14 changes: 8 additions & 6 deletions training_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import cv2, os, sys
import cv2
import os
import sys
import numpy as np
import face_detect as face_detect

Expand All @@ -8,17 +10,17 @@ def training_data(data_folder):
labels = []
for dir_name in dirs:
if not dir_name.startswith("s"):
continue;
continue
label = int(dir_name.replace("s", ""))
subject_dir = data_folder + "/" + dir_name
subject_dir = os.path.join(data_folder, dir_name)
subject_images_names = os.listdir(subject_dir)
for image_name in subject_images_names:
if image_name.startswith("."):
continue;
image_path = subject_dir + "/" + image_name
continue
image_path = os.path.join(subject_dir, image_name)
face, rect, length = face_detect.face_detect(image_path)
if face is not None:
faces.append(face[0])
labels.append(label)

return faces, labels
return faces, labels