-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceDetection.py
More file actions
38 lines (30 loc) · 1.06 KB
/
FaceDetection.py
File metadata and controls
38 lines (30 loc) · 1.06 KB
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
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# from .. import Filters
from ..Filters import gray
import cv2
import sys
def detect_faces(img, scaleFactor=1.1, minNeighbors=5, minSize=(25, 25), cascPath="haarcascade_frontalface_default.xml"):
gray_img = gray(img).astype(np.uint8)
output = np.copy(img).astype(np.uint8)
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascPath)
faces = faceCascade.detectMultiScale(
gray_img,
scaleFactor=scaleFactor,
minNeighbors=minNeighbors,
minSize=minSize,
flags=cv2.CASCADE_SCALE_IMAGE
)
# print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(output, (x, y), (x+w, y+h), (0, 255, 0), 2)
return output
if __name__ == '__main__':
img = mpimg.imread("src/testImgs/zmlk.jpg")
f, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 6))
faces = detect_faces(img)
ax.imshow(faces, cmap="gray")
plt.axis("off")
plt.show()