-
Notifications
You must be signed in to change notification settings - Fork 2
/
face-recognition.py
58 lines (49 loc) · 1.82 KB
/
face-recognition.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
import numpy as np
import os
import sqlite3
import cv2
def insertOrUpdate(id, name):
#connecting to the db
conn =sqlite3.connect("FaceBase")
#check if id already exists
query = "SELECT * FROM People WHERE ID="+str(id)
#returning the data in rows
cursor = conn.execute(query)
isRecordExist=0
for row in cursor:
isRecordExist=1
if isRecordExist==1:
query="UPDATE People SET Name="+str(name)+" WHERE ID="+str(id)
else:
query="INSERT INTO People(ID, Name) VALUES("+str(id)+","+str(name)+")"
conn.execute(query)
conn.commit()
conn.close()
face_cascade = cv2.CascadeClassifier('/home/meracoda/Desktop/face_recognition_with_sqlite-master/Classifiers/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/meracoda/Desktop/face_recognition_with_sqlite-master/Classifiers/haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
id = input('Enter user id: ')
name = input('Enter name: ')
insertOrUpdate(id, name)
sample_number = 0
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:
sample_number += 1
if not os.path.exists('dataSet'):
os.makedirs('dataSet')
cv2.imwrite('dataSet/User.'+str(id)+"."+str(sample_number)+".jpg", gray[y:y+h,x:x+w])
cv2.rectangle(img, (x-50,y-50), (x+w+50, y+h+50), (0,255,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex-50, ey-50), (ex+ew+50, ey+eh+50), (0, 0, 255), 2)
cv2.imshow('img', img)
cv2.waitKey(1);
if(sample_number>30):
cap.release()
cv2.destroyAllWindows()
break;