-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset_creator.py
More file actions
145 lines (106 loc) · 5.41 KB
/
Copy pathdataset_creator.py
File metadata and controls
145 lines (106 loc) · 5.41 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
132
133
134
135
136
137
138
139
140
141
142
143
144
import cv2 # for camera
import numpy as np # for array
import sqlite3 # Database
import trainer as ts
def run_dataset_creator():
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeDetect =cv2.CascadeClassifier('haarcascade_eye.xml')
sideFace = cv2.CascadeClassifier('haarcascade_profileface.xml')
noseDetect =cv2.CascadeClassifier('Nariz.xml')
mouthDetect = cv2.CascadeClassifier('Mouth.xml')
cam = cv2.VideoCapture(0)
def insertorupdate(id, name, age):
connection = sqlite3.connect("face_recognition.db")
command = "SELECT * FROM USERS WHERE ID="+str(id)
cursor = connection.execute(command)
ifRecordExist = 0
for row in cursor:
ifRecordExist = 1
if(ifRecordExist == 1):
connection.execute("UPDATE USERS SET NAME=? WHERE ID=?", (name, id))
connection.execute("UPDATE USERS SET AGE=? WHERE ID=?", (age, id))
else:
connection.execute("INSERT INTO USERS (ID, NAME, AGE) values(?,?,?)",(id, name, age))
connection.commit()
connection.close()
Id = input('Enter User ID: ')
Name = input('Enter User Name: ')
Age = input('Enter User Age: ')
insertorupdate(Id, Name, Age)
sampleNumber = 0 # Asumming there is nothing in our dataset
while(True):
ret, image = cam.read() # Open Camera
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert camera footage to gray image
frontalFace = faceDetect.detectMultiScale(gray, 1.3, 5) # scale face
leftSide = sideFace.detectMultiScale(gray, 1.3, 5)
# So we can detect the right part of the face
flipped = cv2.flip(gray, 1)
rightSide = sideFace.detectMultiScale(flipped, 1.3, 5)
# eyes = eyeDetect.detectMultiScale(gray, 1.5, 14 )
'''
nose = noseDetect.detectMultiScale(gray, 1.5, 5)
mouth = mouthDetect.detectMultiScale(gray, 1.5, 20)
'''
for (x,y,w,h) in frontalFace:
sampleNumber = sampleNumber + 1
# Drawing a rectangle around the face
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255))
#Defining the region of intrest (ROI) for eyes within the detected face
face_gray = gray[y: y +h, x: x + w]
face_color = image[y: y + h, x: x + w]
# Detect eyes within the face region
eyes = eyeDetect.detectMultiScale(face_gray, 1.3, 5)
for (eye_x, eye_y, eye_w, eye_h) in eyes:
# Draw rectangle around each detected eye within the face
cv2.rectangle(face_color, (eye_x, eye_y), (eye_x + eye_w, eye_y + eye_h), (0, 255, 0), 2)
# cv2.waitKey(100)
# Save face region to daaset
cv2.imwrite("data/user."+str(Id)+"."+str(sampleNumber)+ ".jpg",gray[y : y + h, x : x + w])
cv2.imshow("Face Scan", image)
cv2.waitKey(1)
for (x,y,w,h) in leftSide:
sampleNumber = sampleNumber + 1
# Save face region to daaset
#cv2.imwrite("data/user."+str(Id)+"."+str(sampleNumber)+ ".jpg",gray[y : y + h, x : x + w])
# Drawing a rectangle around the left side of the face
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255))
#Defining the region of intrest (ROI) for eyes within the detected face
face_gray = gray[y: y +h, x: x + w]
face_color = image[y: y + h, x: x + w]
# Detect eyes within the face region
eyes = eyeDetect.detectMultiScale(face_gray, 1.3, 5)
for (eye_x, eye_y, eye_w, eye_h) in eyes:
# Draw rectangle around each detected eye within the face
cv2.rectangle(face_color, (eye_x, eye_y), (eye_x + eye_w, eye_y + eye_h), (0, 255, 0), 2)
# cv2.waitKey(100)
# Save face region to daaset
cv2.imwrite("data/user."+str(Id)+"."+str(sampleNumber)+ ".jpg",gray[y : y + h, x : x + w])
for (x,y,w,h) in rightSide:
sampleNumber = sampleNumber + 1
# Save face region to daaset
#cv2.imwrite("data/user."+str(Id)+"."+str(sampleNumber)+ ".jpg",gray[y : y + h, x : x + w])
# Drawing a rectangle around the left side of the face
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255))
#Defining the region of intrest (ROI) for eyes within the detected face
face_gray = gray[y: y +h, x: x + w]
face_color = image[y: y + h, x: x + w]
# Detect eyes within the face region
eyes = eyeDetect.detectMultiScale(face_gray, 1.3, 5)
for (eye_x, eye_y, eye_w, eye_h) in eyes:
# Draw rectangle around each detected eye within the face
cv2.rectangle(face_color, (eye_x, eye_y), (eye_x + eye_w, eye_y + eye_h), (0, 255, 0), 2)
# cv2.waitKey(100)
# Save face region to daaset
cv2.imwrite("data/user."+str(Id)+"."+str(sampleNumber)+ ".jpg",gray[y : y + h, x : x + w])
cv2.waitKey(100)
cv2.imshow("Face Scan", image)
cv2.waitKey(1)
if(sampleNumber >= 500):
break
cam.release()
cv2.destroyAllWindows()
def main():
run_dataset_creator()
ts.main()
if __name__ == "__main__":
main()