-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecognizer.py
More file actions
131 lines (117 loc) · 3.85 KB
/
Copy pathRecognizer.py
File metadata and controls
131 lines (117 loc) · 3.85 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import cv2
import numpy as np
from PIL import Image
import sqlite3
from num2words import num2words
from subprocess import call
path = 'dataSet';
def getImagesWithID(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faces = [];
IDs = [];
for imagePath in imagePaths:
if(imagePath!='dataSet\\Thumbs.db'): # To dump the hidden file Thumbs.db
faceImg = Image.open(imagePath).convert('L');
faceNp = np.array(faceImg, 'uint8');
ID = int(os.path.split(imagePath)[-1].split('.')[1]);
faces.append(faceNp);
IDs.append(ID);
cv2.imshow("training", faceNp);
cv2.waitKey(10);
return np.array(IDs), faces
def train():
Ids, faces = getImagesWithID(path);
rec.train(faces,Ids);
rec.save('recognizer/trainingData.yml');
def insorup(ID, Name):
conn=sqlite3.connect("FaceBase.db");
cmd="SELECT ID FROM People WHERE ID="+str(ID);
cursor=conn.execute(cmd);
isRecordExist=0;
for row in cursor:
isRecordExist=1
if(isRecordExist==1):
cmd="UPDATE People SET Name='"+str(Name)+"' WHERE ID="+str(ID);
else:
cmd="INSERT INTO People(ID,Name) Values("+str(ID)+",'"+str(Name)+"')";
conn.execute(cmd);
conn.commit();
conn.close();
def dataCreate():
cmd = 'Please_enter_your_ID_in_Py_Shell';
call([cmd_beg+cmd+cmd_end], shell=True);
id = raw_input('Enter your id: ');
if(id=='0'):
return;
cmd = 'Please_enter_your_name';
call([cmd_beg+cmd+cmd_end], shell=True);
name = raw_input('Enter your Name: ');
insorup(id,name);
sampleNum=0;
while(True):
ret, img = cam.read();
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);
faces = faceDetect.detectMultiScale(gray, 1.3, 5);
for(x,y,w,h) in faces:
sampleNum = sampleNum+1;
cv2.imwrite("dataSet/User."+id+"."+str(sampleNum)+".jpg", gray[y:y+h,x:x+w]);
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2);
cv2.waitKey(100);
cv2.imshow("Face", img);
cv2.waitKey(1);
if(sampleNum>20):
break;
train();
def getProfile(id):
conn=sqlite3.connect("FaceBase.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;
cmd_beg= 'espeak '
cmd_end= ' 2>/dev/null' # To dump the std errors to /dev/null
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam = cv2.VideoCapture(0);
cam.set(3,640);
rec = cv2.createLBPHFaceRecognizer();
train(); # don't leave dataset folder empty before running
rec.load('recognizer/trainingData.yml');
id = 0;
c=0
prename="";
font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 5,1,0,4);
name="";
while(True):
ret,img = cam.read();
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);
faces = faceDetect.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);
id, conf = rec.predict(gray[y:y+h, x:x+w]);
if(conf<70):
c=0;
profile=getProfile(id);
if(profile!=None):
name=str(profile[1]);
if(prename!=name):
cmd = 'Welcome_'+name;
call([cmd_beg+cmd+cmd_end], shell=True);
prename=name;
else:
name="unknown"
prename="";
c=c+1;
if(c>4):
cmd = 'Welcome_'+name;
call([cmd_beg+cmd+cmd_end], shell=True);
dataCreate();
c=0;
cv2.cv.PutText(cv2.cv.fromarray(img),name, (x,y+h), font, 255);
cv2.imshow("Face", img);
if(cv2.waitKey(1)==ord('q')):
cam.release();
cv2.destroyAllWindows();