$v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
// ³õʼ»¯curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// ÒªÇó½á¹ûΪ×Ö·û´®ÇÒÊä³öµ½ÆÁÄ»ÉÏ
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// postÌá½»·½Ê½
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
// ÔËÐÐcurl
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
$method = !empty($_REQUEST["method"])?$_REQUEST["method"]:"";
$a = empty($_REQUEST['a'])!=false ? 0 : $_REQUEST['a'];
$res="";
switch ($method)
{
case "meter": //ÒÇÆ÷ÒDZíÅ̶ÁÊýʶ±ð
$image = empty($_REQUEST['image'])!=false ? "" : $_REQUEST['image'];
if ($image!="") {
$access_token = access_post($url, $post_data);
$token = $access_token;
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/meter?access_token=' . $token;
$img = file_get_contents($image);
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$api_json = request_post($url, $bodys);
//echo $api_json."
";//exit;
$api_info=json_decode($api_json,true);
$error_code = $method = !empty($api_info["error_code"]) ? $api_info["error_code"] : 0;
if ($error_code==0) {
$res=0;
foreach($api_info["words_result"] as $x=>$x_value) {
$words= !empty(intval($x_value["words"])) ? intval($x_value["words"]) : 0;
if ($words>$res) {
$res=$words;
}
}
}else{
$res = -1; //ʶ±ðʧ°Ü
}
}else{
$res = 0; //ûͼƬ
}
break;
case "driver_behavior": //¼ÝÊ»ÐÐΪ·ÖÎö
$image = empty($_REQUEST['image'])!=false ? "" : $_REQUEST['image'];
if ($image!="") {
//966120¼ÝÊ»·ÖÎö
$post_data='';
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = 'Be2FojUHnbFc89KTXKwgncC9';
$post_data['client_secret'] = 'lVkXfyLO0Awfb2rAf9Nf92Xa8psQt23U';
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$access_token = access_post($url, $post_data);
$token = $access_token;
$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior?access_token=' . $token;
$img = file_get_contents($image);
$img = base64_encode($img);
$bodys = array(
'image' => $img,
'type' => 'not_buckling_up'
);
$api_json = request_post($url, $bodys);
//echo $api_json."
";exit;
$api_info=json_decode($api_json,true);
$person_num = $method = !empty($api_info["person_num"]) ? $api_info["person_num"] : 0;
if ($person_num!=0) {
$res=0;
foreach($api_info["person_info"] as $x=>$x_value) {
foreach($x_value["attributes"] as $i=>$i_value) {
//echo var_dump($i_value["score"])."
";//exit;
$score= !empty(round($i_value["score"],2)) ? round($i_value["score"],2) : 1;
if ($score>$res) {
$res=$score;
}
//½¨ÒéãÐֵСÓÚ0.44
}
}
if (!empty($res)) {$res=100-$res*100;}
//½¨Òé´óÓÚ66ΪÒѰó°²È«´ø
}else{
$res = -1; //ʶ±ðʧ°Ü
}
}else{
$res = 0; //ûͼƬ
}
break;
}
//var_dump($res);
if ($a!=0) {
echo $api_json;
}else{
echo $res;
}
?>