Inderdev07 commited on
Commit
e476409
·
1 Parent(s): 63fbf03

Create add_faces.py

Browse files
Files changed (1) hide show
  1. add_faces.py +56 -0
add_faces.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import pickle
3
+ import numpy as np
4
+ import os
5
+ video=cv2.VideoCapture(0)
6
+ facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
7
+
8
+ faces_data=[]
9
+
10
+ i=0
11
+
12
+ name=input("Enter Your Name: ")
13
+
14
+ while True:
15
+ ret,frame=video.read()
16
+ gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
17
+ faces=facedetect.detectMultiScale(gray, 1.3 ,5)
18
+ for (x,y,w,h) in faces:
19
+ crop_img=frame[y:y+h, x:x+w, :]
20
+ resized_img=cv2.resize(crop_img, (50,50))
21
+ if len(faces_data)<=100 and i%10==0:
22
+ faces_data.append(resized_img)
23
+ i=i+1
24
+ cv2.putText(frame, str(len(faces_data)), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 1)
25
+ cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
26
+ cv2.imshow("Frame",frame)
27
+ k=cv2.waitKey(1)
28
+ if k==ord('q') or len(faces_data)==100:
29
+ break
30
+ video.release()
31
+ cv2.destroyAllWindows()
32
+
33
+ faces_data=np.asarray(faces_data)
34
+ faces_data=faces_data.reshape(100, -1)
35
+
36
+
37
+ if 'names.pkl' not in os.listdir('data/'):
38
+ names=[name]*100
39
+ with open('data/names.pkl', 'wb') as f:
40
+ pickle.dump(names, f)
41
+ else:
42
+ with open('data/names.pkl', 'rb') as f:
43
+ names=pickle.load(f)
44
+ names=names+[name]*100
45
+ with open('data/names.pkl', 'wb') as f:
46
+ pickle.dump(names, f)
47
+
48
+ if 'faces_data.pkl' not in os.listdir('data/'):
49
+ with open('data/faces_data.pkl', 'wb') as f:
50
+ pickle.dump(faces_data, f)
51
+ else:
52
+ with open('data/faces_data.pkl', 'rb') as f:
53
+ faces=pickle.load(f)
54
+ faces=np.append(faces, faces_data, axis=0)
55
+ with open('data/faces_data.pkl', 'wb') as f:
56
+ pickle.dump(faces, f)