人臉面部表情識別 keras實現(二)

2021-09-10 19:12:48 字數 2465 閱讀 2613

import cv2

import sys

import json

import time

import numpy as np

from keras.models import model_from_json

cascpath = sys.ar**[1]

facecascade = cv2.cascadeclassifier(cascpath)

# load json and create model arch

json_file = open('model.json','r')

loaded_model_json = json_file.read()

json_file.close()

model = model_from_json(loaded_model_json)

# load weights into new model

model.load_weights('model.h5')

defpredict_emotion

(face_image_gray):

# a single cropped face

resized_img = cv2.resize(face_image_gray, (48,48), interpolation = cv2.inter_area)

image = resized_img.reshape(1, 1, 48, 48)

im = cv2.resize(resized_img,(90,100))

cv2.imwrite('face.bmp', im)

list_of_list = model.predict(image, batch_size=1, verbose=1)

video_capture = cv2.videocapture(0)

while

true:

# capture frame-by-frame

ret, frame = video_capture.read()

img_gray = cv2.cvtcolor(frame, cv2.color_bgr2gray,1)

faces = facecascade.detectmultiscale(

img_gray,

scalefactor=1.1,

minneighbors=1,

minsize=(30, 30),

flags=cv2.cascade_scale_image

)emotions =

# draw a rectangle around the faces

for (x, y, w, h) in faces:

face_image_gray = img_gray[y:y+h, x:x+w]

m = emotions.index(max(emotions))

biaoqing = ""

for index, val in enumerate(emotion_labels):

if (m == index):

biaoqing = val

print(biaoqing)

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

#cv2.puttext(frame, biaoqing, (x, y),

# cv2.font_hershey_plain, 1, (0, 255, 0),

# thickness=2, linetype=2)

with open('emotion.txt', 'a') as f:

# display the resulting frame

cv2.imshow('video', frame)

if cv2.waitkey(1) & 0xff == ord('q'):

break

# when everything is done, release the capture

video_capture.release()

cv2.destroyallwindows()

執行命令列 python real-time.py haarcascade_frontalface_default.xml

載入haarcascade_frontalface_default.xml是haar分類器,用於檢測人臉,別人訓練好的

載入model.h5模型,這是用caffe訓練的,用於表情**

model.json經過檢視裡面內容,就是別人將model通過keras儲存為json格式,現在重新將json載入

emotion.txt裡面是結果,哪個值越大就是哪個。這僅僅是**,但效果還是比(一)好很多

人臉識別面部表情

import dlib 人臉識別的庫dlib import numpy as np 資料處理的庫numpy import cv2 影象處理的庫opencv class face emotion def init self 使用特徵提取器get frontal face detector self.d...

人臉表情識別

首先我們來了解一下表情識別的相關背景知識以及發展近況。人臉表情是最直接 最有效的情感識別模式。它有很多人機互動方面的應用,例如疲勞駕駛檢測和手機端實時表情識別。早在20世紀ekman等專家就通過跨文化調研提出了七類基礎表情,分別是生氣,害怕,厭惡,開心,悲傷,驚訝以及中立。然而不斷的研究發現這七類基...

輕量化人臉表情識別

1 引言 1.1應用價值 人臉表情識別在人機互動 教育 安防 輔助駕駛 廣告等領域均有廣泛應用,是乙個很有發展前景的方向。目前,僅針對人臉識別的技術相對成熟,表情識別還有很大市場,接下來需要做的是將表情識別運用到實際場景中,將其與現實需求進行良好結合。例如在遊戲製作上面,可以根據人類情感做出實時反映...