1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 人脸检测与情绪识别

人脸检测与情绪识别

时间:2019-11-21 16:15:12

相关推荐

人脸检测与情绪识别

基于开源face_recognition库做的人脸检测,调用的情绪识别模型由VGG训练保存的模型

只是简单的功能实现,速度有待提高

import face_recognitionimport cv2import numpy as npfrom keras.models import load_modeldef img_face_dectect(img):# img = face_recognition.load_image_file(img)face_locations = face_recognition.face_locations(img)# print(type(face_locations))# print(face_locations)# cv2.imshow(face_locations)for (top, right, bottom, left) in face_locations:# top *= 4# right *= 4# bottom *= 4# left *= 4# print(left,top,right,bottom)cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)return imgdef cap_face_dectect():cap = cv2.VideoCapture(0)while (True):ret, frame = cap.read()if ret == True:# known_image = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)img = frameface_locations = face_recognition.face_locations(img)for (top, right, bottom, left) in face_locations:# top *= 4# right *= 4# bottom *= 4# left *= 4# print(left,top,right,bottom)cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)face_image = img[top:bottom, left:right]face_image = cv2.resize(face_image, (48, 48))face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY)face_image = np.reshape(face_image, [1, face_image.shape[0], face_image.shape[1], 1])model = load_model("model_v6_23.hdf5")predicted_class = np.argmax(model.predict(face_image))label_map = dict((v, k) for k, v in emotion_dict.items())predicted_label = label_map[predicted_class]cv2.rectangle(frame, (left, top + 35), (right, top), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEX# cv2.putText(frame, predicted_label, label_position, cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)cv2.putText(frame, predicted_label, (left + 6, top + 18), font, 1.0, (255, 255, 255), 1)cv2.imshow("face_dectect", frame)# video.write(frame)if cv2.waitKey(1) & 0xFF == ord('q'):break# video.release()cap.release()cv2.destroyAllWindows()emotion_dict = {'Angry': 0, 'Sad': 5, 'Neutral': 4, 'Disgust': 1, 'Surprise': 6, 'Fear': 2, 'Happy': 3}cap_face_dectect()

hdf5模型下载:/priya-dwivedi/face_and_emotion_detection/tree/master/emotion_detector_models

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。