人臉實時情緒與性別識別

2021-08-09 13:15:26 字數 3403 閱讀 7277

最近弄乙個情緒識別與性別識別的東東。

opencv + keras

opencv用於人臉檢測

keras用於訓練出識別模型

資料集用於kaggle的(fer2013)

cnn進行訓練。

**如下:

import cv2

import sys

import json

import time

import numpy as np

from keras.models import model_from_json

from keras.models import load_model

gender_labels =

#cascpath = sys.argv[1]

facecascade = cv2.cascadeclassifier("haarcascade_frontalface_default.xml")

# 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)

gender_classifier = load_model('model/gender/******_cnn.81-0.96.hdf5')

# load weights into new model

model.load_weights('model.h5')

defoverlay_memeface

(probs):

emotion = emotion_labels[np.argmax(probs)]

return emotion

defpredict_emotion

(face_image_gray):

# a single cropped face

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

# cv2.imwrite(str(index)+'.png', resized_img)

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

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

defpredict_gender

(face_image_gray):

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

# cv2.imwrite(str(index)+'.png', resized_img)

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

gender_label_arg = np.argmax(gender_classifier.predict(image))

gender = gender_labels[gender_label_arg]

return gender

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)

#img = cv2.cvtcolor(frame, cv2.color_bgr2gray)

faces = facecascade.detectmultiscale(

img_gray,

scalefactor=1.1,

minneighbors=5,

minsize=(30, 30),

#flags=cv2.cv.cv_haar_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]

face_image = frame[y:y+h, x:x+w]

print(face_image_gray.shape)

#face = np.expand_dims(face_image_gray, 0)

#face = face / 255.0

#gender_label_arg = np.argmax(gender_classifier.predict(face))

# gender = gender_labels[gender_label_arg]

gender = predict_gender(face_image)

emotion = overlay_memeface(predict_emotion(face_image_gray))

print(predict_emotion(face_image_gray))

print(emotion)

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

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

cv2.puttext(frame, gender, (x, y - 30), cv2.font_hershey_******x, .7, (0, 255, 0), 1, cv2.line_aa)

cv2.puttext(frame, emotion, (x + 90, y - 30), cv2.font_hershey_******x, .7, (255, 0, 0), 1, cv2.line_aa)

# 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()

結果如下:

長得醜請別噴,,,,,,

QT Opencv實現人臉檢測與性別識別 3

seetaface開源人臉檢測框架實現人臉檢測,opencv dnn模組實現性別分類,qt做顯示介面,完成乙個課程設計。依賴庫 opencv3.1 包含dnn模組,qt5 過了好長時間,我又回來了 打算把程式打包一下發出來,我自己都有點忘記這個專案怎麼完成的了,也可能很亂。整體而言,完成專案可以分為...

實時人臉識別

我們將逐步學習如何使用picam實時識別人臉 這個專案是由這個出色的 開源計算機視覺庫 opencv完成的。在本教程中,我們將重點介紹raspberry pi 因此,raspbian為os 和python,但是我也在my mac上測試了 並且效果也不錯。opencv旨在提高計算效率,並且非常注重實時...

人臉性別識別文獻閱讀筆記(3)

21 基於人臉影象的性別識別方法研究 中文,期刊,2012年,知網 無22 人臉的性別分類 中文,期刊,2003年,知網 對傳統的pca fisher adaboost svm進行組合實驗,用於解決人臉性別識別問題。23 基於級聯支援向量機的人臉影象性別識別 中文,期刊,2012年,知網 對提出支援...