-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiFaceDetectionWebcam.py
60 lines (44 loc) · 1.68 KB
/
multiFaceDetectionWebcam.py
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
import cv2
import matplotlib.pyplot as plt
import numpy as np
# importing_haarcascade_classifiers
face_classifier =cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
eye_classifier =cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml')
# face_detection_function
def face_detection(image):
# grayscaling_image_passed
if ret is True:
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# detecting_faces
faces=face_classifier.detectMultiScale(gray,1.6,7)
number = 0;
# drawing_face_rectangle
for (x,y,w,h) in faces:
# draw_rectangle_around_face
cv2.rectangle(image,(x,y),(x+w,y+h),(127,0,255),2)
# saving face image
plt.imsave('name'+str(number)+'.jpg', gray[y:y + h, x:x + w])
number = number + 1
# cropping_face_only
roi_color=image[y:y+h,x:x+w]
roi_gray=gray[y:y+h,x:x+w]
# eyes_detection
eyes=eye_classifier.detectMultiScale(roi_gray)
# drawing_eyes_rectangles
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(255,255,0),2)
# returning_image_with_rectangles
return image
# capturing_video_from_webcam
cap=cv2.VideoCapture(0)
while True:
# reading_from_camera
ret,frame=cap.read()
cv2.imshow('face_detection',face_detection(frame))
# if_enter_pressed_then_exit
if cv2.waitKey(1)==13:
break
# releasing_camera
cap.release()
# destroying_window
cv2.destroyAllWindows()