百度车牌识别demo

 1 function get_token(){
 2     $url = 'https://aip.baidubce.com/oauth/2.0/token';
 3     $post_data['grant_type']       = 'client_credentials';
 4     $post_data['client_id']      = 'your key';
 5     $post_data['client_secret'] = 'your secret';
 6     $o = "";
 7     foreach ( $post_data as $k => $v ) 
 8     {
 9         $o.= "$k=" . urlencode( $v ). "&" ;
10     }
11     $post_data = substr($o,0,-1);    
12     $res = request_post($url, $post_data);
13     return $res["access_token"];
14     //print_r($res);
15     //$access_token=$res["access_token"];
16 }
17 
18 /**
19 * 发起http post请求(REST API), 并获取REST请求的结果
20 * @param string $url
21 * @param string $param
22 * @return - http response body if succeeds, else false.
23 */
24 function request_post($url = '', $param = ''){
25     if (empty($url) || empty($param)) {
26         return false;
27     }
28     $postUrl = $url;
29     $curlPost = $param;
30     // 初始化curl
31     $curl = curl_init();
32     curl_setopt($curl, CURLOPT_URL, $postUrl);
33     curl_setopt($curl, CURLOPT_HEADER, 0);
34     // 要求结果为字符串且输出到屏幕上
35     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
36     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
37     // post提交方式
38     curl_setopt($curl, CURLOPT_POST, 1);
39     curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
40     // 运行curl
41     $data = curl_exec($curl);
42     curl_close($curl);
43     return $data;
44 }
45 
46 function get_chepai($img_path,$tk){ 
47 $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=' . $tk;
48 $img = file_get_contents($img_path);
49 $img = base64_encode($img);
50 $bodys = array(
51     'image' => $img    
52 );
53 $res = request_post($url, $bodys) ;
54 return $res;
55 }
56 
57 
58 
59 $img_path='./chepai/6.jpg';
60 $tk=get_token();
61 
62 
63 $res=get_chepai($img_path,$tk);
64 $res_json=json_decode(get_chepai($img_path,$tk), true, 512 , JSON_BIGINT_AS_STRING);//防止大数值转变成float类型

66 $car_num=$res_json["words_result"]["number"];
67 $car_clolor=$res_json["words_result"]["color"];
68 $car_real=$res_json["words_result"]["probability"];
69 $car_log=$res_json["log_id"];
70 echo floor($car_real[0]*100)/100;
71 echo $car_log;
72 var_dump($res_json);
73 print_r($res);

 

posted @ 2021-03-15 17:33  fsl  阅读(124)  评论(0编辑  收藏  举报