【鸿睿创智开发板试用】在OpenHarmony 4.1环境下移植OpenCV示例

目录

引言

修改工程环境

配置OpenCV库和头文件

修改代码

CMakeList.txt文件

['qrcodeInfo.decodes' is possibly 'undefined'.](#'qrcodeInfo.decodes' is possibly 'undefined'.)

[warning: backslash and newline separated by space [-Wbackslash-newline-escape]](#warning: backslash and newline separated by space [-Wbackslash-newline-escape])

运行

结语


引言

在前面的博文(【鸿睿创智开发板试用】移植OpenCV 4到OpenHarmony 4.1-CSDN博客)中,我已经介绍了如何将OpenCV库移植到RK3568开发板上。今天来测试一下OpenCV示例:samples/ohos_samples · OpenHarmony-SIG/third_party_opencv - 码云 - 开源中国。原来的工程是针对API 9的,这个版本的工程在DevEco Studio 4.1下会有一些问题,所以需要做一些修改。

修改工程环境

原来的工程是针对API 9的,我们需要先将其改为API 11。API 9和API 10以上版本,工程级build-profile.json5文件中compileSdkVersioncompatibleSdkVersion 字段的位置是不一样的。需要将这两个字段从app下迁移到当前选中的product中。

在我的环境中,如果不升级到API 11,DevEco Studio总是尝试安装老版本的ArkTs,而每次都失败,所以必须升级。

配置OpenCV库和头文件

除了前面介绍的方法生成OpenCV的库文件,还有个办法就是直接下载官方准备好的库文件。现在的地址是:opencv-4.5.5-ndk · OpenHarmony-SIG/third_party_opencv - Gitee.com

下载opencv-4.5.5-ndk.zip 这个文件即可。

解压这个文件后将其中的头文件复制到 entry/src/main/cpp/include,将其中的库文件需要复制到 entry/libs 目录下。

修改entry/build-profile.json5文件中的abiFilters为"arm64-v8a"。

{
  "apiType": "stageMode",
  "buildOption": {
    "externalNativeOptions": {
      "path": "./src/main/cpp/CMakeLists.txt",
      "arguments": "",
      "abiFilters": [  //用于设置本机的ABI编译环境
        //"armeabi-v7a"
        "arm64-v8a"
      ],

entry/libs 目录下的库文件有"armeabi-v7a"、"arm64-v8a"和"x86_64"三个版本,对于我们的开发板,保留"arm64-v8a"这个目录就行,其他两个用不到,除非你打算制作一个在所有OpenHarmony平台上都能工作的版本。

修改代码

编译的过程中,还发现一些问题,大多数是因为OpenHarmony的新版本对语法要求更严了,所以需要做一些小修改。

CMakeList.txt文件

entry\src\main\cpp\CMakeList.txt这个文件有点小问题,修改如下:

# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(opencv_sample)

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
set(OPENCV_LIB_PATH ${NATIVERENDER_ROOT_PATH}/../../../libs/${OHOS_ARCH})
link_directories(${OPENCV_LIB_PATH})
#add_subdirectory(aki)

set(CMAKE_BUILD_WITH_INSTALL_RPATH On)

# opencv test, perf_test, examples, docs不需要编译,开启opencl并行运行
set(BUILD_TESTS OFF)
set(BUILD_PERF_TESTS OFF)
set(BUILD_EXAMPLES OFF)
set(BUILD_DOCS OFF)
set(WITH_OPENCL ON)

# 设置编译类型为Release
set(CMAKE_BUILD_TYPE RELEASE)
set(CMAKE_CXX_FLAGS_RELEASE -g0)

# 按需选择要编译的opencv module, 与target_link_libraries中的一致
set(BUILD_LIST core imgproc imgcodecs objdetect)

#add_subdirectory(opencv)

add_library(ohos_opencv_sample SHARED
    common/init.cpp
    common/common.cpp
    image_basic/image2Gray.cpp
    image_basic/QRCode_identify.cpp
    image_basic/faceDetect.cpp
    image_basic/edgeDetection.cpp
    )

#debug日志编译宏
#add_definitions(-DOHOS_OPENCV_SAMPLE_DEBUG)

target_include_directories(ohos_opencv_sample PUBLIC
    include
    opencv/include
    opencv/includecv4
    opencv/modules/core/include
    opencv/modules/calib3d/include
    opencv/modules/features2d/include
    opencv/modules/dnn/include
    opencv/modules/flann/include
    opencv/modules/highgui/include
    opencv/modules/imgcodecs/include
    opencv/modules/imgproc/include
    opencv/modules/ml/include
    opencv/modules/objdetect/include
    opencv/modules/photo/include
    opencv/modules/stitching/include
    opencv/modules/video/include
    opencv/modules/videoio/include
    opencv/build)
target_link_libraries(ohos_opencv_sample PUBLIC
    libace_napi.z.so
    librawfile.z.so
    libhilog_ndk.z.so
    opencv_core
    opencv_imgcodecs
    opencv_imgproc
    opencv_objdetect)

'qrcodeInfo.decodes' is possibly 'undefined'.

G:\DevEcoStudioProjects\opencv_app_sample\entry\src\main\ets\pages\image\basicSample\qrcode.ets:76
ERROR: ArkTS:ERROR File: G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/ets/pages/image/basicSample/qrcode.ets:76:40
 'qrcodeInfo.decodes' is possibly 'undefined'.

这个错误因为ArkTS怀疑访问一个可能是undefined的对象属性或方法。将代码修改如下:

                      if(qrcodeInfo.decodes != undefined)
                      {
                        this.qrcodeStr = qrcodeInfo.decodes[0];
                      }

warning: backslash and newline separated by space [-Wbackslash-newline-escape]

G:\HUAWEI\OpenHarmony\Sdk\11\native\llvm\bin\clang++.exe --target=aarch64-linux-ohos --gcc-toolchain=G:/HUAWEI/OpenHarmony/Sdk/11/native/llvm --sysroot=G:/HUAWEI/OpenHarmony/Sdk/11/native/sysroot  -Dohos_opencv_sample_EXPORTS -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/includecv4 -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/core/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/calib3d/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/features2d/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/dnn/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/flann/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/highgui/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/imgcodecs/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/imgproc/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/ml/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/objdetect/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/photo/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/stitching/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/video/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/videoio/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/build -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security   -D__MUSL__ -g0 -fPIC -MD -MT CMakeFiles/ohos_opencv_sample.dir/image_basic/QRCode_identify.cpp.o -MF CMakeFiles\ohos_opencv_sample.dir\image_basic\QRCode_identify.cpp.o.d -o CMakeFiles/ohos_opencv_sample.dir/image_basic/QRCode_identify.cpp.o -c G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/image_basic/QRCode_identify.cpp
clang++: warning: argument unused during compilation: '--gcc-toolchain=G:/HUAWEI/OpenHarmony/Sdk/11/native/llvm' [-Wunused-command-line-argument]
In file included from G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/image_basic/QRCode_identify.cpp:17:
G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/include/common.h:85:47: warning: backslash and newline separated by space [-Wbackslash-newline-escape]
        if ((theCall) != 0) {                \ 

删除这行反斜杠后面的空格。

运行

编译成功后,就可以连上真机运行了。

总体效果还不错,OpenCV的人脸识别速度不错,但是二维码识别有点慢。

结语

完整的示例文件参见文章顶部的资源。至此,我们完成了OpenCV基本功能的测试,后面就可以再次基础上完成更有趣的项目了。

相关推荐
AI2AGI24 分钟前
天天AI-20250121:全面解读 AI 实践课程:动手学大模型(含PDF课件)
大数据·人工智能·百度·ai·文心一言
滴滴哒哒答答1 小时前
《自动驾驶与机器人中的SLAM技术》ch4:基于预积分和图优化的 GINS
人工智能·机器人·自动驾驶
流烟默1 小时前
NLP自然语言处理中Word2Vec和GloVe概述
人工智能·自然语言处理
羊小猪~~1 小时前
深度学习基础--LSTM学习笔记(李沐《动手学习深度学习》)
人工智能·rnn·深度学习·学习·机器学习·gru·lstm
青松@FasterAI1 小时前
Word2Vec如何优化从中间层到输出层的计算?
人工智能·深度学习·自然语言处理·nlp面题
CES_Asia2 小时前
CES Asia 2025优惠期即将截止,独特模式助力科技盛会
人工智能·科技·数码相机·智能手表
paradoxjun2 小时前
落地级分类模型训练框架搭建(1):resnet18/50和mobilenetv2在CIFAR10上测试结果
人工智能·深度学习·算法·计算机视觉·分类
sci_ei1232 小时前
高水平EI会议-第四届机器学习、云计算与智能挖掘国际会议
数据结构·人工智能·算法·机器学习·数据挖掘·机器人·云计算
Denodo2 小时前
10倍数据交付提升 | 通过逻辑数据仓库和数据编织高效管理和利用大数据
大数据·数据库·数据仓库·人工智能·数据挖掘·数据分析·数据编织