-
Notifications
You must be signed in to change notification settings - Fork 7
/
face_detection_opencv_haar.py
71 lines (53 loc) · 1.75 KB
/
face_detection_opencv_haar.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
61
62
63
64
65
66
67
68
69
70
71
# import required packages
import cv2
import argparse
# handle command-line arguments
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', help='path to image file')
ap.add_argument('-v', '--video', help='path to video file')
args = ap.parse_args()
image_file = args.image
video_file = args.video
# initialize face detector
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# process image input (if provided)
if image_file:
img = cv2.imread(image_file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
cv2.imshow("face detection - opencv haar", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
exit()
# check for video input / webcam
if video_file:
webcam = cv2.VideoCapture(video_file)
else:
webcam = cv2.VideoCapture(0)
if not webcam.isOpened():
print("Could not open webcam")
exit()
# process frames one by one
while(webcam.isOpened()):
# read frame
status, frame = webcam.read()
if not status:
print("Could not read frame")
exit()
# convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# apply face detection
faces = face_detector.detectMultiScale(gray, 1.3, 5)
# draw boxes over detected faces
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 2)
# display output frame
cv2.imshow("face detection - opencv haar", frame)
# press 'Q' to stop the program
if cv2.waitKey(1) & 0XFF == ord('q'):
break
# release resources
webcam.release()
cv2.destroyAllWindows()