1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XSolve\FaceValidatorBundle\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
|
7
|
|
|
class GuzzleWrapper implements AzureFaceAPIClient |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var ClientInterface |
11
|
|
|
*/ |
12
|
|
|
private $client; |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $subscriptionKey; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
private static $faceAttributes = [ |
22
|
|
|
'age', 'gender', 'headPose', 'smile', 'facialHair', |
23
|
|
|
'glasses', 'emotion', 'hair', 'makeup', 'occlusion', |
24
|
|
|
'accessories', 'blur', 'exposure', 'noise', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
public function __construct(ClientInterface $client, string $subscriptionKey) |
28
|
|
|
{ |
29
|
|
|
$this->client = $client; |
30
|
|
|
$this->subscriptionKey = $subscriptionKey; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function detect( |
34
|
|
|
string $filePath, |
35
|
|
|
bool $returnFaceId = false, |
36
|
|
|
bool $returnFaceLandmarks = true, |
37
|
|
|
array $returnFaceAttributes = null |
38
|
|
|
): array { |
39
|
|
|
$fs = fopen($filePath, 'r'); |
40
|
|
|
|
41
|
|
|
if (null === $returnFaceAttributes) { |
42
|
|
|
$returnFaceAttributes = self::$faceAttributes; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$response = $this->client->request('POST', 'detect', [ |
46
|
|
|
'headers' => [ |
47
|
|
|
'Content-Type' => 'application/octet-stream', |
48
|
|
|
'Ocp-Apim-Subscription-Key' => $this->subscriptionKey, |
49
|
|
|
], |
50
|
|
|
'query' => [ |
51
|
|
|
'returnFaceId' => $returnFaceId, |
52
|
|
|
'returnFaceLandmarks' => $returnFaceLandmarks, |
53
|
|
|
'returnFaceAttributes' => implode(',', $returnFaceAttributes), |
54
|
|
|
], |
55
|
|
|
'body' => $fs, |
56
|
|
|
]); |
57
|
|
|
$data = json_decode($response->getBody()->getContents(), true); |
58
|
|
|
|
59
|
|
|
return $data ? current($data) : []; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|