-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detector.py
31 lines (22 loc) · 1.02 KB
/
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
import cv2
from random import randrange
# load some pre-trained data on face frontal from opencv (haaar cascade algorithm)
# and use them to detect faces
trained_face_data = cv2.CascadeClassifier('./TrainData/haarcascade_frontalface_default.xml')
# choose image to detect faces in
img = cv2.imread('./test.jpg')
# convert to grayscale to determine the face coordinate easily
grayscaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detect faces coordinates
# Detected objects are returned as a list of rectangles
face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
# surround the faces with rectangles using all coordinates
for (x, y, w, h) in face_coordinates:
cv2.rectangle(img, (x, y), (x+w, y+h), (randrange(128, 256), randrange(128, 256), randrange(128, 256)), 3)
# display the image on a window
cv2.imshow('python image detection', img)
# prevent the window from automatically close
cv2.waitKey()
# save the image with all face surrounded by rectangles
# cv2.imwrite('image.jpeg', img)
print("code completed")