-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo_multi.py
131 lines (100 loc) · 4.47 KB
/
demo_multi.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
from cv2 import CAP_V4L2
import tensorflow as tf
import numpy as np
import time
from datetime import datetime
from scipy.ndimage.filters import gaussian_filter
import cv2
#---setting------------------------------#
# recording setting
recording = False
recording_time = 25
# model input size
input_size = 368#200
# ear model
landmark_size = 55
ear_part_num = [20, 15, 15, 5]
ear_threshold = 0.5
# face model
landmark_size_face = 68
face_threshold = 0.0
# resize setting for coordinate correction
r_size = 368#200
# capture size (Default = 620x480)
IM_W = 1280
IM_H = 720
# output size (Default = 620x480)
o_size_w = 720
o_size_h = 720
#----------------------------------------#
def _gaussian_kernel(kernel_size, sigma, n_channels, dtype):
x = tf.range(-kernel_size // 2 + 1, kernel_size // 2 + 1, dtype=dtype)
g = tf.math.exp(-(tf.pow(x, 2) / (2 * tf.pow(tf.cast(sigma, dtype), 2))))
g_norm2d = tf.pow(tf.reduce_sum(g), 2)
g_kernel = tf.tensordot(g, g, axes=0) / g_norm2d
g_kernel = tf.expand_dims(g_kernel, axis=-1)
return tf.expand_dims(tf.tile(g_kernel, (1, 1, n_channels)), axis=-1)
def apply_blur(img, landmark):
blur = _gaussian_kernel(5, 2.5, landmark, img.dtype)
img = tf.nn.depthwise_conv2d(img, blur, [1,1,1,1], 'SAME')
return img[0]
color_list = [(0,255,0), (255,51,0), (255,204,0), (255,204,0)]
model = tf.keras.models.load_model('saved_model_openpose_face2ear_v1.h5', compile=False)
pred = tf.keras.backend.function([model.input], [model.get_layer('s6_e').output, model.get_layer('s6').output])
if recording:
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(datetime.today().strftime("%Y%m%d%H%M%S")+'.avi', fourcc, 25.0, (o_size_w,o_size_h))
capture = cv2.VideoCapture(CAP_V4L2)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, IM_W)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, IM_H)
start = time.time() # 시작 시간 저장
# 정사각형 형태로 이미지 자르기 위함
cut_w_r = (IM_W - IM_H) // 2
cut_w_l = IM_W - cut_w_r
while cv2.waitKey(33) < 0:
ret, frame = capture.read()
# frame = cv2.resize(frame, (o_size_w, o_size_h), interpolation = cv2.INTER_AREA)
frame = frame[:,cut_w_r:cut_w_l,:]
frame = cv2.flip(frame, 1)
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, dsize=(input_size, input_size), interpolation=cv2.INTER_AREA)
result_all = pred([np.expand_dims(image, axis=0)/255.])
# ear-detect---------------------------------------------------------------
result = result_all[0]
result[result < ear_threshold] = 0 # threshold setting
result = tf.image.resize(result, [r_size, r_size])
result = apply_blur(result, landmark_size).numpy()
result = np.argmax(result.reshape(-1,landmark_size), axis=0)
prev_xy = [[],[],[],[]]
for i, idx in enumerate(result):
x, y = idx%r_size/r_size*o_size_w, idx//r_size/r_size*o_size_h
if x < 1 or y < 1 : continue
if i > 49: prev_xy[3].append([int(x),int(y)]); continue
if i > 34: prev_xy[2].append([int(x),int(y)]); continue
if i > 19: prev_xy[1].append([int(x),int(y)]); continue
prev_xy[0].append([int(x),int(y)])
for i, xy in enumerate(prev_xy):
if len(xy)==ear_part_num[i]:cv2.polylines(frame, [np.asarray(xy)], False , color_list[i], 2)
# -------------------------------------------------------------------------
# face-detect--------------------------------------------------------------
result = result_all[1]
result[result < face_threshold] = 0 # threshold setting
result = tf.image.resize(result, [r_size, r_size])
result = apply_blur(result, landmark_size_face).numpy()
result = np.argmax(result.reshape(-1,landmark_size_face), axis=0)
for i, idx in enumerate(result):
x, y = idx%r_size/r_size*o_size_w, idx//r_size/r_size*o_size_h
if x < 1 or y < 1 : continue
# if i in [0, 1, 2, 14, 15, 16]: cv2.circle(frame, (int(x), int(y)), 2, (0, 255, 0), -1)
# else: cv2.circle(frame, (int(x), int(y)), 2, (0, 0, 255), -1)
cv2.circle(frame, (int(x), int(y)), 2, (0, 0, 255), -1)
# -------------------------------------------------------------------------
if ret and recording:
if (time.time() - start) > recording_time: break
out.write(frame)
# guide line
# cv2.ellipse(frame, (o_size_w//2,o_size_h//2), (o_size_w//5,o_size_h//3), 0, 0, 360, (0, 255, 0), 1)
cv2.imshow("VFrame", frame)
capture.release()
if recording: out.release()
cv2.destroyAllWindows()