Skip to content

Commit

Permalink
性别识别代码添加
Browse files Browse the repository at this point in the history
  • Loading branch information
vipstone committed May 6, 2018
1 parent 6ec229f commit 2c67ca6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@

人脸检测:face_recognition

视频识别:OpenCV + face_recognition
性别识别:keras + tensorflow

文字识别:Tesseract OCR



## 教程文档 ##

[OpenCV环境搭建](doc/settingup.md)

[OpenCV添加中文](doc/chinese.md)

[Tesseract OCR文字识别](doc/tesseractOCR.md)

[图片人脸检测(OpenCV版)](doc/detectionOpenCV.md)
Expand All @@ -54,10 +53,14 @@

[头像特效合成](doc/compose.md)

性别识别

## 其他相关 ##

[pip/pip3更换国内源](doc/pipChange.md)

[OpenCV添加中文](doc/chinese.md)


## 功能预览 ##

Expand Down
10 changes: 5 additions & 5 deletions faceai/ChineseText.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy
from PIL import Image, ImageDraw, ImageFont

img = cv2.imread("img/xingye-1.png")
# img = cv2.imread("img/xingye-1.png")


def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
Expand All @@ -18,8 +18,8 @@ def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)


img = cv2ImgAddText(img, "大家好,我是星爷", 140, 60, (255, 255, 0), 20)
# img = cv2ImgAddText(img, "大家好,我是星爷", 140, 60, (255, 255, 0), 20)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cv2.imshow("Image", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
Binary file not shown.
Binary file not shown.
34 changes: 34 additions & 0 deletions faceai/gender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#coding=utf-8
#表情识别

import cv2
from keras.models import load_model
import numpy as np
import ChineseText

img = cv2.imread("img/gather.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=(140, 140))

gender_classifier = load_model(
"classifier/gender_models/simple_CNN.81-0.96.hdf5")
gender_labels = {0: '女', 1: '男'}
color = (255, 255, 255)

for (x, y, w, h) in faces:
face = img[(y - 60):(y + h + 60), (x - 30):(x + w + 30)]
face = cv2.resize(face, (48, 48))
face = np.expand_dims(face, 0)
face = face / 255.0
gender_label_arg = np.argmax(gender_classifier.predict(face))
gender = gender_labels[gender_label_arg]
cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
img = ChineseText.cv2ImgAddText(img, gender, x + h, y, color, 30)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Binary file added faceai/img/gather.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2c67ca6

Please sign in to comment.