使用OpenCV简易实现HowOldRobot人脸轮廓识别功能

HowOldRobot 是微软推出的一个小服务,实现识别图片中的人脸以及预测图片中人的年龄,预测模型可能比较复杂,但是想做一个简单的人脸轮廓的识别还是比较简单的,下面记录使用Opencv工具包里的haarcascade进行识别的过程。

import matplotlib.pyplot as plt
%matplotlib inline
import sys, cv2
imagePath = '3jpg.jpg'
image = cv2.imread(imagePath)
# plt.imshow(image)
# plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
faceCascade = cv2.CascadeClassifier('F:BaiduYunDownloadopencvsourcesdatahaarcascadeshaarcascade_frontalface_default.xml')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# plt.imshow(gray)
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30,30),
    flags=cv2.CASCADE_SCALE_IMAGE)
print faces
[[401  26  91  91]
 [ 47  26 108 108]
 [238  44  60  60]]
import random
font = cv2.FONT_HERSHEY_SIMPLEX
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (14, 201, 255), 2)
    cv2.putText(image, str(random.randrange(20, 30)), (x+(w/2)-18, y-10), font, 1,(14, 201, 255), 2)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

OpenCV官方教程中文版(For Python) PDF  http://www.linuxidc.com/Linux/2015-08/121400.htm

Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm

[翻译]Ubuntu 14.04, 13.10 下安装 OpenCV 2.4.9  http://www.linuxidc.com/Linux/2014-12/110045.htm

OpenCV的详细介绍请点这里
OpenCV的下载地址请点这里