forked from shantnu/FaceDetect
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathface_detect_python2.py
More file actions
37 lines (28 loc) · 808 Bytes
/
face_detect_python2.py
File metadata and controls
37 lines (28 loc) · 808 Bytes
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
#coding=utf8
import cv2
import sys
# python face_detect.py t3.jpg haarcascade_frontalface_alt.xml
# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.25,
minNeighbors=2,
minSize=(30,30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
print "Found {0} faces!".format(len(faces))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces found", image)
cv2.imwrite("face_detection.jpg", image)
cv2.waitKey(0)
cv2.destroyAllWindows()