[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 ...
随机推荐
- Codeforces Round #190 (Div. 2).D
一道贪心题. 可以分两种情况 1 .是没有把对面的牌全打败,那么只要用最大的可能去打攻击状态的牌. 2. 是将对面的牌全打败,那么只要保证打对面防守状态的花费最小,就可以保证最后的结果最大 两种情况下 ...
- 【BZOJ3425】Poi2013 Polarization 猜结论+DP
[BZOJ3425]Poi2013 Polarization Description 给定一棵树,可以对每条边定向成一个有向图,这张有向图的可达点对数为树上有路径从u到达v的点对(u,v)个数.求最小 ...
- ios 去掉UITableView Group形式下面的白色阴影
初始化的时候 设置_tableView.separatorStyle = UITableViewCellSeparatorStrleNone;
- LeetCode 笔记系列 14 N-Queen II [思考的深度问题]
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- quartz 防止上一任务未执行完毕,下一时间点重复执行
/** * 订单监控类 * 定时扫描所有待付款订单,超时自动取消 * Created by huangbaidong * 2017/3/29. */ @Component public class O ...
- maven引用本地jar包
教程:http://www.cnblogs.com/dcba1112/archive/2011/05/01/2033805.html 安装 到下载maven: http://maven.apache. ...
- Servlet------>servletDemo 及细节注意
原理图: 前提:我用的命令行都是mac系统下用的,非win jsp实质是一个servlet,所以要先了解servlet,如上页面是一个servletdemo,下面是尝试的步骤 1.先写好Demo.ja ...
- PHP的生命周期
了解PHP生命周期之前,先了解一下apache是怎么和php关联起来的吧~ 1.Apache运行机制剖析 ----------------------------- 总体示意图如下: Apache ...
- Ignite缓存管理初体验
Ignite缓存管理初体验:ignite服务端配置,大家可以用参考官方进行配置(或者使用默认配置也可以). 本文中的ignite使用版本是1.7,与spring结合使用.maven依赖配置 ignit ...
- J.U.C Atomic(一)CAS原理
CAS概念 CAS:Compare And Swap,比较并交换.java.util.concurrent包完全是建立于CAS机制之上的. CAS原理 Java CAS是通过调用Unsafe的nati ...