-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify.pl
executable file
·72 lines (56 loc) · 2.25 KB
/
identify.pl
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
use strict;
use warnings;
use Net::Azure::CognitiveServices::Face;
use Guard 'guard';
### more shortly
my $api = 'Net::Azure::CognitiveServices::Face';
### add access_key string
$api->access_key($ARGV[0]);
### set endpoint url
$api->endpoint($ARGV[1]);
### Source data of Japanese popular persons
my %image = (
'Yo Ooizumi' => 'http://s3-ap-northeast-1.amazonaws.com/topicks/article_thumb/15584_original.jpg',
'Takayuki Suzui' => 'https://www.office-cue.com/image/portrait/xl_talent01.jpg',
'Ken Yasuda' => 'http://yononakanews.com/wp-content/uploads/2014/10/3658-1.jpg',
'Takuma Otoo' => 'http://image.pia.jp/images/news/img/ORG_20111128001518.jpg',
'Hiroyuki Morisaki' => 'http://www.suruga-ya.jp/database/pics/game/g6235130.jpg',
);
### Definition of PersonGroup ID
my $person_group_id = 'dummy';
### Remove the PersonGroup when finished each tasks
my $guard = guard {
$api->PersonGroup->delete($person_group_id)
};
### Create the PersonGroup
$api->PersonGroup->create($person_group_id, name => 'Super Star');
### Register persons to Face API
for my $name (keys %image) {
sleep 1; ## Care for Request Rate Limit
my $person = $api->Person->create($person_group_id, name => $name);
$api->Person->add_face($person_group_id, $person->{personId}, $image{$name});
}
### Train the PersonGroup
$api->PersonGroup->train($person_group_id);
### Wait a moment to finish training...
for (1 .. 12) {
sleep 5;
my $status = $api->PersonGroup->training_status($person_group_id);
last if $status->{status} eq "succeeded";
}
### Detect a face image of "Yo Ooizumi", and fetch it's faceId.
my $face = $api->Face->detect('http://www.officiallyjd.com/wp-content/uploads/2012/02/20120227_akb_13.jpg');
### Identify the face image from PersonGroup by faceId
my $ident = $api->Face->identify(
faceIds => [ $face->[0]{faceId} ],
personGroupId => $person_group_id,
maxNumOfCandidatesReturned => 1,
confidenceThreshold => 0.5,
);
### Fetch a candidated data
my $candidate = $ident->[0]{candidates}[0];
### Get a person data by candidated personId
my $hit = $api->Person->get($person_group_id, $candidate->{personId});
### output a person data
use Data::Dumper;
warn Dumper($hit);