-
Notifications
You must be signed in to change notification settings - Fork 0
/
haar_face_detector.py
46 lines (39 loc) · 1.52 KB
/
haar_face_detector.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
# USAGE
# python haar_face_detector.py --image images/adrian_01.png
# import the necessary packages
import argparse
import imutils
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str, required=True,
help="path to input image")
ap.add_argument("-c", "--cascade", type=str,
default="./face_detector/haarcascade_frontalface_default.xml",
help="path to haar cascade face detector")
ap.add_argument("-s", "--save", type=str, default="image.png", help="Place Where to Save File", required=False)
args = vars(ap.parse_args())
# load the haar cascade face detector from
print("[INFO] loading face detector...")
detector = cv2.CascadeClassifier(args["cascade"])
# load the input image from disk, resize it, and convert it to
# grayscale
image = cv2.imread(args["image"])
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the input image using the haar cascade face
# detector
print("[INFO] performing face detection...")
rects = detector.detectMultiScale(gray, scaleFactor=1.05,
minNeighbors=5, minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE)
print("[INFO] {} faces detected...".format(len(rects)))
# loop over the bounding boxes
for (x, y, w, h) in rects:
# draw the face bounding box on the image
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show the output image
cv2.imwrite(str(args["save"]),image)
print("[INFO] Saved the File on the Disc")
cv2.imshow("Image", image)
cv2.waitKey(0)