|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Computer Vision Client. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright (c) 2017 Andreas Erhard |
|
6
|
|
|
* @author Andreas Erhard <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Andaris\ComputerVision; |
|
10
|
|
|
|
|
11
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
12
|
|
|
use GuzzleHttp\Exception\ClientException as HttpClientException; |
|
13
|
|
|
use Andaris\ComputerVision\Exception\ClientException; |
|
14
|
|
|
|
|
15
|
|
|
class Client |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Visual Features supported by the Computer Vision API. |
|
19
|
|
|
* |
|
20
|
|
|
* @see https://westus.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa |
|
21
|
|
|
*/ |
|
22
|
|
|
const FEATURE_CATEGORIES = 'Categories'; |
|
23
|
|
|
const FEATURE_TAGS = 'Tags'; |
|
24
|
|
|
const FEATURE_DESCRIPTION = 'Description'; |
|
25
|
|
|
const FEATURE_FACES = 'Faces'; |
|
26
|
|
|
const FEATURE_IMAGE_TYPE = 'ImageType'; |
|
27
|
|
|
const FEATURE_COLOR = 'Color'; |
|
28
|
|
|
const FEATURE_ADULT = 'Adult'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string Computer Vision API Key |
|
32
|
|
|
* |
|
33
|
|
|
* @see https://azure.microsoft.com/en-us/try/cognitive-services/my-apis/ |
|
34
|
|
|
*/ |
|
35
|
|
|
private $apiKey = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string Computer Vision API Endpoint, the default should work for most users. |
|
39
|
|
|
*/ |
|
40
|
|
|
private $endpoint = ''; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var HttpClient Guzzle HTTP Client. |
|
44
|
|
|
*/ |
|
45
|
|
|
private $httpClient; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $apiKey |
|
51
|
|
|
* @param string $endpoint |
|
52
|
|
|
* @param HttpClient|null $httpClient |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function __construct( |
|
55
|
|
|
$apiKey, |
|
56
|
|
|
$endpoint = 'https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze', |
|
57
|
|
|
$httpClient = null |
|
58
|
|
|
) { |
|
59
|
2 |
|
$this->apiKey = $apiKey; |
|
60
|
2 |
|
$this->endpoint = $endpoint; |
|
61
|
|
|
|
|
62
|
2 |
|
$this->httpClient = $httpClient ? $httpClient : new HttpClient(); |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Analyzes an image. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $imageData Binary image data. |
|
69
|
|
|
* @param string[] $visualFeatures Features which should be analyzed, see FEATURE_* constants. |
|
70
|
|
|
* |
|
71
|
|
|
* @return array Result of the Computer Vision API, the JSON is automatically decoded. |
|
72
|
|
|
* @see https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/php |
|
73
|
|
|
* |
|
74
|
|
|
* @throws ClientException |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function analyze($imageData, $visualFeatures = [self::FEATURE_TAGS]) |
|
77
|
|
|
{ |
|
78
|
|
|
$headers = [ |
|
79
|
2 |
|
'Content-Type' => 'application/octet-stream', |
|
80
|
2 |
|
'Ocp-Apim-Subscription-Key' => $this->apiKey, |
|
81
|
2 |
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$parameters = [ |
|
84
|
2 |
|
'visualFeatures' => implode(',', $visualFeatures), |
|
85
|
2 |
|
'language' => 'en', |
|
86
|
2 |
|
]; |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
2 |
|
$response = $this->httpClient->request( |
|
90
|
2 |
|
'POST', |
|
91
|
2 |
|
$this->endpoint, |
|
92
|
|
|
[ |
|
93
|
2 |
|
'headers' => $headers, |
|
94
|
2 |
|
'query' => $parameters, |
|
95
|
2 |
|
'body' => $imageData, |
|
96
|
|
|
] |
|
97
|
2 |
|
); |
|
98
|
2 |
|
} catch (HttpClientException $e) { |
|
99
|
1 |
|
throw new ClientException( |
|
100
|
1 |
|
sprintf('An error occured when calling the Computer Vision API: "%s"', $e->getMessage()) |
|
101
|
1 |
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return json_decode($response->getBody(), true); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|