背景:客户说"太贵了"
“我这个系统里的百度车牌识别每个月费用太高了,能不能找个免费方案?”
这是近期我接手的一个停车场管理系统(PHP项目)遇到的真实需求。
作为以 PHP 为主力开发语言的程序员,面对这个看似简单的需求却陷入了沉思:PHP 生态中应该是没有成熟的车牌识别方案。所以只能从其他开发语言中寻找一套成熟的替代方案。
技术选型
客户提出需求后,我们开始在网上到处搜索,最终在多方对比后,决定使用 Python 生态中的 HyperLPR 作为最终选择。
HyperLPR 介绍
HyperLPR 是一个开源的中文车牌识别(License Plate Recognition)库,专注于高效、准确地识别中国大陆的车牌。最重要的是支持一键部署。
https://github.com/szad670401/HyperLPR
https://gitee.com/mirrors/HyperLPR
HyperLPR 安装部署
快速安装
python -m pip install hyperlpr3
注意:OpenCV 最新版可能存在兼容问题,最终单独安装了一下 opencv-python==4.2.0.32,再执行上面的命令才成功。
快速测试
lpr3 sample -src 669916bb5769ffeb07aa92877c9c0656.jpg
测试结果如下,车牌识别成功:
启动 WebApi 服务
因为需要部署到服务器上去调用,所以需要用到 HyperLPR3 中内置的 WebApi 服务
lpr3 rest --port 8715 --host 0.0.0.0
ApiPost 测试
PHP项目封装接口请求
function lpr3PlateOcr($filePath) {
// 检查文件是否存在
if (!file_exists($filePath) || !is_readable($filePath)) {
return false;
}
// 初始化 cURL
$ch = curl_init();
$postData = [
'file' => new \CURLFile($filePath)
];
// 设置 cURL 选项
curl_setopt_array($ch, [
CURLOPT_URL => 'http://127.0.0.1:8715/api/v1/rec',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30,
]);
// 执行请求
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
// 处理请求错误
if ($error) {
return false;
}
// 解析 JSON 响应
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}
// 处理成功响应
if (isset($result['code']) && $result['code'] == 5000) {
if (!empty($result['result']['plate_list'][0]['code'])) {
return $result['result']['plate_list'][0]['code'];
}
}
// 处理其他错误情况
return false;
}
百度车牌识别使用简介
百度AI的车辆牌照识别(OCR)技术提供了对机动车车牌的自动检测和识别能力,支持多种车牌类型(如普通蓝牌、黄牌、新能源车牌等)。这是客户原来使用的技术方案,在这里也做一下记录、
官网地址
https://ai.baidu.com/tech/ocr_cars/plate
技术文档
https://ai.baidu.com/ai-doc/OCR/ck3h7y191
注册与准备
-
访问百度智能云官网,注册账号并完成实名认证。
-
进入「控制台」→ 选择「文字识别」→ 找到「车辆牌照识别」服务并开通。
-
创建应用后获取 API Key 和 Secret Key。
调用API
- 获取 Access Token。使用以下请求获取(替换自己的 API Key 和 Secret Key)。文档地址:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API Key】&client_secret=【Secret Key】
- 通过 HTTP POST 请求调用接口。需将图片进行Base64编码或通过URL传递,URL参数中添加 access_token 参数。文档地址:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate
示例代码
// 获取 Access Token
function getAccessToken($apiKey, $secretKey) {
$url = "https://aip.baidubce.com/oauth/2.0/token";
$params = [
"grant_type" => "client_credentials",
"client_id" => $apiKey,
"client_secret" => $secretKey
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
return $result['access_token'];
}
// 车牌识别
function licensePlateOCR($imagePath, $accessToken) {
$url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
// 读取图片并Base64编码
$imageData = base64_encode(file_get_contents($imagePath));
// 构建请求数据
$data = [
"image" => $imageData,
"multi_detect" => "false" // 是否检测多车牌
];
// 发送POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 添加access_token参数
$queryParams = ['access_token' => $accessToken];
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($queryParams));
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// 使用示例
$apiKey = "您的API_KEY";
$secretKey = "您的SECRET_KEY";
$imageFile = "path/to/your/car_plate.jpg";
// 获取Access Token
$accessToken = getAccessToken($apiKey, $secretKey);
// 调用车牌识别
$result = licensePlateOCR($imageFile, $accessToken);
// 输出结果
print_r($result);