-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.py
61 lines (45 loc) · 1.72 KB
/
detector.py
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
import cv2
import numpy as np
import sqlite3
faceDetect = cv2.CascadeClassifier(r'C:\\Users\\maajdkhan\\Desktop\\OpenCV-Python_Face_Recognition-SQLite--master\\haarcascade_frontalface_default.xml')
# capture frames from a camera
cap = cv2.VideoCapture(0)
rec=cv2.createLBPHFaceRecognizer()
rec.load('recognizer\\trainingData.yml')
id=0
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,5,1,0,4)
def getProfile(id):
conn=sqlite3.connect("FaceDB.db")
cmd="SELECT * FROM People WHERE ID="+str(id)
cursor=conn.execute(cmd)
profile=None
for row in cursor:
profile=row
conn.close()
return profile
# loop runs if capturing has been initialized.
while 1:
# reads frames from a camera
ret, img = cap.read()
# convert to gray scale of each frames
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detects faces of different sizes in the input image
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
# To draw a rectangle in a face
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
id,conf=rec.predict(gray[y:y+h,x:x+w])
profile=getProfile(id)
if(profile!=None):
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[0]),(x,y+50),font,255);
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[1]),(x,y+100),font,255);
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[2]),(x,y+150),font,255);
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[3]),(x,y+200),font,255);
cv2.imshow('img',img)
# Wait for Esc key to stop
if(cv2.waitKey(1) == ord('q')):
break;
# Close the window
cap.release()
# De-allocate any associated memory usage
cv2.destroyAllWindows()