-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacial_recognition.py
33 lines (27 loc) · 1.19 KB
/
facial_recognition.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
import cv2 as cv
import os
import time
def tagFaces(directory):
save_path = os.path.join(f"{os.getcwd()}", "Detected_Faces")
if not os.path.exists(save_path):
os.mkdir(save_path)
haar_cascade_path = \
"C:\\Users\\JOE\\JOB\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"
for filename in os.listdir(directory):
start_time = time.clock()
original_image = cv.imread(os.path.join(directory, filename))
grayscale_image = cv.cvtColor(original_image, cv.COLOR_BGR2GRAY)
face_cascade = cv.CascadeClassifier(haar_cascade_path)
detected_faces = face_cascade.detectMultiScale(grayscale_image)
for (column, row, width, height) in detected_faces:
cv.rectangle(
original_image,
(column, row),
(column + width, row + height),
(0, 255, 0),
2
)
image_save_path = os.path.join(save_path, filename)
cv.imwrite(image_save_path, original_image)
process_time = time.clock() - start_time
print(f"processed.....{filename} of {len(os.listdir(directory))} in {process_time:.3f} seconds")