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

经过了强烈的思想斗争才把自己拖到图书馆做毕设T^T

anyway, 因为毕设里面有人脸识别的部分,所以就想找个现成的api先玩玩,于是就找到最近很火的face++:http://www.faceplusplus.com.cn/

接口什么的还是很简单的,主要就是看它的api开发文档,最终实现把demo中的hello.py改造之后能够上传本地的三张图片进行训练,然后对新的一幅图片进行识别,看这幅图片中的人脸是三张图片中的哪一张,对于我的毕设而言,这个功能其实就足够了。修改后的hello.py如下:

  1 #!/usr/bin/env python2
  2 # -*- coding: utf-8 -*-
  3 # $File: hello.py
  4 
  5 # In this tutorial, you will learn how to call Face ++ APIs and implement a
  6 # simple App which could recognize a face image in 3 candidates.
  7 # 在本教程中,您将了解到Face ++ API的基本调用方法,并实现一个简单的App,用以在3
  8 # 张备选人脸图片中识别一个新的人脸图片。
  9 
 10 # You need to register your App first, and enter you API key/secret.
 11 # 您需要先注册一个App,并将得到的API key和API secret写在这里。
 12 API_KEY = '********'
 13 API_SECRET = '*********'
 14 
 15 # Import system libraries and define helper functions
 16 # 导入系统库并定义辅助函数
 17 import time
 18 from pprint import pformat
 19 def print_result(hint, result):
 20     def encode(obj):
 21         if type(obj) is unicode:
 22             return obj.encode('utf-8')
 23         if type(obj) is dict:
 24             return {encode(k): encode(v) for (k, v) in obj.iteritems()}
 25         if type(obj) is list:
 26             return [encode(i) for i in obj]
 27         return obj
 28     print hint
 29     result = encode(result)
 30     print '\n'.join(['  ' + i for i in pformat(result, width = 75).split('\n')])
 31 
 32 # First import the API class from the SDK
 33 # 首先,导入SDK中的API类
 34 from facepp import API
 35 from facepp import File
 36 
 37 api = API(API_KEY, API_SECRET)
 38 
 39 # Here are the person names and their face images
 40 # 人名及其脸部图片
 41 PERSONS = [
 42     ('Yanzi Sun', './syz.jpeg'),
 43     ('Qiaoen Chan', './cqe.jpeg'),
 44     ('Jackie Chan', './jk.jpeg')
 45 ]
 46 TARGET_IMAGE = './cl.jpg'
 47 
 48 # Step 1: Create a group to add these persons in
 49 # 步骤1: 新建一个group用以添加person
 50 api.group.create(group_name = 'forfun')
 51 
 52 # Step 2: Detect faces from those three images and add them to the persons
 53 # 步骤2:从三种图片中检测人脸并将其加入person中。 
 54 for (name, path) in PERSONS:
 55     result = api.detection.detect(img = File(path))
 56     print_result('Detection result for {}:'.format(name), result)
 57 
 58     face_id = result['face'][0]['face_id'] 
 59 
 60     # Create a person in the group, and add the face to the person
 61     # 在该group中新建一个person,并将face加入期中
 62     api.person.create(person_name = name, group_name = 'forfun',
 63             face_id = face_id)
 64 
 65 
 66 # Step 3: Train the group.
 67 # Note: this step is required before performing recognition in this group,
 68 # since our system needs to pre-compute models for these persons
 69 # 步骤3:训练这个group
 70 # 注:在group中进行识别之前必须执行该步骤,以便我们的系统能为这些person建模
 71 result = api.recognition.train(group_name = 'forfun', type = 'all')
 72 
 73 # Because the train process is time-consuming, the operation is done
 74 # asynchronously, so only a session ID would be returned.
 75 # 由于训练过程比较耗时,所以操作必须异步完成,因此只有session ID会被返回
 76 print_result('Train result:', result)
 77 
 78 session_id = result['session_id']
 79 
 80 # Now, wait before train completes
 81 # 等待训练完成
 82 while True:
 83     result = api.info.get_session(session_id = session_id)
 84     if result['status'] == u'SUCC':
 85         print_result('Async train result:', result)
 86         break
 87     time.sleep(1)
 88 
 89 #也可以通过调用api.wait_async(session_id)函数完成以上功能
 90 
 91 
 92 # Step 4: recognize the unknown face image
 93 # 步骤4:识别未知脸部图片
 94 result = api.recognition.recognize(img = File(TARGET_IMAGE), group_name = 'forfun')
 95 print_result('Recognize result:', result)
 96 print '=' * 60
 97 print 'The person with highest confidence:', \
 98         result['face'][0]['candidate'][0]['person_name']
 99 
100 
101 # Finally, delete the persons and group because they are no longer needed
102 # 最终,删除无用的person和group
103 api.group.delete(group_name = 'forfun')
104 api.person.delete(person_name = [i[0] for i in PERSONS])
105 
106 # Congratulations! You have finished this tutorial, and you can continue
107 # reading our API document and start writing your own App using Face++ API!
108 # Enjoy :)
109 # 恭喜!您已经完成了本教程,可以继续阅读我们的API文档并利用Face++ API开始写您自
110 # 己的App了!
111 # 旅途愉快 :)

要注意的就是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

posted @ 2015-08-04 22:00  SunshineAtNoon  阅读(6236)  评论(0编辑  收藏  举报