From 6a968c196fd08ff287fc8a739665f42a6e44f80e Mon Sep 17 00:00:00 2001 From: Suraj S Date: Sat, 12 Oct 2024 21:48:33 +0530 Subject: [PATCH 1/2] Updated to Python3 --- face_detect.py | 56 ++++++++++++++++++++++++------------------------ facemaster.py | 23 ++++++++++---------- requirement.txt | 3 +++ training_data.py | 14 ++++++------ 4 files changed, 50 insertions(+), 46 deletions(-) create mode 100644 requirement.txt diff --git a/face_detect.py b/face_detect.py index a99aa19..6b12521 100644 --- a/face_detect.py +++ b/face_detect.py @@ -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) \ No newline at end of file diff --git a/facemaster.py b/facemaster.py index e6a2500..cd157e6 100644 --- a/facemaster.py +++ b/facemaster.py @@ -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) \ No newline at end of file diff --git a/requirement.txt b/requirement.txt new file mode 100644 index 0000000..61425f7 --- /dev/null +++ b/requirement.txt @@ -0,0 +1,3 @@ +numpy==2.1.2 +opencv-contrib-python==4.10.0.84 +opencv-python==4.10.0.84 \ No newline at end of file diff --git a/training_data.py b/training_data.py index 2c3ad2b..735bbd6 100644 --- a/training_data.py +++ b/training_data.py @@ -1,4 +1,6 @@ -import cv2, os, sys +import cv2 +import os +import sys import numpy as np import face_detect as face_detect @@ -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 \ No newline at end of file From cd0b1a365bc220ec699ebd8bd34bf09de8c6ac5b Mon Sep 17 00:00:00 2001 From: Suraj S Date: Sat, 12 Oct 2024 21:48:45 +0530 Subject: [PATCH 2/2] Create .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ce25c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +/__pycache__