-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_functions.py
224 lines (199 loc) · 8.57 KB
/
detect_functions.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
215
216
217
218
219
220
221
222
223
224
import os
import cv2
import numpy as np
import json
from utils.inference import draw_text
class FrameEmotion:
def __init__(self, time, prediction):
self.time = time
self.prediction = prediction
# 根据固定时长抽帧并分析
def get_emotion_stream(video_path, detector, frame_interval_ms):
video_capture = cv2.VideoCapture(video_path)
if not video_capture.isOpened():
return []
fps = video_capture.get(cv2.CAP_PROP_FPS)
frame_count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
emotion_stream = []
frame_no = 1 # 抽取帧的序号
interval_frame_num = int(frame_interval_ms / 1000 * fps) # 间隔帧数
if interval_frame_num < 1:
interval_frame_num = 1 # 防止帧间隔为0的情况
while frame_no <= frame_count:
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
frame = video_capture.read()[1]
if frame is None or np.size(frame) is 0:
frame_no += interval_frame_num
continue
time = int(video_capture.get(cv2.CAP_PROP_POS_MSEC))
prediction, _ = detector.detect_biggest(frame)
if prediction is None:
frame_no += interval_frame_num
continue
frame_emotion = FrameEmotion(time, prediction)
emotion_stream.append(frame_emotion)
frame_no += interval_frame_num
video_capture.release()
return emotion_stream
# 只分析指定范围内的视频
def get_emotion_stream_cut(video_path, detector, frame_interval_ms, start_ms, end_ms):
video_capture = cv2.VideoCapture(video_path)
if not video_capture.isOpened():
return []
fps = video_capture.get(cv2.CAP_PROP_FPS)
frame_count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
start_frame_no = int(start_ms / 1000 * fps + 1)
end_frame_no = int(end_ms / 1000 * fps)
emotion_stream = []
if start_frame_no < 0 or end_frame_no > frame_count:
return emotion_stream
frame_no = start_frame_no # 抽取帧的序号
interval_frame_num = int(frame_interval_ms / 1000 * fps) # 间隔帧数
if interval_frame_num < 1:
interval_frame_num = 1 # 防止帧间隔为0的情况
while frame_no <= end_frame_no:
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
frame = video_capture.read()[1]
if frame is None or np.size(frame) is 0:
frame_no += interval_frame_num
continue
time = int(video_capture.get(cv2.CAP_PROP_POS_MSEC))
prediction = detector.detect_biggest(frame)
frame_emotion = FrameEmotion(time, prediction)
emotion_stream.append(frame_emotion)
frame_no += interval_frame_num
video_capture.release()
return emotion_stream
def get_image_emotion(image, detector):
return detector.detect_biggest(image)
def save_biggest_emotion_images(video_path, save_path, detector, frame_interval_ms):
video_capture = cv2.VideoCapture(video_path)
if not video_capture.isOpened():
return
fps = video_capture.get(cv2.CAP_PROP_FPS)
frame_count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
# 创建视频存储文件夹
if not os.path.exists(save_path):
os.mkdir(save_path)
frame_no = 1 # 抽取帧的序号
interval_frame_num = int(frame_interval_ms / 1000 * fps) # 间隔帧数
if interval_frame_num < 1:
interval_frame_num = 1 # 防止帧间隔为0的情况
while frame_no <= frame_count:
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
frame = video_capture.read()[1]
if frame is None or np.size(frame) is 0:
frame_no += interval_frame_num
continue
time = int(video_capture.get(cv2.CAP_PROP_POS_MSEC))
prediction, coord = detector.detect_biggest(frame)
if prediction is None:
frame_no += interval_frame_num
continue
emotion_probability = np.max(prediction)
frame_no += interval_frame_num
emotion_label_arg = np.argmax(prediction)
emotion_text = detector.labels[emotion_label_arg]
# 根据情绪选择颜色
if emotion_text == 'angry':
color = emotion_probability * np.asarray((255, 0, 0))
elif emotion_text == 'sad':
color = emotion_probability * np.asarray((0, 0, 255))
elif emotion_text == 'happy':
color = emotion_probability * np.asarray((255, 255, 0))
elif emotion_text == 'surprise':
color = emotion_probability * np.asarray((0, 255, 255))
else:
color = emotion_probability * np.asarray((0, 255, 0))
color = color.astype(int)
color = color.tolist()
text = emotion_text + ' ' + str(time / 1000) + 's'
cv2.rectangle(frame, (coord[0], coord[2]), (coord[1], coord[3]), color, 2)
draw_text(coord, frame, text,
color, 0, -45, 1, 1)
image_path = save_path + '/' + str(frame_no) + '.png'
cv2.imwrite(image_path, frame)
video_capture.release()
return
def save_biggest_emotion_images_cut(video_path, save_path, detector, frame_interval_ms, start_ms, end_ms):
video_capture = cv2.VideoCapture(video_path)
if not video_capture.isOpened():
return
fps = video_capture.get(cv2.CAP_PROP_FPS)
start_frame_no = int(start_ms / 1000 * fps + 1)
end_frame_no = int(end_ms / 1000 * fps)
frame_count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
if start_frame_no < 0 or end_frame_no > frame_count:
return
# 创建视频存储文件夹
if not os.path.exists(save_path):
os.mkdir(save_path)
frame_no = 1 # 抽取帧的序号
interval_frame_num = int(frame_interval_ms / 1000 * fps) # 间隔帧数
if interval_frame_num < 1:
interval_frame_num = 1 # 防止帧间隔为0的情况
while frame_no <= end_frame_no:
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
frame = video_capture.read()[1]
if frame is None or np.size(frame) is 0:
frame_no += interval_frame_num
continue
time = int(video_capture.get(cv2.CAP_PROP_POS_MSEC))
prediction, coord = detector.detect_biggest(frame)
if prediction is None:
frame_no += interval_frame_num
continue
emotion_probability = np.max(prediction)
frame_no += interval_frame_num
emotion_label_arg = np.argmax(prediction)
emotion_text = detector.labels[emotion_label_arg]
# 根据情绪选择颜色
if emotion_text == 'angry':
color = emotion_probability * np.asarray((255, 0, 0))
elif emotion_text == 'sad':
color = emotion_probability * np.asarray((0, 0, 255))
elif emotion_text == 'happy':
color = emotion_probability * np.asarray((255, 255, 0))
elif emotion_text == 'surprise':
color = emotion_probability * np.asarray((0, 255, 255))
else:
color = emotion_probability * np.asarray((0, 255, 0))
color = color.astype(int)
color = color.tolist()
text = emotion_text + ' ' + str(time / 1000) + 's'
cv2.rectangle(frame, (coord[0], coord[2]), (coord[1], coord[3]), color, 2)
draw_text(coord, frame, text,
color, 0, -45, 1, 1)
image_path = save_path + '/' + str(frame_no) + '.png'
cv2.imwrite(image_path, frame)
video_capture.release()
return
# 根据固定时长抽帧并分析,返回为json格式
def get_emotion_stream_json(video_path, detector, frame_interval_ms):
video_capture = cv2.VideoCapture(video_path)
if not video_capture.isOpened():
return []
fps = video_capture.get(cv2.CAP_PROP_FPS)
frame_count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
emotion_stream = []
frame_no = 1 # 抽取帧的序号
interval_frame_num = int(frame_interval_ms / 1000 * fps) # 间隔帧数
if interval_frame_num < 1:
interval_frame_num = 1 # 防止帧间隔为0的情况
while frame_no <= frame_count:
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
frame = video_capture.read()[1]
if frame is None or np.size(frame) is 0:
frame_no += interval_frame_num
continue
time = int(video_capture.get(cv2.CAP_PROP_POS_MSEC))
prediction, _ = detector.detect_biggest(frame)
if prediction is None:
frame_no += interval_frame_num
continue
frame_emotion = FrameEmotion(time, prediction.tolist())
emotion_stream.append(frame_emotion.__dict__)
frame_no += interval_frame_num
video_capture.release()
emotion_stream_json = json.dumps(emotion_stream)
return emotion_stream_json