-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncAli.php
229 lines (187 loc) · 7.3 KB
/
SyncAli.php
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* Created by PhpStorm.
* User: xing.chen
* Date: 2018/6/13
* Time: 10:44
*/
namespace xing\contentSafe;
use GuzzleHttp\Client;
use xing\contentSafe\core\BaseAli;
use xing\contentSafe\core\ImageSafeInterFace;
use GuzzleHttp\Psr7\Request;
/**
* 同步检测
* Class SyncAli
* @package xing\contentSafe
*/
class SyncAli extends BaseAli implements ImageSafeInterFace
{
/**
* 返回建议
* @param string $suggestion
* @return string
*/
private function getSuggestion(string $suggestion) : string
{
return $suggestion == 'pass' ? static::CHECK_OK : ($suggestion == 'block' ? static::CHECK_SERIOUS : $suggestion);
}
public function getTextInfo(array $contents, $taskIds = '')
{
$client = static::getClient();
$request = static::getTextRequest();
if (count($contents) > 100) throw new \Exception('每次任务不能超过100个');
// 组织任务数据
$tasks = [];
foreach ($contents as $k => $content) {
$tasks[] = [
'dataId' => $k,
'content' => $content,
];
}
// 封装为JSON
$request->setContent(json_encode([
"tasks" => $tasks,
"scenes" => ['antispam']
]));
// 返回鉴别结果
$result = [];
$this->response = $response = $client->getAcsResponse($request);
if (200 != $response->code) throw new \Exception($response->msg, $response->code);
foreach ($response->data as $key => $taskResult) {
if(200 != $taskResult->code) continue;
$sceneResults = $taskResult->results;
foreach ($sceneResults as $detail) {
$this->results[$key][$detail->label] = $this->getSuggestion($detail->suggestion);
$result[$key][$detail->label] = $this->getSuggestion($detail->suggestion);
}
}
return $result;
}
public function getImageInfo(array $urls, $taskIds = '')
{
$client = static::getClient();
$request = static::getSyncRequest();
if (count($urls) > 100) throw new \Exception('每次任务不能超过100个');
// 组织任务数据
foreach ($urls as $k => $url) {
$tasks[] = [
'dataId' => $k,
'url' => $url,
];
}
// 封装为JSON
$request->setContent(json_encode([
"tasks" => $tasks,
"scenes" => $this->config['scenes']
]));
// 返回鉴别结果
$resultImg = [];
$this->response = $response = $client->getAcsResponse($request);
if (200 != $response->code) throw new \Exception($response->msg, $response->code);
$taskResults = $response->data;
foreach ($taskResults as $key => $taskResult) {
if(200 != $taskResult->code) continue;
$sceneResults = $taskResult->results;
foreach ($sceneResults as $sceneResult) {
$label = $sceneResult->label;
$rate = $sceneResult->rate;
//根据scene和suggetion做相关的处理
//do something
$suggestion = $this->getSuggestion($sceneResult->suggestion);
switch ($sceneResult->scene) {
case 'sface':
// 名人脸
if (isset($sceneResult->sfaceData) && isset($sceneResult->sfaceData[0]->faces)) {
$this->results[$key]['celebrityFace'] = $sceneResult->sfaceData;
} else {
$this->results[$key]['celebrityFaceRate'] = 0;
}
// 人脸
$this->results[$key]['isFace'] = $label == 'sface' && isset($sceneResult->sfaceData);
break;
default:
$resultImg[$key][$sceneResult->scene] = $suggestion;
}
}
}
return $resultImg;
}
public function addVideoTask(array $urls, $taskIds = '')
{
$client = static::getClient();
$request = static::getVideoRequest();
if (count($urls) > 100) throw new \Exception('每次任务不能超过100个');
// 组织任务数据
$tasks = [];
foreach ($urls as $k => $url) {
$tasks[] = [
'dataId' => $k,
'url' => $url,
];
}
// 封装为JSON
$request->setContent(json_encode([
"tasks" => $tasks,
"scenes" => $this->config['scenes']
]));
// 返回鉴别结果
$result = [];
$this->response = $response = $client->getAcsResponse($request);
if (200 != $response->code) throw new \Exception($response->msg, $response->code);
return $response->data;
}
public function faceDetect($image, $type = '0')
{
$post = ['type' => $type];
$type == 0 ? $post['image_url'] = $image : $post['content'] = $image;
// 参数设置
$config = $this->config;
$akId = $config["accessKeyId"];
$akSecret = $config["accessKeySecret"];
$body1 = '{"type":"0","image_url":"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1553926699&di=3e4484731c8897c57e67b3f632801f9a&src=http://b-ssl.duitang.com/uploads/item/201603/28/20160328121906_ErzAB.jpeg"}';
$body1 = json_encode($post);
$url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/detect";
$date1 = gmdate("D, d M Y H:i:s \G\M\T");
// 参数构造
$options = array(
'http' => array(
'header' => array(
'accept'=> "application/json",
'content-type'=> "application/json",
'date'=> $date1,
'authorization' => ''
),
'method' => "POST", //可以是 GET, POST, DELETE, PUT
'content' => $body1
// 'content' => json_encode($body1)
)
);
$http = $options['http'];
$header = $http['header'];
$urlObj = parse_url($url);
if(empty($urlObj["query"]))
$path = $urlObj["path"];
else
$path = $urlObj["path"]."?".$urlObj["query"];
$body = $http['content'];
if(empty($body))
$bodymd5 = $body;
else
$bodymd5 = base64_encode(md5($body,true));
$stringToSign = $http['method']."\n".$header['accept']."\n".$bodymd5."\n".$header['content-type']."\n".$header['date']."\n".$path;
$signature = base64_encode(
hash_hmac(
"sha1",
$stringToSign,
$akSecret, true));
$authHeader = "Dataplus "."$akId".":"."$signature";
$options['http']['header']['authorization'] = $authHeader;
# 构造Rest API Client请求
$client = new Client();
$headers = ['Content-type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => $options['http']['header']['authorization'], 'Date' => $date1];
$request = new Request('POST','https://dtplus-cn-shanghai.data.aliyuncs.com/face/detect',$headers,$body1);
$response = $client->send($request);
return json_decode($response->getBody());
}
}