[Face++]Face初探——人脸检测
经过了强烈的思想斗争才把自己拖到图书馆做毕设T^T
anyway, 因为毕设里面有人脸识别的部分,所以就想找个现成的api先玩玩,于是就找到最近很火的face++:http://www.faceplusplus.com.cn/
接口什么的还是很简单的,主要就是看它的api开发文档,最终实现把demo中的hello.py改造之后能够上传本地的三张图片进行训练,然后对新的一幅图片进行识别,看这幅图片中的人脸是三张图片中的哪一张,对于我的毕设而言,这个功能其实就足够了。修改后的hello.py如下:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# $File: hello.py # In this tutorial, you will learn how to call Face ++ APIs and implement a
# simple App which could recognize a face image in 3 candidates.
# 在本教程中,您将了解到Face ++ API的基本调用方法,并实现一个简单的App,用以在3
# 张备选人脸图片中识别一个新的人脸图片。 # You need to register your App first, and enter you API key/secret.
# 您需要先注册一个App,并将得到的API key和API secret写在这里。
API_KEY = '********'
API_SECRET = '*********' # Import system libraries and define helper functions
# 导入系统库并定义辅助函数
import time
from pprint import pformat
def print_result(hint, result):
def encode(obj):
if type(obj) is unicode:
return obj.encode('utf-8')
if type(obj) is dict:
return {encode(k): encode(v) for (k, v) in obj.iteritems()}
if type(obj) is list:
return [encode(i) for i in obj]
return obj
print hint
result = encode(result)
print '\n'.join([' ' + i for i in pformat(result, width = 75).split('\n')]) # First import the API class from the SDK
# 首先,导入SDK中的API类
from facepp import API
from facepp import File api = API(API_KEY, API_SECRET) # Here are the person names and their face images
# 人名及其脸部图片
PERSONS = [
('Yanzi Sun', './syz.jpeg'),
('Qiaoen Chan', './cqe.jpeg'),
('Jackie Chan', './jk.jpeg')
]
TARGET_IMAGE = './cl.jpg' # Step 1: Create a group to add these persons in
# 步骤1: 新建一个group用以添加person
api.group.create(group_name = 'forfun') # Step 2: Detect faces from those three images and add them to the persons
# 步骤2:从三种图片中检测人脸并将其加入person中。
for (name, path) in PERSONS:
result = api.detection.detect(img = File(path))
print_result('Detection result for {}:'.format(name), result) face_id = result['face'][0]['face_id'] # Create a person in the group, and add the face to the person
# 在该group中新建一个person,并将face加入期中
api.person.create(person_name = name, group_name = 'forfun',
face_id = face_id) # Step 3: Train the group.
# Note: this step is required before performing recognition in this group,
# since our system needs to pre-compute models for these persons
# 步骤3:训练这个group
# 注:在group中进行识别之前必须执行该步骤,以便我们的系统能为这些person建模
result = api.recognition.train(group_name = 'forfun', type = 'all') # Because the train process is time-consuming, the operation is done
# asynchronously, so only a session ID would be returned.
# 由于训练过程比较耗时,所以操作必须异步完成,因此只有session ID会被返回
print_result('Train result:', result) session_id = result['session_id'] # Now, wait before train completes
# 等待训练完成
while True:
result = api.info.get_session(session_id = session_id)
if result['status'] == u'SUCC':
print_result('Async train result:', result)
break
time.sleep(1) #也可以通过调用api.wait_async(session_id)函数完成以上功能 # Step 4: recognize the unknown face image
# 步骤4:识别未知脸部图片
result = api.recognition.recognize(img = File(TARGET_IMAGE), group_name = 'forfun')
print_result('Recognize result:', result)
print '=' * 60
print 'The person with highest confidence:', \
result['face'][0]['candidate'][0]['person_name'] # Finally, delete the persons and group because they are no longer needed
# 最终,删除无用的person和group
api.group.delete(group_name = 'forfun')
api.person.delete(person_name = [i[0] for i in PERSONS]) # Congratulations! You have finished this tutorial, and you can continue
# reading our API document and start writing your own App using Face++ API!
# Enjoy :)
# 恭喜!您已经完成了本教程,可以继续阅读我们的API文档并利用Face++ API开始写您自
# 己的App了!
# 旅途愉快 :)
要注意的就是35行,因为原来demo里面的图像是通过url获取的,而这里需要从本地上传,所以就要用到facepp.py里面定义的File类。另外注意12,13行的API_KEY和API_SECRET是通过在网站注册得到的。
其它改动的地方就是图片的路径,剩下的都是原来demo中的代码了。最终的结果如下:
Detection result for Yanzi Sun:
{'face': [{'attribute': {'age': {'range': 5, 'value': 30},
'gender': {'confidence': 99.9991,
'value': 'Female'},
'race': {'confidence': 80.13329999999999,
'value': 'Asian'},
'smiling': {'value': 99.3116}},
'face_id': 'f2790efd530b569cdc505cc2465da34f',
'position': {'center': {'x': 52.57732, 'y': 41.923077},
'eye_left': {'x': 42.224794, 'y': 36.929538},
'eye_right': {'x': 62.156701, 'y': 35.701385},
'height': 27.692308,
'mouth_left': {'x': 42.051031, 'y': 49.590385},
'mouth_right': {'x': 63.552577,
'y': 49.841154},
'nose': {'x': 53.861856, 'y': 46.203462},
'width': 37.113402},
'tag': ''}],
'img_height': 260,
'img_id': '09c7c2d49eb98dc2e90340ef2a6c9531',
'img_width': 194,
'session_id': '3a47b91a118d4c7cae9dcaf5ba61eec5',
'url': None}
Detection result for Qiaoen Chan:
{'face': [{'attribute': {'age': {'range': 6, 'value': 15},
'gender': {'confidence': 99.9974,
'value': 'Female'},
'race': {'confidence': 98.2572,
'value': 'Asian'},
'smiling': {'value': 2.82502}},
'face_id': '805b397a72899eda36be3f1dfed73451',
'position': {'center': {'x': 32.0, 'y': 47.02381},
'eye_left': {'x': 26.078067, 'y': 39.870476},
'eye_right': {'x': 36.355667, 'y': 39.246726},
'height': 38.095238,
'mouth_left': {'x': 28.270367, 'y': 59.064881},
'mouth_right': {'x': 34.999667,
'y': 59.091369},
'nose': {'x': 30.3052, 'y': 49.743036},
'width': 21.333333},
'tag': ''}],
'img_height': 168,
'img_id': 'ebee384c2b96399c3f52565682e4c249',
'img_width': 300,
'session_id': '5c1623ef71944c11a0efc6b4a698b3b0',
'url': None}
Detection result for Jackie Chan:
{'face': [{'attribute': {'age': {'range': 10, 'value': 50},
'gender': {'confidence': 99.9967,
'value': 'Male'},
'race': {'confidence': 76.5193,
'value': 'Asian'},
'smiling': {'value': 96.2044}},
'face_id': 'f164cc74a49e3d6766c8733ebdfe616d',
'position': {'center': {'x': 50.166667, 'y': 37.202381},
'eye_left': {'x': 45.798, 'y': 32.12381},
'eye_right': {'x': 53.721333, 'y': 30.344464},
'height': 31.547619,
'mouth_left': {'x': 46.665333, 'y': 46.910298},
'mouth_right': {'x': 54.770667,
'y': 45.298393},
'nose': {'x': 49.889667, 'y': 39.642143},
'width': 17.666667},
'tag': ''}],
'img_height': 168,
'img_id': 'd2ef3d2bd1d907fa15130f505300226e',
'img_width': 300,
'session_id': 'c7d498450b28453f8f90135ca92a327c',
'url': None}
Train result:
{'session_id': '041678d25ac94c2689396d0e6a660302'}
Async train result:
{'create_time': 1438667400,
'finish_time': 1438667400,
'result': {'success': True},
'session_id': '041678d25ac94c2689396d0e6a660302',
'status': 'SUCC'}
Recognize result:
{'face': [{'candidate': [{'confidence': 10.85891,
'person_id': '476ec2d1e98b8da80bf661a5241b85fd',
'person_name': 'Jackie Chan',
'tag': ''},
{'confidence': 0.24913,
'person_id': '0ef10cf989df7888f376fc54e339b93a',
'person_name': 'Yanzi Sun',
'tag': ''},
{'confidence': 0.0,
'person_id': 'a8070f1d28f28fffbb45491da06f3620',
'person_name': 'Qiaoen Chan',
'tag': ''}],
'face_id': 'e9b0968077ae7a40ff9eebffadec1520',
'position': {'center': {'x': 44.5, 'y': 29.75},
'eye_left': {'x': 40.519167, 'y': 24.590125},
'eye_right': {'x': 47.810167, 'y': 23.993575},
'height': 23.0,
'mouth_left': {'x': 40.731833, 'y': 35.77625},
'mouth_right': {'x': 47.273167, 'y': 35.041},
'nose': {'x': 45.096167, 'y': 31.58725},
'width': 15.333333}}],
'session_id': 'dbbabdf0e75d49ff8674f136f0c06bdd'}
============================================================
The person with highest confidence: Jackie Chan
我给了三张训练图片:syz.jpeg, cqe.jpeg, jk.jpeg分别代表三个明星,最后一个是Jackie Chan,测试图片也给的Jackie Chan,最终还是准确的检测和识别出来了。
最后要注意python是脚本语言,所以没有编译的过程,上述代码也没有错误处理的过程,所以如果程序出现了bug会直接停止执行,那么就没办法执行103,104行删除group和person的代码了。这个造成的影响就是再次运行上述代码的时候,云端数据库里面仍然有上一次的group和person,而同一个app里面是不允许的,就会报“NAME_EXIST”的错误,这时候一种办法是运行demo下面的cmdtool.py,在出现的交互式命令行里面用下面的代码手动删除创建的group和person:
api.group.delete(group_name = 'forfun')
api.person.delete(person_name='Jackie Chan')
api.person.delete(person_name='Qiaoen Chan')
api.person.delete(person_name='Yanzi Sun')
参考:
[1]Face++主页:http://www.faceplusplus.com.cn/
[2]Face++开发者文档:http://www.faceplusplus.com.cn/api-overview/
[3]Face++ python sdk: https://github.com/FacePlusPlus/facepp-python-sdk
[Face++]Face初探——人脸检测的更多相关文章
- caffe_实战之两个简单的例子(物体分类和人脸检测)
一.物体分类: 这里使用的是caffe官网中自带的例子,我这里主要是对代码的解释~ 首先导入一些必要的库: import caffe import numpy as np import matplot ...
- 利用html5、websocket和opencv实现人脸检测
最近学习人脸识别相关的东西,在MFC下使用OpenCV做了一个简单的应用.训练需要较多的数据,windows应用程序终究还是不方便,于是想着做成CS模式:检测识别都放在服务器端,视频获取和显示都放在网 ...
- 基于Emgu CV的人脸检测代码
这个提供的代码例子是Emgu CV提供的源码里面自带的例子,很好用,基本不需要改,代码做的是人脸检测不是人脸识别,这个要分清楚.再就是新版本的Emgu CV可能会遇到系统32位和64位处理方式有区别的 ...
- 浅谈人脸检测之Haar分类器方法
我们要探讨的Haar分类器实际上是Boosting算法(提升算法)的一个应用,Haar分类器用到了Boosting算法中的AdaBoost算法,只是把AdaBoost算法训练出的强分类器进行了级联,并 ...
- 基于OpenCv的人脸检测、识别系统学习制作笔记之三
1.在windows下编写人脸检测.识别系统.目前已完成:可利用摄像头提取图像,并将人脸检测出来,未进行识别. 2.在linux下进行编译在windows环境下已经能运行的代码. 为此进行了linux ...
- 基于MATLAB的adaboost级联形式的人脸检测实现
很早之前就做过一些关于人脸检测和目标检测的课题,一直都没有好好总结出来,趁着这个机会,写个总结,希望所写的内容能给研究同类问题的博友一些见解和启发!!博客里面涉及的公式太繁琐了,直接截图了. 转载请注 ...
- 学习OpenCV——行人检测&人脸检测(总算运行出来了)
之前运行haar特征的adaboost算法人脸检测一直出错,加上今天的HOG&SVM行人检测程序,一直报错. 今天总算发现自己犯了多么白痴的错误——是因为外部依赖项lib文件没有添加完整,想一 ...
- 基于OpenCv的人脸检测、识别系统学习制作笔记之二
在网上找到了一个博客,里面有大量内容适合初学者接触和了解人脸检测的博文,正好符合我目前的学习方面,故将链接放上来,后续将分类原博客的博文并加上学习笔记. 传送门: http://blog.sina.c ...
- 基于OpenCv的人脸检测、识别系统学习制作笔记之一
基于OpenCv从视频文件到摄像头的人脸检测 在OpenCv中读取视频文件和读取摄像头的的视频流然后在放在一个窗口中显示结果其实是类似的一个实现过程. 先创建一个指向CvCapture结构的指针 Cv ...
随机推荐
- ArcGIS中的 .tpk数据
转:http://blog.csdn.net/mytudousi/article/details/33347249 什么是tpk文件 tpk是ArcGIS10.1推出的一种新的数据文件类型,主要是用于 ...
- js根据数组对象中某个元素合并数组
一个数组,根据数组中某个元素,合并数组 // 需要被合并的数组,把Index相同的数组合并 const arr = [{id:0,name:'张三'}, {id:0,name:'李四'}, {id:1 ...
- 忽略UserInterfaceState.xcuserstate
GIT忽略iOS项目UserInterfaceState.xcuserstate 1.删除仓库中跟踪的UserInterfaceState.xcuserstate git rm --cached ...
- 购物车删除商品,总价变化 innerHTML = ''并没有删除节点,内容仍存在
w元素的上的下. function deleteLi(tmpId) { //document.getElementById(tmpId).innerHTML = ''; var wdel = docu ...
- Yii2的主从数据库设置
项目做大了,数据库主从还是不可少的.使用Yii框架开发,如何设置数据库的主从呢?其实很简单. 先说一个主数据库服务器和多个从数据库服务器的情况,修改配置文件 config/db.php ,其中 sla ...
- Xshell 连接虚拟机特别慢 解决方案
由于各种原因,xshell连接虚拟机的rhel或者CentOS都几乎是龟速...... 今天专门查了一下解决方案: 原来是ssh的服务端在连接时会自动检测dns环境是否一致导致的,修改为不检测即可,操 ...
- Bad Hair Day---poj3250(栈的运用)
题目链接:http://poj.org/problem?id=3250 题意: n个牛排成一列向右看,牛i能看到牛j的头顶,当且仅当牛j在牛i的右边并且牛i与牛j之间的所有牛均比牛i矮. 设牛i能看到 ...
- Web应用扩展系列(1):架构篇(转)
原文:Web应用扩展系列(1):架构篇 在这篇文章中,我将尽量涵盖Web应用扩展或性能调优时可能会遇到的一些架构问题. 首先,让我们来统一一些名词或项目的概念,下文中我将列举在扩展Web应用时可能会遇 ...
- Nginx 之 内存池
1.基本结构 先来学习一下nginx内存池的几个主要数据结构:[见:./src/core/ngx_palloc.h/.c] ngx_pool_data_t(内存池数据块结构) 1: typed ...
- Python多线程、多进程和协程的实例讲解
线程.进程和协程是什么 线程.进程和协程的详细概念解释和原理剖析不是本文的重点,本文重点讲述在Python中怎样实际使用这三种东西 参考: 进程.线程.协程之概念理解 进程(Process)是计算机中 ...