百度运车牌识别API接口分两段PHP程序,
一段为获取TOKEN, 一段为利用TOKEN去识别车号. 整个代码如下:
<?php
/**
* 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
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;
}
/////////////////////////////////////
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = '你向百度云申请的client_id';
$post_data['client_secret'] = '你向百度云申请的client_secret';
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$res = request_post($url, $post_data);
//var_dump($res);
$json = json_decode($res,true);//将json解析成数组
//var_dump($json);
//echo "refresh_token=".$json['refresh_token']."<br>";
//echo "expires_in=".$json['expires_in']."<br>";
//echo "session_key=".$json['session_key']."<br>";
//echo "access_token=".$json['access_token']."<br>";
//echo "scope=".$json['scope'];
//////////////////////////////////////
$token = $json['access_token'];
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=' . $token;
//$img = file_get_contents("upload/" . $_FILES["file"]["name"]);
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$res = request_post($url, $bodys);
//var_dump($res);
$json = json_decode($res,true);//将json解析成数组
//var_dump($json);
if(!isset($json['error_code'])){ //排除不是车号
echo $msg;
echo "<center><p style='color: #231d82;font-size: 24PX;'>识别车号为:".$json['words_result']['number']." 号牌颜色:".$json['words_result']['color']."</p><br>";
echo "系统将对车号:".$json['words_result']['number']."执行程序逻辑</br>";
echo "例如:保安巡查核对在场库车辆,拍照上传照片将与软件场内车辆匹配。</br>";
echo "或者车辆照片上传,自动得到车号列表,利用电子表格完成匹配。</center>";
/* echo $json['words_result']['vertexes_location'][0]['x']."<br>";
echo $json['words_result']['vertexes_location'][0]['y']."<br>";
echo $json['words_result']['vertexes_location'][1]['x']."<br>";
echo $json['words_result']['vertexes_location'][1]['y']."<br>";
echo $json['words_result']['vertexes_location'][2]['x']."<br>";
echo $json['words_result']['vertexes_location'][2]['y']."<br>";
echo $json['words_result']['vertexes_location'][3]['x']."<br>";
echo $json['words_result']['vertexes_location'][3]['y']."<br>";
echo $json['words_result']['color']."<br>";
echo $json['words_result']['probability'][0]."<br>";
echo $json['words_result']['probability'][1]."<br>";
echo $json['words_result']['probability'][2]."<br>";
echo $json['words_result']['probability'][3]."<br>";
echo $json['words_result']['probability'][4]."<br>";
echo $json['words_result']['probability'][5]."<br>";
echo $json['words_result']['probability'][6]."<br>";
echo $json['log_id']."<br>"; */
}else{
echo "<center>识别错误代码为:".$json['error_code'].",提示:".$json['error_msg']."</center>";
}