Three steps to implement license plate recognition in OpenHarmony

introduce

This license plate recognition project is based on the open source project EasyPR (Easy to do Plate Recognition). EasyPR is an open source Chinese license plate recognition system developed based on the OpenCV open source library.

This project uses Runhe HiSpark Taurus AI Camera (Hi3516DV300) camera development board kit (hereinafter referred to as Hi3516) to take pictures of license plates and display the recognition results. The system used is OpenAtom OpenHarmony (referred to as "OpenHarmony") 3.1 Release small system.

First, aim the camera in Hi3516 at the license plate, and its distance is about 60cm~70cm as shown below:

After running the program, press 1 to take a photo, and press 2 to output the recognition result as shown below:

Development Process

This license plate recognition project is implemented using the media subsystem in OpenHarmony. The code is based on local license plate recognition in parking scenarios. To explain, the code structure is as follows:

Three-party library transplantation

The EasyPR implementation is based on the OpenCV implementation, so the implementation of EasyPR must first port OpenCV. The way of transplantation is to use Gn to call the Shell script, and the Shell script to call the Makefile for implementation.

├── BUILD.gn
├── include 
│   ├── camera.h                                       // 摄像头定义
│   ├── local_net_communication.h          // 设备协同主要功能定义
│   ├── local_net_def.h                             // 设备协同打印日志
│   ├── local_net_dlist.h                            // 设备协同设备列表定义
│   ├── local_net_message.h                    // 设备协同传输消息定义
│   ├── local_net_udp.h                             // 设备协同udp协议定义
│   ├── local_net_utils.h                            // 设备协同通用工具定义
│   ├── log.h                                              // 打印日志定义
│   └── wpa_work.h                                   // wifi设置定义
└── src
    ├── base64.cpp                                     // 图片转base64格式功能代码 
    ├── camera.cpp                                     // 摄像头实现
    ├── local_net_communication.c            // 设备协同主要功能实现
    ├── local_net_dlist.c                              // 设备协同设备列表实现
    ├── local_net_message.c                      // 设备协同传输消息实现
    ├── local_net_udp.c                              // 设备协同udp协议实现
    ├── local_net_utils.c                              // 设备协同通用工具实现
    ├── main.cpp                                         // 主程序
    └── wpa_work.c                                     // wifi设置实现

The general process of porting is described below. For details, please refer to Running Open Source Project License Plate Recognition on Small Systems and Porting OpenCV Library.

Porting OpenCV

Download the source code

Get the source code Put the OpenCV library source code under third_party in the OpenHarmony root directory:

Generate Makefile

Create a new build directory in the root directory of the OpenCV source code to generate the Makefile file:

Use cmake-gui to configure the build environment:

cd build
make-gui ..

The displayed UI interface is as follows:

Click Configure to configure, select the fourth option to configure, as shown below:

Configure the toolchain:

Click Generate to generate the Makefile.

Create a shell script   

Add build_opencv.sh to the root directory of the OpenCV source code:

touch build_opencv.sh
chmod 777 build_opencv.sh
vim build_opencv.sh
##添加如下内容
#!/bin/sh
processor=`cat /proc/cpuinfo|grep processor | sort -u | wc -l`
cd build
make -j$processor
cp lib/* $1/libs/

Create Gn file

Add BUILD.gn to the root directory of the OpenCV source code and add the OpenCV library to the build:

Porting EasyPR

Download the source code

获取源码 EasyPR 库源码放在源码根目录下的 third_party 下:

生成Makefile

在 EasyPr 源码根目录新建 build 目录:

mkdir build
cd build
cmake-gui ..

显示的 UI 界面如下图:

点击 Configure 进行配置,选择第四个选项进行配置,如下图:

配置工具链:

点击 Generate 生成 Makefile。

创建Shell脚本

在 EasyPR 源码根目录新增 build_easypr.sh:

创建Gn文件

在 EasyPR 源码根目录新增 BUILD.gn 加入至编译构建:

vim BUILD.gn


#BUILD.gn中添加如下内容
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/ndk/ndk.gni")


root_build = rebase_path(root_build_dir)


build_ext_component("easypr_lib") {
    command = "sh build_easypr.sh $root_build"
    exec_path = "$root_build/../../../third_party/EasyPR"
}


lite_component("easypr") {
    deps = [
        "//third_party/opencv:opencv",
        ":easypr_lib"
    ]
    features = []
}

最终 OpenCV 与 EasyPR 在 third_party 目录如下图所示:

在 OpenHarmony 实现 EasyPR 需要主要分为如下三步:

  1. GN 构建,将 EasyPR 加入编译构建;

  2. 拍照,调用 OpenHarmony 拍照接口,拍摄车牌;

  3. EasyPR 本地识别,调用 EasyPR 识别车牌接口并返回识别结果。

GN构建

GN 构建中包含了 EasyPR 的头文件路径 、链接 EasyPR 动态库、编译依赖 EasyPR。如下所示:

拍照

拍照功能是基于官方文档拍照开发指导开发的,其 demo 样例在如下目录:

在停车场景中二维码识别与车牌识别共用同一份拍照代码 ,为提高二维码识别率在拍照初始化时须将分辨率设置为 1280*720。该改动在进行车牌识别时不会影响 ,初始化拍照代码如下图:

设置照片保存路径在文件 camera.h 下: 

因为在停车场景中二维码扫码与车牌识别都会调用拍照接口,因此使用 s_runAi 作区分:

int main(int argc,char **argv)
{
    int ret;
    char licensePlate[32] = {0};
    char input;
    InitCamera();
    PlateInit();
    while(cin >> input) {
        switch (input) {
            case '1':
                RunAICamera();                                                  // 拍照
                break;
            case '2':
                memset(licensePlate, 0, sizeof(licensePlate));
                ret = GetPlateString(IMG_PATH, licensePlate);   // 识别车牌
                SAMPLE_INFO("ret -> %d, licensePlate->%s", ret, licensePlate);
                break;
            case 's':
                PlateDeinit();
                ExitCamera();
                return 0;
            default:
                SAMPLE_ERROR("input Error");
                break;
        }
    }
    return 0;
}

进行拍照后会进入拍照数据处理,当 s_runAi 为 false 说明是二维码识别,直接调用二维码识别接口即可。当 s_runAi 为 true 时须将拍照的数据保存为图片:

将拍照数据以图片保存路径为“/sdcard/CaptureAi.jpg” 。

EasyPR本地识别

编写主程序 main.cpp 设置程序功能为按 1 拍照、按 2 显示结果 :

编译烧录

前文大致概括了 OpenCV 和 EasyPR 的移植步骤,更详细的关于环境搭建、烧录以及项目源码构建的步骤,请查看参考文章本地车牌识别。

总结

编写车牌识别库对外接口,相关接口使用可以参考作者文章介绍;本文章的源码参考本地车牌识别。丰富多样的 OpenHarmony 开发样例离不开广大合作伙伴和开发者的贡献,如果你也想把自己开发的样例分享出来,欢迎把样例提交到 OpenHarmony 知识体系 SIG 仓来,共建开发样例请参考如何共建开发样例。

车牌识别器(OpenCV版本)

gitee.com/openharmony…

车牌识别器

gitee.com/openharmony…

拍照开发指导

gitee.com/openharmony…

作者文章列表

gitee.com/link?target…

源码参考

gitee.com/openharmony…

构建开发样例

gitee.com/openharmony…

Guess you like

Origin juejin.im/post/7117073657121210398