-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.py
44 lines (39 loc) · 1.28 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
import cv2
import numpy as np
import mysql.connector
import os
conn = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='facialsketch'
)
c = conn.cursor()
fname = "recognizer/trainingData.yml"
if not os.path.isfile(fname):
print("Please train the data first")
exit(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(fname)
while True:
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), 3)
ids, conf = recognizer.predict(gray[y:y + h, x:x + w])
c.execute("SELECT name FROM users where id = %s;", (ids,))
result = c.fetchall()
name = result[0][0]
if conf < 50:
cv2.putText(img, name, (x + 2, y + h - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (150, 255, 0), 2)
else:
cv2.putText(img, 'No Match', (x + 2, y + h - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow('Face Recognizer', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()