-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagegenderemotion_webcam_flask.py
137 lines (108 loc) · 5.17 KB
/
agegenderemotion_webcam_flask.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
import sys
import argparse
import cv2
from libfaceid.detector import FaceDetectorModels, FaceDetector
from libfaceid.encoder import FaceEncoderModels, FaceEncoder
from libfaceid.pose import FacePoseEstimatorModels, FacePoseEstimator
from libfaceid.age import FaceAgeEstimatorModels, FaceAgeEstimator
from libfaceid.gender import FaceGenderEstimatorModels, FaceGenderEstimator
from libfaceid.emotion import FaceEmotionEstimatorModels, FaceEmotionEstimator
import pandas as pd
import time
from datetime import datetime
from flask import Flask, request, redirect, url_for, render_template,Response,jsonify
# Use flask for web app
from flask import Flask, render_template, Response
app = Flask(__name__)
# Set the input directories
INPUT_DIR_DATASET = "datasets"
INPUT_DIR_MODEL_DETECTION = "models/detection/"
INPUT_DIR_MODEL_ENCODING = "models/encoding/"
INPUT_DIR_MODEL_TRAINING = "models/training/"
INPUT_DIR_MODEL_ESTIMATION = "models/estimation/"
# Set width and height
RESOLUTION_QVGA = (320, 240)
RESOLUTION_VGA = (640, 480)
RESOLUTION_HD = (1280, 720)
RESOLUTION_FULLHD = (1920, 1080)
t0 = time.time()
def cam_init(cam_index, width, height):
cap = cv2.VideoCapture(cam_index)
if sys.version_info < (3, 0):
cap.set(cv2.cv.CV_CAP_PROP_FPS, 30)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, height)
else:
cap.set(cv2.CAP_PROP_FPS, 30)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
return cap
def label_face(frame, face_rect, face_id, confidence):
(x, y, w, h) = face_rect
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 1)
if face_id is not None:
cv2.putText(frame, "{} {:.2f}%".format(face_id, confidence),
(x+5,y+h-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
def process_facedetection1():
cam_index = 0
cam_resolution = RESOLUTION_VGA
model_detector=FaceDetectorModels.HAARCASCADE
# model_detector=FaceDetectorModels.DLIBHOG
# model_detector=FaceDetectorModels.DLIBCNN
# model_detector=FaceDetectorModels.SSDRESNET
# model_detector=FaceDetectorModels.MTCNN
# model_detector=FaceDetectorModels.FACENET
model_poseestimator=FacePoseEstimatorModels.DEFAULT
model_ageestimator=FaceAgeEstimatorModels.DEFAULT
model_genderestimator=FaceGenderEstimatorModels.DEFAULT
model_emotionestimator=FaceEmotionEstimatorModels.DEFAULT
# Initialize the camera
camera = cam_init(cam_index, cam_resolution[0], cam_resolution[1])
try:
# Initialize face detection
face_detector = FaceDetector(model=model_detector, path=INPUT_DIR_MODEL_DETECTION)#, optimize=True)
# Initialize face pose/age/gender estimation
face_pose_estimator = FacePoseEstimator(model=model_poseestimator, path=INPUT_DIR_MODEL_ESTIMATION)
face_age_estimator = FaceAgeEstimator(model=model_ageestimator, path=INPUT_DIR_MODEL_ESTIMATION)
face_gender_estimator = FaceGenderEstimator(model=model_genderestimator, path=INPUT_DIR_MODEL_ESTIMATION)
face_emotion_estimator = FaceEmotionEstimator(model=model_emotionestimator, path=INPUT_DIR_MODEL_ESTIMATION)
except:
print("Warning, check if models and trained dataset models exists!")
(age, gender, emotion) = (None, None, None)
df = pd.DataFrame(columns=['Gender'])
while (True):
# Capture frame from webcam
ret, frame = camera.read()
t1 = time.time() # current time
num_seconds = t1 - t0 # diff
if frame is None:
print("Error, check if camera is connected!")
break
# Detect and identify faces in the frame
faces = face_detector.detect(frame)
for (index, face) in enumerate(faces):
(x, y, w, h) = face
# Detect age, gender, emotion
face_image = frame[y:y+h, h:h+w]
age = face_age_estimator.estimate(frame, face_image)
gender = face_gender_estimator.estimate(frame, face_image)
emotion = face_emotion_estimator.estimate(frame, face_image)
# Detect and draw face pose locations
shape = face_pose_estimator.detect(frame, face)
face_pose_estimator.add_overlay(frame, shape)
# Display age, gender, emotion
cv2.putText(frame, "Age: {}".format(age),
(x, y-45), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frame, "Gender: {}".format(gender),
(x, y-30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frame, "Emotion: {}".format(emotion),
(x, y-15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
df = df.append({'Gender':gender}, ignore_index=True)
df1 = df.groupby('Gender').size()
g = df1.values.tolist()
#print(dfg)
# Display updated frame to web app
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', frame)[1].tobytes() + b'\r\n\r\n')
# Release the camera
camera.release()
cv2.destroyAllWindows()