video4.py
4.2 KB
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
import dlib
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import math
import os
import pathlib
import time
import pandas as pd
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.models import load_model
from tensorflow.keras import regularizers
from tensorflow import keras
import time
def convertMillis(millis):
seconds = (millis/1000) % 60
minutes = (millis/(1000*60)) % 60
hours = (millis/(1000*60*60)) % 24
return seconds, int(minutes), int(hours)
def videoDetector(second, video_name):
# face & emotion detection model load
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(
'face_emotion_recognition/models/shape_predictor_68_face_landmarks.dat')
facerec = dlib.face_recognition_model_v1(
'face_emotion_recognition/models/dlib_face_recognition_resnet_model_v1.dat')
model = load_model(
'checkpoint/er-best-mobilenet1-bt32-model-classweight-adam.h5')
# face & emotion detection time dict
descs = np.load('static/img/descs.npy', allow_pickle=True)[()]
labels_dict_ = {0: 'angry', 1: 'fear', 2: 'happy',
3: 'neutral', 4: 'sad', 5: 'surprise'}
face_emotion_dict = {}
for name, saved_desc in descs.items():
face_emotion_dict[name] = {'angry': [], 'fear': [
], 'happy': [], 'neutral': [], 'sad': [], 'surprise': []}
# video 정보 불러오기
video_path = 'static/video/' + video_name
cap = cv2.VideoCapture(video_path)
# 동영상 크기(frame정보)를 읽어옴
fps = cap.get(cv2.CAP_PROP_FPS)
multiplier = fps * second
frameCount = 0
ret = 1
while ret:
frameId = int(round(cap.get(1))) # 현재 프레임 번호 가져오기
ret, frameBGR = cap.read() # 영상을 한 frame씩 읽어오기
if(type(frameBGR) == type(None)):
pass
else:
frame = cv2.cvtColor(frameBGR, cv2.COLOR_BGR2RGB)
if (ret is True) and (frameId % multiplier < 1):
faces = detector(frame, 1)
for (i, face) in enumerate(faces):
try:
shape = predictor(frame, face)
face_descriptor = facerec.compute_face_descriptor(
frame, shape)
img = cv2.resize(frame[face.top():face.bottom(), face.left(
):face.right()], dsize=(224, 224), interpolation=cv2.INTER_CUBIC)
imgarr = np.array(img).reshape(1, 224, 224, 3) / 255
emotion = labels_dict_[
model.predict(imgarr).argmax(axis=-1)[0]]
last_found = {'name': 'unknown',
'dist': 0.6, 'color': (0, 0, 255)}
for name, saved_desc in descs.items():
dist = np.linalg.norm(
[face_descriptor] - saved_desc, axis=1)
if dist < last_found['dist']:
last_found = {
'name': name, 'dist': dist, 'color': (255, 255, 255)}
cv2.rectangle(frameBGR, pt1=(face.left(), face.top()), pt2=(
face.right(), face.bottom()), color=last_found['color'], thickness=2)
cv2.putText(frameBGR, last_found['name'] + ',' + emotion, org=(face.left(), face.top(
)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=last_found['color'], thickness=2)
con_sec, con_min, con_hour = convertMillis(
cap.get(cv2.CAP_PROP_POS_MSEC))
face_emotion_dict[last_found['name']][emotion].append(
"{0}:{1}:{2}".format(con_hour, con_min, round(con_sec, 3)))
print("{0}:{1}:{2} {3}".format(
con_hour, con_min, round(con_sec, 3), emotion))
except Exception as e:
print(str(e))
frameCount += 1
print(face_emotion_dict)
return face_emotion_dict