python 實現(xiàn)表情識別
表情識別
表情識別支持7種表情類型,生氣、厭惡、恐懼、開心、難過、驚喜、平靜等。
實現(xiàn)思路
使用OpenCV識別圖片中的臉,在使用keras進(jìn)行表情識別。
效果預(yù)覽

實現(xiàn)代碼
與《性別識別》相似,本文表情識別也是使用keras實現(xiàn)的,和性別識別相同,型數(shù)據(jù)使用的是oarriaga/face_classification的,代碼如下:
#coding=utf-8
#表情識別
import cv2
from keras.models import load_model
import numpy as np
import chineseText
import datetime
startTime = datetime.datetime.now()
emotion_classifier = load_model(
'classifier/emotion_models/simple_CNN.530-0.65.hdf5')
endTime = datetime.datetime.now()
print(endTime - startTime)
emotion_labels = {
0: '生氣',
1: '厭惡',
2: '恐懼',
3: '開心',
4: '難過',
5: '驚喜',
6: '平靜'
}
img = cv2.imread("img/emotion/emotion.png")
face_classifier = cv2.CascadeClassifier(
"C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(
gray, scaleFactor=1.2, minNeighbors=3, minSize=(40, 40))
color = (255, 0, 0)
for (x, y, w, h) in faces:
gray_face = gray[(y):(y + h), (x):(x + w)]
gray_face = cv2.resize(gray_face, (48, 48))
gray_face = gray_face / 255.0
gray_face = np.expand_dims(gray_face, 0)
gray_face = np.expand_dims(gray_face, -1)
emotion_label_arg = np.argmax(emotion_classifier.predict(gray_face))
emotion = emotion_labels[emotion_label_arg]
cv2.rectangle(img, (x + 10, y + 10), (x + h - 10, y + w - 10),
(255, 255, 255), 2)
img = chineseText.cv2ImgAddText(img, emotion, x + h * 0.3, y, color, 20)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
以上就是python 實現(xiàn)表情識別的詳細(xì)內(nèi)容,更多關(guān)于python 表情識別的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實現(xiàn)合并同一個文件夾下所有PDF文件的方法示例
這篇文章主要介紹了Python實現(xiàn)合并同一個文件夾下所有PDF文件的方法,涉及Python針對pdf文件的讀取、判斷、解密、寫入合并等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
Python使用pickle進(jìn)行序列化和反序列化的示例代碼
這篇文章主要介紹了Python使用pickle進(jìn)行序列化和反序列化,幫助大家更好的理解和使用python的pickle庫,感興趣的朋友可以了解下2020-09-09
Python正則表達(dá)式re.compile()和re.findall()詳解
re?模塊提供了不少有用的函數(shù),用以匹配字符串,下面這篇文章主要給大家介紹了關(guān)于Python正則表達(dá)式re.compile()和re.findall()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
python矩陣轉(zhuǎn)換為一維數(shù)組的實例
今天小編就為大家分享一篇python矩陣轉(zhuǎn)換為一維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
ROS1?rosbag的詳細(xì)使用并且使用python合并bag包的方法
這篇文章主要介紹了ROS1?rosbag的詳細(xì)使用,并且使用python來合并bag包,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05

