-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreal-time-de-identification.py
153 lines (124 loc) · 4.86 KB
/
real-time-de-identification.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import cv2
import numpy as np
import tensorflow as tf
from tensorflow import keras
from scipy.ndimage import gaussian_filter
# Color Code
RED = (0, 0, 255)
GREEN = (0, 255, 0)
# Instead of Streaming
VIDEO_FILE = '/Users/praharshgurudatta/Downloads/My Videos/202109080914_45_1.mp4'
# Detect Face
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Load Model
model_name = 'face_classifier.h5'
face_classifier = keras.models.load_model(f'models/{model_name}')
class_names = ['me', 'not_me']
# Check the Coordinates will be Non-negative
def get_extended_image(img, x, y, w, h, k=0.1): # img, x, y, w, h: from video
if x - k * w > 0:
start_x = int(x - k * w)
else:
start_x = x
if y - k * h > 0:
start_y = int(y - k * h)
else:
start_y = y
end_x = int(x + (1 + k) * w)
end_y = int(y + (1 + k) * h)
face_image = img[start_y:end_y, start_x:end_x]
face_image = tf.image.resize(face_image, [250, 250]) # Check the Shape
# shape from (250, 250, 3) to (1, 250, 250, 3)
face_image = np.expand_dims(face_image, axis=0)
return face_image
# Streaming or Video File
video_capture = cv2.VideoCapture()
if not video_capture.isOpened():
print("Unable to access the camera, Replaces with saved video files")
video_capture = cv2.VideoCapture(VIDEO_FILE) # Instead of Streaming
else:
print("Access to the camera was successfully obtained")
video_capture = cv2.VideoCapture(0) # Check the Activated Cam Number
print("Streaming started - to quit press ESC")
# Loop for Real-Time
while True:
ret, frame = video_capture.read()
if not ret:
print("Can't Receive Frame, Exit Streaming")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(100, 100),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Loop
for (x, y, w, h) in faces:
face_image = get_extended_image(frame, x, y, w, h, 0.5)
# Predict Result
result = face_classifier.predict(face_image)
prediction = class_names[np.array(
result[0]).argmax(axis=0)] # predicted class
confidence = np.array(result[0]).max(axis=0) # degree of confidence
# Draw a rectangle around the face
if prediction == 'me':
# <de-identification>
# Select One Option You Want, Then Delete the Other Options
# Opt 1. gaussian_filter() : face_blurred
# face_blurred = frame
# face_blurred[y:y + h, x:x + w] = gaussian_filter(face_blurred[y:y + h, x:x + w], 10)
# Opt 2. cv2.blur() : cv2_blur
# cv2_blur = frame
# cv2_blur[y:y + h, x:x + w] = cv2.blur(cv2_blur[y:y + h, x:x + w], (30, 30))
# Opt 3. cv2.GaussianBlur() : cv2_Gau_blur
cv2_Gau_blur = frame
cv2_Gau_blur[y:y + h, x:x + w] = cv2.GaussianBlur(cv2_Gau_blur[y:y + h, x:x + w], (9, 9), 10)
# # Opt 4. Resize the image : mosaic
# if len(faces):
# for (x, y, w, h) in faces:
# mosaic = frame[y:y + h, x:x + w]
# mosaic = cv2.resize(mosaic, dsize=(0, 0), fx=0.04, fy=0.04) # Reduction
# mosaic = cv2.resize(mosaic, (w, h), interpolation=cv2.INTER_AREA) # Expansion
# frame[y:y + h, x:x + w] = mosaic
# color = GREEN
# Insert the Option You Choose
cv2.rectangle(mosaic, # Original: frame
(x, y),
(x + w, y + h),
color,
2) # thickness in px
cv2.putText(frame,
# text to put
"{:6} - {:.2f}%".format(prediction, confidence * 100),
(x, y),
cv2.FONT_HERSHEY_PLAIN, # font
2, # fontScale
color,
2) # thickness in px
else:
color = RED
# Draw a rectangle around the face
cv2.rectangle(frame,
(x, y),
(x + w, y + h),
color,
2) # thickness in px
cv2.putText(frame,
# text to put
"{:6} - {:.2f}%".format(prediction, confidence * 100),
(x, y),
cv2.FONT_HERSHEY_PLAIN, # font
2, # fontScale
color,
2) # thickness in px
# Display
cv2.imshow("Real-time Face detector", frame)
# Exit with ESC
key = cv2.waitKey(1)
if key % 256 == 27: # ESC code
break
video_capture.release()
cv2.destroyAllWindows()