资源简介
代码基于python3和opencv框架,可能需要安装所需的module;
功能描述
--实现笔记本摄像头获取人脸的面部表情识别,happy,angry,neural,sad..
--实现指定路径下视频中人脸的识别..
验证成功,未做改动,源自github
代码片段和文件信息
import cv2
import numpy as np
from keras.models import load_model
from statistics import mode
from utils.datasets import get_labels
from utils.inference import detect_faces
from utils.inference import draw_text
from utils.inference import draw_bounding_box
from utils.inference import apply_offsets
from utils.inference import load_detection_model
from utils.preprocessor import preprocess_input
USE_WEBCAM = True # If false loads video file source
# parameters for loading data and images
emotion_model_path = ‘./models/emotion_model.hdf5‘
emotion_labels = get_labels(‘fer2013‘)
# hyper-parameters for bounding boxes shape
frame_window = 10
emotion_offsets = (20 40)
# loading models
face_cascade = cv2.CascadeClassifier(‘./models/haarcascade_frontalface_default.xml‘)
emotion_classifier = load_model(emotion_model_path)
# getting input model shapes for inference
emotion_target_size = emotion_classifier.input_shape[1:3]
# starting lists for calculating modes
emotion_window = []
# starting video streaming
cv2.namedWindow(‘window_frame‘)
video_capture = cv2.VideoCapture(0)
# Select video or webcam feed
cap = None
if (USE_WEBCAM == True):
cap = cv2.VideoCapture(0) # Webcam source
else:
cap = cv2.VideoCapture(‘./demo/dinner.mp4‘) # Video file source
while cap.isOpened(): # True:
ret bgr_image = cap.read()
#bgr_image = video_capture.read()[1]
gray_image = cv2.cvtColor(bgr_image cv2.COLOR_BGR2GRAY)
rgb_image = cv2.cvtColor(bgr_image cv2.COLOR_BGR2RGB)
faces = face_cascade.detectMultiScale(gray_image scaleFactor=1.1 minNeighbors=5
minSize=(30 30) flags=cv2.CASCADE_SCALE_IMAGE)
for face_coordinates in faces:
x1 x2 y1 y2 = apply_offsets(face_coordinates emotion_offsets)
gray_face = gray_image[y1:y2 x1:x2]
try:
gray_face = cv2.resize(gray_face (emotion_target_size))
except:
continue
gray_face = preprocess_input(gray_face True)
gray_face = np.expand_dims(gray_face 0)
gray_face = np.expand_dims(gray_face -1)
emotion_prediction = emotion_classifier.predict(gray_face)
emotion_probability = np.max(emotion_prediction)
emotion_label_arg = np.argmax(emotion_prediction)
emotion_text = emotion_labels[emotion_label_arg]
emotion_window.append(emotion_text)
if len(emotion_window) > frame_window:
emotion_window.pop(0)
try:
emotion_mode = mode(emotion_window)
except:
continue
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_probab
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-01-17 00:38 Emotion-master\
文件 1068 2018-01-17 00:38 Emotion-master\LICENSE
文件 881 2018-01-17 00:38 Emotion-master\README.md
目录 0 2018-01-17 00:38 Emotion-master\demo\
文件 5408300 2018-01-17 00:38 Emotion-master\demo\demo.gif
文件 2613425 2018-01-17 00:38 Emotion-master\demo\dinner.mp4
文件 980831 2018-01-17 00:38 Emotion-master\demo\report.pdf
文件 3458 2018-01-17 00:38 Emotion-master\emotions.py
目录 0 2018-01-17 00:38 Emotion-master\models\
文件 872856 2018-01-17 00:38 Emotion-master\models\emotion_model.hdf5
文件 930127 2018-01-17 00:38 Emotion-master\models\haarcascade_frontalface_default.xm
目录 0 2018-01-17 00:38 Emotion-master\utils\
文件 0 2018-01-17 00:38 Emotion-master\utils\__init__.py
文件 149 2018-01-17 00:38 Emotion-master\utils\__init__.pyc
目录 0 2018-01-17 00:38 Emotion-master\utils\__pycache__\
文件 145 2018-01-17 00:38 Emotion-master\utils\__pycache__\__init__.cpython-36.pyc
文件 5076 2018-01-17 00:38 Emotion-master\utils\__pycache__\datasets.cpython-36.pyc
文件 1778 2018-01-17 00:38 Emotion-master\utils\__pycache__\inference.cpython-36.pyc
文件 966 2018-01-17 00:38 Emotion-master\utils\__pycache__\preprocessor.cpython-36.pyc
文件 10257 2018-01-17 00:38 Emotion-master\utils\data_augmentation.py
文件 5855 2018-01-17 00:38 Emotion-master\utils\datasets.py
文件 6495 2018-01-17 00:38 Emotion-master\utils\datasets.pyc
文件 6693 2018-01-17 00:38 Emotion-master\utils\grad_cam.py
文件 1368 2018-01-17 00:38 Emotion-master\utils\inference.py
文件 2509 2018-01-17 00:38 Emotion-master\utils\inference.pyc
文件 642 2018-01-17 00:38 Emotion-master\utils\preprocessor.py
文件 1375 2018-01-17 00:38 Emotion-master\utils\preprocessor.pyc
文件 6368 2018-01-17 00:38 Emotion-master\utils\visualizer.py
- 上一篇:Python程序设计与算法基础教程
- 下一篇:Python DBC LIB
评论
共有 条评论