-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathface-recognition-detector.py
More file actions
63 lines (54 loc) · 2.42 KB
/
Copy pathface-recognition-detector.py
File metadata and controls
63 lines (54 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import os
import cv2
from PIL import Image
import pickle, sqlite3
face_cascade = cv2.CascadeClassifier('Classifiers/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('Classifiers/haarcascade_eye.xml')
recognizer = cv2.face.createLBPHFaceRecognizer()
recognizer.load("trainer/training_data.yml")
#To train using images captured or saved online
# img = cv2.imread("him.jpg")
def getProfile(Id):
conn=sqlite3.connect("FaceBase")
query="SELECT * FROM People WHERE ID="+str(Id)
cursor=conn.execute(query)
profile=None
for row in cursor:
profile=row
conn.close()
return profile
#to train using frames from video
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_COMPLEX
while True:
#comment the next line and make sure the image being read is names img when using imread
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
# Hiding the eye detector for now
# eyes = eye_cascade.detectMultiScale(roi_gray)
# for (ex, ey, ew, eh) in eyes:
# cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (255, 0, 0), 2)
nbr_predicted, conf = recognizer.predict(gray[y:y+h, x:x+w])
if conf < 70:
profile=getProfile(nbr_predicted)
if profile != None:
cv2.putText(img, "Name: "+str(profile[1]), (x, y+h+30), font, 0.4, (0, 0, 255), 1);
cv2.putText(img, "Age: " + str(profile[2]), (x, y + h + 50), font, 0.4, (0, 0, 255), 1);
cv2.putText(img, "Gender: " + str(profile[3]), (x, y + h + 70), font, 0.4, (0, 0, 255), 1);
cv2.putText(img, "Criminal Records: " +str(profile[4]), (x, y + h + 90), font, 0.4, (0, 0, 255), 1);
else:
cv2.putText(img, "Name: Unknown", (x, y + h + 30), font, 0.4, (0, 0, 255), 1);
cv2.putText(img, "Age: Unknown", (x, y + h + 50), font, 0.4, (0, 0, 255), 1);
cv2.putText(img, "Gender: Unknown", (x, y + h + 70), font, 0.4, (0, 0, 255), 1);
cv2.putText(img, "Criminal Records: Unknown", (x, y + h + 90), font, 0.4, (0, 0, 255), 1);
cv2.imshow('img', img)
if(cv2.waitKey(1) == ord('q')):
break
cap.release()
cv2.destroyAllWindows()