diff --git a/Facial_Recognition_Software.py b/Facial_Recognition_Software.py index bc5ea9ebc..a94c1d7f2 100644 --- a/Facial_Recognition_Software.py +++ b/Facial_Recognition_Software.py @@ -1,62 +1,89 @@ -import cv2 +import sys +import os + +import cv2 import numpy as np -import sys,os,numpy from glob import glob from skimage import io -#read test photo -pwd = sys.path[0] -img = cv2.imread(pwd + "/Facial_test_images/6.jpg") -grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) +def face_detector_haarcascade(image): -resize_fx = 1 -resize_fy = 1 + grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) -grey = cv2.resize(grey, dsize=None, fx=resize_fx, fy=resize_fy, interpolation = cv2.INTER_AREA) + resize_fx = 1 + resize_fy = 1 + grey = cv2.resize(grey, dsize=None, fx=resize_fx, fy=resize_fy, interpolation = cv2.INTER_AREA) + + pwd = sys.path[0] + classfier = cv2.CascadeClassifier(pwd + "/Facial_models/haarcascade_frontalface_alt2.xml") + + faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=1, minSize=(16, 16)) + + if len(faceRects) > 0: + for faceRect in faceRects: + x, y, w, h = faceRect + x = int(x/resize_fx) + y = int(y/resize_fy) + w = int(w/resize_fx) + h = int(h/resize_fy) + cv2.rectangle(image, (x - 10, y - 10), (x + w + 10, y + h + 10), (0, 255, 0), 5) + + return image -classfier = cv2.CascadeClassifier(pwd + "/Facial_models/haarcascade_frontalface_alt2.xml") +def face_detector_ssd(image): -faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=1, minSize=(16, 16)) + pwd = sys.path[0] + net = cv2.dnn.readNetFromCaffe(pwd+"/Facial_models/deploy.prototxt", pwd+"/Facial_models/res10_300x300_ssd_iter_140000_fp16.caffemodel") -color = (0, 255, 0) -if len(faceRects) > 0: - for faceRect in faceRects: - x, y, w, h = faceRect - x = int(x/resize_fx) - y = int(y/resize_fy) - w = int(w/resize_fx) - h = int(h/resize_fy) - cv2.rectangle(img, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 5) + resize = (800, 800) + confidence_thres = 0.65 + + blob = cv2.dnn.blobFromImage(cv2.resize(image, dsize=resize), 1.0, resize, (104.0, 177.0, 123.0)) + # blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) -cv2.imwrite(pwd + "/Facial_test_images/output-a.jpg",img) -cv2.imshow("face_image_a",img) + net.setInput(blob) + + detections = net.forward() + + h,w,c=image.shape + + for i in range(0, detections.shape[2]): + confidence = detections[0, 0, i, 2] + if confidence > confidence_thres: + box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) + (startX, startY, endX, endY) = box.astype("int") + text = "{:.2f}%".format(confidence * 100) + y = startY - 10 if startY - 10 > 10 else startY + 10 + cv2.rectangle(image, (startX, startY), (endX, endY),(0, 255,0), 5) + cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1.00, (0, 255, 0), 3) + + return image -image = cv2.imread(pwd + "/Facial_test_images/6.jpg") +if __name__=="__main__": -net = cv2.dnn.readNetFromCaffe(pwd+"/Facial_models/deploy.prototxt", pwd+"/Facial_models/res10_300x300_ssd_iter_140000_fp16.caffemodel") + image_name = "8.jpg" + split_name = image_name.split(".") -blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) + image_read_path = sys.path[0]+"/Facial_test_images/"+image_name + image_save_path = sys.path[0]+"/Facial_test_images/output/"+split_name[0]+"_result."+split_name[1] -net.setInput(blob) + image = cv2.imread(image_read_path) -detections = net.forward() + image = face_detector_ssd(image) + #image = face_detector_haarcascade(image) + + print(image_save_path) + + cv2.imwrite(image_save_path, image) + cv2.imshow("result", image) + cv2.waitKey() + cv2.destroyAllWindows() + + -h,w,c=image.shape -for i in range(0, detections.shape[2]): - confidence = detections[0, 0, i, 2] - if confidence > 0.65: - box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) - (startX, startY, endX, endY) = box.astype("int") - text = "{:.2f}%".format(confidence * 100) - y = startY - 10 if startY - 10 > 10 else startY + 10 - cv2.rectangle(image, (startX, startY), (endX, endY),(0, 255,0), 5) - cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1.00, (0, 255, 0), 3) -cv2.imwrite(pwd + "/Facial_test_images/output-b.jpg", image) -cv2.imshow("face_image_b",image) -cv2.waitKey(0) diff --git a/Facial_test_images/3.jpg b/Facial_test_images/3.jpg index 430f5ccf4..8f6cb3967 100644 Binary files a/Facial_test_images/3.jpg and b/Facial_test_images/3.jpg differ diff --git a/Facial_test_images/8.jpg b/Facial_test_images/8.jpg new file mode 100644 index 000000000..2dbda3c41 Binary files /dev/null and b/Facial_test_images/8.jpg differ diff --git a/Facial_test_images/9.jpg b/Facial_test_images/9.jpg new file mode 100644 index 000000000..570af9af0 Binary files /dev/null and b/Facial_test_images/9.jpg differ diff --git a/Facial_test_images/output-a.jpg b/Facial_test_images/output-a.jpg deleted file mode 100644 index 595d8f2fb..000000000 Binary files a/Facial_test_images/output-a.jpg and /dev/null differ diff --git a/Facial_test_images/output-b.jpg b/Facial_test_images/output-b.jpg deleted file mode 100644 index 8b67f39c4..000000000 Binary files a/Facial_test_images/output-b.jpg and /dev/null differ diff --git a/Facial_test_images/output-b1.jpg b/Facial_test_images/output-b1.jpg deleted file mode 100644 index 2c25a7fda..000000000 Binary files a/Facial_test_images/output-b1.jpg and /dev/null differ diff --git a/Facial_test_images/output-b2.jpg b/Facial_test_images/output-b2.jpg deleted file mode 100644 index 3cd16efcc..000000000 Binary files a/Facial_test_images/output-b2.jpg and /dev/null differ diff --git a/Facial_test_images/output-b4.jpg b/Facial_test_images/output-b4.jpg deleted file mode 100644 index 73214c085..000000000 Binary files a/Facial_test_images/output-b4.jpg and /dev/null differ diff --git a/Facial_test_images/output-b5.jpg b/Facial_test_images/output-b5.jpg deleted file mode 100644 index 4faeac227..000000000 Binary files a/Facial_test_images/output-b5.jpg and /dev/null differ diff --git a/Facial_test_images/output-b6.jpg b/Facial_test_images/output-b6.jpg deleted file mode 100644 index 1fcd81a54..000000000 Binary files a/Facial_test_images/output-b6.jpg and /dev/null differ diff --git a/Facial_test_images/output/3_result.jpg b/Facial_test_images/output/3_result.jpg new file mode 100644 index 000000000..acbafe0b3 Binary files /dev/null and b/Facial_test_images/output/3_result.jpg differ diff --git a/Facial_test_images/output/3s_result.jpg b/Facial_test_images/output/3s_result.jpg new file mode 100644 index 000000000..da9cce7b0 Binary files /dev/null and b/Facial_test_images/output/3s_result.jpg differ diff --git a/Facial_test_images/output/8_result.jpg b/Facial_test_images/output/8_result.jpg new file mode 100644 index 000000000..cbc1aa45d Binary files /dev/null and b/Facial_test_images/output/8_result.jpg differ diff --git a/Facial_test_images/output/8s_result.jpg b/Facial_test_images/output/8s_result.jpg new file mode 100644 index 000000000..257bab4ea Binary files /dev/null and b/Facial_test_images/output/8s_result.jpg differ diff --git a/Facial_test_images/output/9_result.jpg b/Facial_test_images/output/9_result.jpg new file mode 100644 index 000000000..7cae966fb Binary files /dev/null and b/Facial_test_images/output/9_result.jpg differ diff --git a/Facial_test_images/output/9s_result.jpg b/Facial_test_images/output/9s_result.jpg new file mode 100644 index 000000000..099b4c925 Binary files /dev/null and b/Facial_test_images/output/9s_result.jpg differ diff --git a/Facial_test_images/output1.jpg b/Facial_test_images/output1.jpg deleted file mode 100644 index 5cf47a062..000000000 Binary files a/Facial_test_images/output1.jpg and /dev/null differ diff --git a/Facial_test_images/output2.jpg b/Facial_test_images/output2.jpg deleted file mode 100644 index a72f146eb..000000000 Binary files a/Facial_test_images/output2.jpg and /dev/null differ diff --git a/Facial_test_images/output3.jpg b/Facial_test_images/output3.jpg deleted file mode 100644 index 5b3c89dd4..000000000 Binary files a/Facial_test_images/output3.jpg and /dev/null differ diff --git a/Facial_test_images/output4.jpg b/Facial_test_images/output4.jpg deleted file mode 100644 index bdad2e39a..000000000 Binary files a/Facial_test_images/output4.jpg and /dev/null differ diff --git a/Facial_test_images/output5.jpg b/Facial_test_images/output5.jpg deleted file mode 100644 index 680d6add9..000000000 Binary files a/Facial_test_images/output5.jpg and /dev/null differ diff --git a/Facial_test_images/output6.jpg b/Facial_test_images/output6.jpg deleted file mode 100644 index 531291332..000000000 Binary files a/Facial_test_images/output6.jpg and /dev/null differ