-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_faces.py
More file actions
85 lines (67 loc) · 2.44 KB
/
Copy pathadd_faces.py
File metadata and controls
85 lines (67 loc) · 2.44 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
import cv2 as cv
import pickle
import numpy as np
import os
if not os.path.exists('data/'):
os.makedirs('data/')
video = cv.VideoCapture(0)
facedetect= cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces_data = []
attempts = 0
while attempts < 3:
name = input("Enter your aadhar number: ")
if name.isdigit() and len(name) == 12:
print("Valid Aadhar number entered!")
break
else:
attempts += 1
print("Invalid Aadhar number. Please enter a 12-digit numeric value.")
if attempts == 3:
print("Too Many Attempts.")
exit()
framesTotal=51
captureAfterFrame=2
i=0
while True:
ret, frame = video.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.2 ,6) #1.2 scale of how many different frame resizes to be processed {1.1 - 1.5}, 6 accuracy of detection {3-6}
for (x, y, w, h) in faces:
crop_img = frame[y:y+h, x:x+w]
resized_img = cv.resize(crop_img, (50, 50))
if len(faces_data)<= framesTotal and i%captureAfterFrame==0:
faces_data.append(resized_img)
i=i+1
cv.putText(frame, str(len(faces_data)),(50,50),cv.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 1 )
cv.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv.imshow('frame', frame)
k=cv.waitKey(1)
if k== ord('q') or len(faces_data) >= framesTotal:
break
video.release()
cv.destroyAllWindows()
faces_data = np.asarray(faces_data)
faces_data = faces_data.reshape((framesTotal, -1))
print(faces_data)
if 'names.pkl' not in os.listdir('data/'):
names=[name]*framesTotal
with open('data/names.pkl', 'wb') as f:
pickle.dump(names, f)
else:
with open('data/names.pkl', 'rb') as f:
names=pickle.load(f)
names=names+[name]*framesTotal
with open('data/names.pkl', 'wb') as f:
pickle.dump(names, f)
if 'faces_data.pkl' not in os.listdir('data/'):
with open('data/faces_data.pkl', 'wb') as f:
pickle.dump(faces_data, f)
else:
with open('data/faces_data.pkl', 'rb') as f:
faces=pickle.load(f)
faces=np.append(faces, faces_data, axis=0)
with open('data/faces_data.pkl', 'wb') as f:
pickle.dump(faces, f)
if name in names:
print("Congratulations, You are enrolled Successfully")
exit()