-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacedetection.py
More file actions
44 lines (30 loc) · 1.09 KB
/
facedetection.py
File metadata and controls
44 lines (30 loc) · 1.09 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
import cv2
face_classifier = cv2.CascadeClassifier('C:/Users/pulkit/AppData/Local/Programs/Python/Python37-32/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
def face_extractor(img):
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray,1.3,5)
if faces is():
return None
for(x,y,w,h) in faces:
cropped_face = img[y:y+h,x:x+w]
return cropped_face
cap = cv2.VideoCapture(0)
count =0
while True:
ret,frame=cap.read()
if face_extractor(frame) is not None:
count+=1
face = cv2.resize(face_extractor(frame),(400,640))
face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
file_name = 'D:/faces/person'+str(count)+'.jpg'
cv2.imwrite(file_name,face)
Font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(face,str(count),(50,50),Font,1,(0,0,255),2)
cv2.imshow('face cropped',face)
else:
print("not found")
pass
if cv2.waitKey(1)==27 or count==100:
break
cap.release()
cv2.destroyAllWindows()