Skip to content

Commit f1098c6

Browse files
Initial files.
1 parent d44a7ef commit f1098c6

File tree

4 files changed

+33374
-0
lines changed

4 files changed

+33374
-0
lines changed

face_detect.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cv2
2+
import sys
3+
from matplotlib.pyplot import imshow, show
4+
5+
6+
def collect_detected_faces(image_path, casc_path=None, draw_rectangles=False,
7+
show_image=False):
8+
if casc_path is None:
9+
casc_path = "haarcascade_frontalface_default.xml"
10+
11+
# Create the haar cascade
12+
face_cascade = cv2.CascadeClassifier(casc_path)
13+
14+
# Read the image
15+
image = cv2.imread(image_path)
16+
gray = cv2.imread(image_path, 0)
17+
18+
# Detect faces in the image
19+
faces = face_cascade.detectMultiScale(
20+
gray,
21+
scaleFactor=1.1,
22+
minNeighbors=5,
23+
minSize=(30, 30)
24+
# flags = cv2.CV_HAAR_SCALE_IMAGE
25+
)
26+
if draw_rectangles:
27+
# Draw a rectangle around the faces
28+
for (x, y, w, h) in faces:
29+
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2, 4)
30+
31+
if show_image:
32+
imshow(image)
33+
show()
34+
35+
return faces, image
36+
37+
38+
if __name__ == "__main__":
39+
# Get user supplied values
40+
imagePath = sys.argv[1]
41+
cascPath = "haarcascade_frontalface_default.xml"
42+
43+
faces, image = collect_detected_faces(imagePath, draw_rectangles=True,
44+
show_image=True)
45+
print("Found {0} faces!".format(len(faces)))

0 commit comments

Comments
 (0)