$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": //仪器仪表盘读数识别 $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; $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage?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; //识别失败 } //if ($res<=100) { // //数字识别 // $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/numbers?access_token=' . $token; // $api_json = request_post($url, $bodys); // $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 = -2; //没图片 } break; } //var_dump($res); if ($a!=0) { echo $api_json; }else{ echo $res; } ?>