您的位置:首页 > 其它

[Face++]Face初探——人脸检测

2015-08-04 22:00 387 查看
经过了强烈的思想斗争才把自己拖到图书馆做毕设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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: