You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.9 KiB
92 lines
3.9 KiB
<?php
|
|
|
|
require_once $_SERVER["DOCUMENT_ROOT"] . "/config.php";
|
|
require_once $_SERVER["DOCUMENT_ROOT"] . "/baidu/BaiduAiRequest.php";
|
|
require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Garbage.php";
|
|
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php";
|
|
require_once $_SERVER["DOCUMENT_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 = new QueryGarbage();
|
|
try {
|
|
DbUtil::query($sql, $garbage_result, $keyword);
|
|
json_res(json_encode(array("img_parse" => true, "query" => true, "img_res" => $image_res, "result" => $garbage_result->getGarbageObjArray()), JSON_UNESCAPED_UNICODE));
|
|
} catch (Exception $e) {
|
|
json_res(json_encode(array("img_parse" => true, "query" => false, "img_res" => $image_res, "error" => "查询异常,请联系管理员"), JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
} else {
|
|
json_res(json_encode(array("img_parse" => true, "img_res" => $image_res, "query" => false, "error" => "没有查询结果")));
|
|
}
|
|
} else {
|
|
json_res(json_encode(array("img_parse" => false)));
|
|
}
|
|
}
|
|
|
|
|
|
// 查询token
|
|
$token_result = new QueryToken();
|
|
try {
|
|
DbUtil::query("select * from api_token limit 0,1", $token_result);
|
|
} catch (Exception $e) {
|
|
json_res(json_encode(array("img_parse" => false, "msg" => "无法调用百度API,请联系管理员")));
|
|
return;
|
|
}
|
|
|
|
// toekn如果失效,则更新token
|
|
if (strtotime(date(default_format)) >= strtotime($token_result->getToken()->getEndTime())) {
|
|
info("token失效了,更新token");
|
|
$result = BaiduAiRequest::request_token();
|
|
if (array_key_exists("status", $result) and $result["status"]) {
|
|
try {
|
|
DbUtil::query("select * from api_token limit 0,1", $token_result);
|
|
} catch (Exception $e) {
|
|
json_res(json_encode(array("img_parse" => false, "msg" => "无法调用百度API,请联系管理员")));
|
|
return;
|
|
}
|
|
} else {
|
|
json_res(json_encode(array("img_parse" => false, "msg" => "无法调用百度API,请联系管理员")));
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
//获取图片参数
|
|
if (isset($_POST["image"])) {
|
|
// 请求通用物体和场景识别高级版接口
|
|
// 获取解析结果
|
|
$image_res = BaiduAiRequest::request_with_param("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general", array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $_POST["image"]));
|
|
query_waste_soring($image_res);
|
|
} else if ($_FILES["file"]["error"] > 0) {
|
|
json_res(json_encode(array("img_parse" => false, "msg" => "照片上传失败")));
|
|
} else if ($_FILES["file"]["error"] == 0) {
|
|
// base64加密文件二进制数据
|
|
$image = base64_encode(file_get_contents($_FILES["file"]["tmp_name"]));
|
|
// 请求通用物体和场景识别高级版接口
|
|
// 获取解析结果
|
|
$image_res = BaiduAiRequest::request_with_param("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general", array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $image));
|
|
query_waste_soring($image_res);
|
|
} else {
|
|
json_res(json_encode(array("img_parse" => false, "msg" => "缺少图片参数")));
|
|
}
|
|
|