-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·106 lines (87 loc) · 3.52 KB
/
train.py
File metadata and controls
executable file
·106 lines (87 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/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 = 'fa3950c60e396f6c4826eb8a0248f5ca'
API_SECRET = 'CXaUC1qyMRnfdPVJ7xmFP1zjXyz5noPp'
import sys
# Import system libraries and define helper functions
# 导入系统库并定义辅助函数
from facepp import File, API
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
group_name = sys.argv[1]
api = API(API_KEY, API_SECRET)
# Here are the person names and their face images
# 人名及其脸部图片
IMAGE_DIR = './images/'
PERSONS = []
f = open('list.txt','r')
for line in f.readlines():
PERSONS.append((line.strip().split('.')[0], IMAGE_DIR + line.strip()))
print PERSONS
#PERSONS = [
# ('1', IMAGE_DIR + '1.jpg'),
# ('2', IMAGE_DIR + '2.jpg'),
# ('3', IMAGE_DIR + '3.jpg')
#]
#TARGET_IMAGE = IMAGE_DIR + '4.jpg'
# Step 1: Detect faces in the 3 pictures and find out their positions and
# attributes
# 步骤1:检测出三张输入图片中的Face,找出图片中Face的位置及属性
FACES = {name: api.detection.detect(img = File(url))
for name, url in PERSONS}
for name, face in FACES.iteritems():
print_result(name, face)
# Step 2: create persons using the face_id
# 步骤2:引用face_id,创建新的person
for name, face in FACES.iteritems():
rst = api.person.create(
person_name = name, face_id = face['face'][0]['face_id'])
print_result('create person {}'.format(name), rst)
# Step 3: create a new group and add those persons in it
# 步骤3:.创建Group,将之前创建的Person加入这个Group
rst = api.group.create(group_name = group_name)
print_result('create group', rst)
rst = api.group.add_person(group_name = group_name , person_name = FACES.iterkeys())
print_result('add these persons to group', rst)
# Step 4: train the model
# 步骤4:训练模型
rst = api.train.identify(group_name = group_name )
print_result('train', rst)
# wait for training to complete
# 等待训练完成
rst = api.wait_async(rst['session_id'])
print_result('wait async', rst)
# Step 5: recognize face in a new image
# 步骤5:识别新图中的Face
# Finally, delete the persons and group because they are no longer needed
# 最终,删除无用的person和group
#api.group.delete(group_name )
#api.person.delete(person_name = FACES.iterkeys())
# 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了!
# 旅途愉快 :)