|
|
|
<?php
|
|
|
|
|
|
|
|
require_once "../config.php";
|
|
|
|
require_once __ROOT__ . "/baidu/BaiduAiRequest.php";
|
|
|
|
require_once __ROOT__ . "/entity/Garbage.php";
|
|
|
|
require_once __ROOT__ . "/database/Query.php";
|
|
|
|
require_once __ROOT__ . "/api/JsonResponse.php";
|
|
|
|
|
|
|
|
//查询垃圾分类
|
|
|
|
function query_waste_soring($image_res)
|
|
|
|
{
|
|
|
|
// 匹配结果大于0才进行处理
|
|
|
|
if (isset($image_res["result_num"]) && $image_res["result_num"] > 0) {
|
|
|
|
// 匹配关键字
|
|
|
|
$keyword = array();
|
|
|
|
$sql = "";
|
|
|
|
// 去除匹配结果数组
|
|
|
|
foreach ($image_res["result"] as $result) {
|
|
|
|
// 筛选置信值大于0.5的结果
|
|
|
|
if ($result["score"] >= 0.5) {
|
|
|
|
array_push($keyword, '%' . $result["keyword"] . '%');
|
|
|
|
// 拼接模糊查询语句
|
|
|
|
$sql = $sql . " or name like ?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 如果筛选结果大于0处理
|
|
|
|
if (count($keyword) > 0) {
|
|
|
|
|
|
|
|
$sql = "select * from garbage where" . substr($sql, 3);
|
|
|
|
info("查询sql" . $sql);
|
|
|
|
$garbage_result = QueryGarbageWithName($sql, $keyword);
|
|
|
|
json_res(json_encode(array("img_parse" => true, "query" => true, "img_res" => $image_res, "result" => $garbage_result->getGarbageObjArray()), JSON_UNESCAPED_UNICODE));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
json_res(json_encode(array("img_parse" => true, "img_res" => $image_res)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
json_res(json_encode(array("img_parse" => false)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查询token
|
|
|
|
$token_result = new class() extends AbstractTokenQuery
|
|
|
|
{
|
|
|
|
public function bind_param(mysqli_stmt $stmt, array $param = null)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DbUtil::query("select * from api_token limit 0,1", $token_result);
|
|
|
|
|
|
|
|
// toekn如果失效,则更新token
|
|
|
|
if (strtotime(date(default_format)) >= strtotime($token_result->getToken()->getEndTime())) {
|
|
|
|
info("token失效了,更新token");
|
|
|
|
$updateToken = new BaiduAiRequest("https://aip.baidubce.com/oauth/2.0/token");
|
|
|
|
$updateToken->request_token();
|
|
|
|
DbUtil::query("select * from api_token limit 0,1", $token_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//获取图片参数
|
|
|
|
if (isset($_POST["image"])) {
|
|
|
|
// 请求通用物体和场景识别高级版接口
|
|
|
|
$image_req = new BaiduAiRequest("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general");
|
|
|
|
// 获取解析结果
|
|
|
|
$image_res = $image_req->request_with_param(array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $_POST["image"]));
|
|
|
|
query_waste_soring($image_res);
|
|
|
|
} else if ($_FILES["file"]["error"] > 0) {
|
|
|
|
json_res(<<<EOF
|
|
|
|
{"msg":"文件上传失败"}
|
|
|
|
EOF
|
|
|
|
);
|
|
|
|
} else if ($_FILES["file"]["error"] == 0) {
|
|
|
|
// base64加密文件二进制数据
|
|
|
|
$image = base64_encode(file_get_contents($_FILES["file"]["tmp_name"]));
|
|
|
|
// 请求通用物体和场景识别高级版接口
|
|
|
|
$image_req = new BaiduAiRequest("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general");
|
|
|
|
// 获取解析结果
|
|
|
|
$image_res = $image_req->request_with_param(array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $image));
|
|
|
|
query_waste_soring($image_res);
|
|
|
|
} else {
|
|
|
|
json_res(<<<EOF
|
|
|
|
{"msg":"缺少图片参数"}
|
|
|
|
EOF
|
|
|
|
);
|
|
|
|
}
|