|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ridvanbaluyos\Face; |
|
3
|
|
|
|
|
4
|
|
|
use Noodlehaus\Config as Config; |
|
5
|
|
|
/** |
|
6
|
|
|
* Microsoft Cognitive Services- Face API |
|
7
|
|
|
* https://www.microsoft.com/cognitive-services/en-us/face-api/documentation/overview |
|
8
|
|
|
* |
|
9
|
|
|
* @package Cognitive Services |
|
10
|
|
|
* @author Ridvan Baluyos <[email protected]> |
|
11
|
|
|
* @link https://github.com/ridvanbaluyos/face |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
class FaceDetection |
|
15
|
|
|
{ |
|
16
|
|
|
private $url; |
|
17
|
|
|
private $subscriptionKey; |
|
18
|
|
|
private $image; |
|
19
|
|
|
private $returnFaceId = false; |
|
20
|
|
|
private $returnFaceLandmarks = false; |
|
21
|
|
|
private $returnFaceAttributes = ''; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($image) { |
|
27
|
|
|
$conf = Config::load(__DIR__ . '/config.json'); |
|
28
|
|
|
$this->subscriptionKey = $conf['subscriptionKey']; |
|
29
|
|
|
$this->url = $conf['url']; |
|
30
|
|
|
$this->image = $image; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get all the face/s detected in an image. |
|
35
|
|
|
* |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getFaces() |
|
38
|
|
|
{ |
|
39
|
|
|
$params = array( |
|
40
|
|
|
'returnFaceId' => $this->returnFaceId, |
|
41
|
|
|
'returnFaceLandmarks' => $this->returnFaceLandmarks, |
|
42
|
|
|
'returnFaceAttributes' => $this->returnFaceAttributes |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$query = http_build_query($params); |
|
46
|
|
|
$image = json_encode($this->image); |
|
47
|
|
|
|
|
48
|
|
|
$ch = curl_init(); |
|
49
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->url . '?' . $query); |
|
50
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
|
51
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $image); |
|
52
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
53
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
|
54
|
|
|
'Ocp-Apim-Subscription-Key: ' . $this->subscriptionKey, |
|
55
|
|
|
'Content-Type: application/json', |
|
56
|
|
|
'Content-Length: ' . strlen($image) |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
$response = curl_exec($ch); |
|
60
|
|
|
|
|
61
|
|
|
return $response; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Optional parameter to get face landmarks. |
|
66
|
|
|
* |
|
67
|
|
|
*/ |
|
68
|
|
|
public function analyzeFaceLandmarks() { |
|
69
|
|
|
$this->returnFaceLandmarks = 'true'; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Optional parameter to get face attributes. |
|
76
|
|
|
* |
|
77
|
|
|
*/ |
|
78
|
|
|
public function analyzeFaceAttributes() { |
|
79
|
|
|
$this->returnFaceAttributes = 'age,gender,headPose,smile,facialHair,glasses'; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Alternatively, you can enable all options. |
|
86
|
|
|
* |
|
87
|
|
|
*/ |
|
88
|
|
|
public function analyzeAll() { |
|
89
|
|
|
$this->returnFaceLandmarks = 'true'; |
|
|
|
|
|
|
90
|
|
|
$this->returnFaceAttributes = 'age,gender,headPose,smile,facialHair,glasses'; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.