-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime_face_detection_blur.py
67 lines (50 loc) · 2.14 KB
/
realtime_face_detection_blur.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 7 17:25:02 2023
@author: slbouknight
"""
# Import required libraries
import cv2
import face_recognition
# Get default webcam video stream
webcam_video_stream = cv2.VideoCapture(0)
# Array to store face locations
all_face_locations = []
# Loop through each video frame until user exits
while True:
ret, current_frame = webcam_video_stream.read()
# Lets use a smaller version (0.25x) of the image for faster processing
scale_factor = 4
current_frame_small = cv2.resize(
current_frame, (0, 0), fx=1/scale_factor, fy=1/scale_factor)
# Find total number of faces
all_face_locations = face_recognition.face_locations(
current_frame_small, number_of_times_to_upsample=2, model='hog')
# Let's print the location of each of the detected faces
for index, current_face_location in enumerate(all_face_locations):
# Splitting up tuple of face location
top_pos, right_pos, bottom_pos, left_pos = current_face_location
# Correct positions based on scale factor
top_pos *= scale_factor
right_pos *= scale_factor
bottom_pos *= scale_factor
left_pos *= scale_factor
print(
f'Found face {index + 1} at location Top: {top_pos}, Left: {left_pos}, Bottom: {bottom_pos}, Right: {right_pos}')
# Now we'll slice our image array to isolate the faces, then blur them
current_face_image = current_frame[top_pos: bottom_pos,
left_pos:right_pos]
current_face_image = cv2.GaussianBlur(current_face_image, (99, 99), 30)
# Paste blurred face over current frame
current_frame[top_pos: bottom_pos,
left_pos:right_pos] = current_face_image
# Draw rectangle around each face in video frame
cv2.rectangle(current_frame, (left_pos, top_pos),
(right_pos, bottom_pos), (0, 0, 255), 2)
# Show current face with rectangle
cv2.imshow('Webcam Video', current_frame)
# Press 'enter' key to exit loop
if cv2.waitKey(1) == 13:
break
webcam_video_stream.release()
cv2.destroyAllWindows()