-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdemo.py
214 lines (179 loc) · 7.57 KB
/
demo.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import argparse
import yaml
import cv2
import numpy as np
import torch
from insightface.app import FaceAnalysis
from model_factory import model_build
from dataset.utils import crop_n_align
import pdb
##########################################################
# Updated
# Multiplt face is available(11/20)
##########################################################
def demo(cfg, args, mode):
'''
Test code for face blur detection
Args:
cfg: configuration file of yaml format
args
mode: inference mode. it can be "video" or "image"
'''
##############################
# BUILD MODEL #
##############################
model = model_build(model_name=cfg['train']['model'], num_classes=1)
# only predict blur regression label -> num_classes = 1
if '.ckpt' or '.pt' in args.pretrained_path:
try:
model_state = torch.load(args.pretrained_path, map_location=args.device)
model = model.load_state_dict(model_state, map_location=args.device)
except:
model = torch.load(args.pretrained_path, map_location=args.device)
device = args.device
if 'cuda' in device and torch.cuda.is_available():
model = model.to(device)
model.eval()
##############################
# MODE : VIDEO #
##############################
if mode == 'video':
video_path = args.file_path
cap = cv2.VideoCapture(video_path, apiPreference=cv2.CAP_MSMF)
width = int(cap.get(3))
height = int(cap.get(4))
fps = int(cap.get(cv2.CAP_PROP_FPS))
save_path = args.save_path
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(save_path, fourcc, fps, (width, height))
# for face detection
app = FaceAnalysis(allowed_modules=['detection'],
providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
while(cap.isOpened()):
grabbed, frame = cap.read()
if not grabbed:
break
pad = 0
while pad <= 200:
padded = np.pad(frame, ((pad, pad), (pad, pad), (0, 0)), 'constant', constant_values=0)
face_image, boxes = crop_n_align(app, padded, box=True)
if len(face_image) > 0:
break
pad += 50
if len(face_image) > 0:
batch = torch.FloatTensor(np.array(face_image)/255).permute(0, 3, 1, 2)
with torch.no_grad():
blur_labels = model(batch)
else:
blur_labels = 'Face not found'
# Put text on the image
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (255, 255, 255)
thickness = 1
lineType = 2
if blur_labels == 'Face not found':
TextPosition = (int(width * 0.4), int(height * 0.9))
cv2.putText(frame, blur_labels,
TextPosition,
font,
fontScale,
fontColor,
thickness,
lineType)
else:
for i, box in enumerate(boxes):
left_top = (int(box[0] - pad // 2), int(box[1] - pad // 2))
right_btm = (int(box[2] - pad // 2), int(box[3] - pad // 2))
red_color = (0, 0, 255)
thickness = 3
cv2.rectangle(frame, left_top, right_btm, red_color, thickness)
blur_label = f"{blur_labels[i].item():.2f}"
TextPosition = (int(box[0] - pad), int(box[1]))
cv2.putText(frame, blur_label,
TextPosition,
font,
fontScale,
fontColor,
thickness,
lineType)
cv2.imshow('blur image', frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
##############################
# MODE : IMAGE #
##############################
if mode == 'image':
image_path = args.file_path
frame = cv2.imread(image_path)
width, height = frame.shape[0], frame.shape[1]
app = FaceAnalysis(allowed_modules=['detection'],
providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
pad = 0
while pad <= 200:
padded = np.pad(frame, ((pad, pad), (pad, pad), (0, 0)), 'constant', constant_values=0)
face_image, boxes = crop_n_align(app, padded, box=True)
if len(face_image) > 0:
break
pad += 50
if len(face_image) > 0:
batch = torch.FloatTensor(np.array(face_image)/255).permute(0, 3, 1, 2)
with torch.no_grad():
blur_labels = model(batch)
else:
blur_labels = 'Face not found'
# Put text on the image
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (255, 255, 255)
thickness = 1
lineType = 1
if blur_labels == 'Face not found':
TextPosition = (int(width*0.4), int(height*0.9))
cv2.putText(frame, blur_labels,
TextPosition,
font,
fontScale,
fontColor,
thickness,
lineType)
else:
for i, box in enumerate(boxes):
left_top = (int(box[0]-pad//2), int(box[1]-pad//2))
right_btm = (int(box[2]-pad//2), int(box[3]-pad//2))
red_color = (0, 0, 255)
thickness = 3
cv2.rectangle(frame, left_top, right_btm, red_color, thickness)
blur_label = f"{blur_labels[i].item():.2f}"
TextPosition = (int(box[0]-pad), int(box[1]))
cv2.putText(frame, blur_label,
TextPosition,
font,
fontScale,
fontColor,
thickness,
lineType)
cv2.imwrite(args.save_path, frame)
cv2.imshow('blur image', frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default='./config/CONFIG_NAME.yaml', help='Path for configuration file')
parser.add_argument('--device', type=str, default='cpu', help='Device for model inference. It can be "cpu" or "cuda" ')
parser.add_argument('--pretrained_path', type=str, default='PRETRAINED_NAME.pt', help='Path for pretrained model file')
parser.add_argument('--mode', type=str, default='video', help='Inference mode. it can be "video" or "image"')
parser.add_argument('--file_path', type=str, default='SAMPLE_NAME.mp4', help='Path for the video or image you want to infer')
parser.add_argument('--save_path', type=str, default='SAVE_PATH_NAME.mp4', help='Path for saved the inference video')
args = parser.parse_args()
with open(args.config, 'r') as f:
cfg = yaml.safe_load(f)
mode = args.mode.lower()
assert mode in ['video', 'image']
demo(cfg, args, mode)