准备工作

百度PaddleOCR V2.5链接 PaddleOCR
推理库 链接paddle_inference
模型 链接cls det rec
opencv V4.2.0链接opencv

文件放置

在这里插入图片描述

paddle_inference

提取so 复制到 /usr/lib

cd paddle_inference/
mkdir paddle_lib
find . -name "*.so*" | xargs -i cp {} ./paddle_lib
sudo cp -rf ./paddle_lib/*.so* /usr/lib

opencv

修改/PaddleOCR/deploy/cpp_infer/tools/build_opencv.sh

root_path="/home/xd/tool/PaddleOCR/deploy/cpp_infer/opencv-4.2.0"
install_path=${root_path}/opencv4
build_dir=${root_path}/build

rm -rf ${build_dir}
mkdir ${build_dir}
cd ${build_dir}

cmake .. \
    -DCMAKE_INSTALL_PREFIX=${install_path} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DWITH_IPP=OFF \
    -DBUILD_IPP_IW=OFF \
    -DWITH_LAPACK=OFF \
    -DWITH_EIGEN=OFF \
    -DCMAKE_INSTALL_LIBDIR=lib64 \
    -DWITH_ZLIB=ON \
    -DBUILD_ZLIB=ON \
    -DWITH_JPEG=ON \
    -DBUILD_JPEG=ON \
    -DWITH_PNG=ON \
    -DBUILD_PNG=ON \
    -DWITH_TIFF=ON \
    -DBUILD_TIFF=ON

make -j4

执行 ./build_opencv.sh

PaddleOCR

修改/PaddleOCR/deploy/cpp_infer/tools/build.sh

OPENCV_DIR="/home/xd/tool/PaddleOCR/deploy/cpp_infer/opencv-4.2.0/"
LIB_DIR="/home/xd/tool/paddle_inference"
#CUDA_LIB_DIR=your_cuda_lib_dir
#CUDNN_LIB_DIR=your_cudnn_lib_dir

BUILD_DIR=build
rm -rf ${BUILD_DIR}
mkdir ${BUILD_DIR}
cd ${BUILD_DIR}
cmake .. \
    -DPADDLE_LIB=${LIB_DIR} \
    -DWITH_MKL=OFF \
    -DWITH_GPU=OFF \
    -DWITH_STATIC_LIB=OFF\
    -DWITH_TENSORRT=OFF \
    -DOPENCV_DIR=${OPENCV_DIR} \
   # -DCUDNN_LIB=${CUDNN_LIB_DIR} \
   # -DCUDA_LIB=${CUDA_LIB_DIR} \
    #-DTENSORRT_DIR=${TENSORRT_DIR} \

make -j4
cd /PaddleOCR/deploy/cpp_infer/
mkdir build 
sudo ./tools/build.sh

编译报错

修改CMakeLists.txt
在这里插入图片描述

测试

将模型文件放置在inference/文件夹
在这里插入图片描述

./ppocr --det_model_dir=./inference/ch_ppocr_mobile_v2.0_det_infer  --rec_model_dir=./inference/ch_ppocr_mobile_v2.0_rec_infer --image_dir=/home/xd/code/pic

在这里插入图片描述

Qt Demo

将 cpp_infer 里的 include src 拷贝置Qt工程下
在这里插入图片描述
编辑paddleocr.pri

CONFIG += c++11

PADDLE_PATH = /home/xd/tool/paddle_inference

INCLUDEPATH += $$PWD
INCLUDEPATH += $$PADDLE_PATH/third_party/install/gflags/include
INCLUDEPATH += $$PADDLE_PATH/third_party/install/glog/include
INCLUDEPATH += $$PADDLE_PATH/paddle/include

INCLUDEPATH +=  /usr/include/opencv4/
INCLUDEPATH +=  /usr/include/opencv4/opencv2
LIBS += -L/usr/local/x86_64-linux-gnu -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs

LIBS += -L$$PADDLE_PATH/paddle/lib \
    -L$$PADDLE_PATH/third_party/install/protobuf/lib \
    -L$$PADDLE_PATH/third_party/install/glog/lib \
    -L$$PADDLE_PATH/third_party/install/gflags/lib \
    -L$$PADDLE_PATH/third_party/install/xxhash/lib \
    -lpaddle_inference \
    -lprotobuf \
    -lglog \
    -lgflags \
    -lxxhash \
    -lz

SOURCES += \
    $$PWD/src/args.cpp \
    $$PWD/src/clipper.cpp \
    $$PWD/src/ocr_cls.cpp \
    $$PWD/src/ocr_det.cpp \
    $$PWD/src/ocr_rec.cpp \
    $$PWD/src/paddleocr.cpp \
    $$PWD/src/postprocess_op.cpp \
    $$PWD/src/preprocess_op.cpp \
    $$PWD/src/utility.cpp

HEADERS += \
    $$PWD/include/args.h \
    $$PWD/include/clipper.h \
    $$PWD/include/args.h \
    $$PWD/include/ocr_cls.h \
    $$PWD/include/ocr_det.h \
    $$PWD/include/ocr_rec.h \
    $$PWD/include/paddleocr.h \
    $$PWD/include/postprocess_op.h \
    $$PWD/include/preprocess_op.h \
    $$PWD/include/utility.h

TestDemo3.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11
DESTDIR = $$PWD/../bin/
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mygraphicsview.cpp \
    widget.cpp

HEADERS += \
    mygraphicsview.h \
    widget.h

FORMS += \
    widget.ui


include( $$PWD/3rdparty/paddle/paddleocr.pri)

unix {
    INCLUDEPATH += /usr/lib
    LIBS += -ldl -lstdc++

}

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

重要部分!!!

修改args.cpp 默认模型路径
在这里插入图片描述

DEFINE_string(det_model_dir, "./inference/ch_PP-OCRv4_det_infer/", "Path of det inference model.");
DEFINE_string(cls_model_dir, "./inference/ch_ppocr_mobile_v2.0_cls_infer/", "Path of cls inference model.");
DEFINE_string(rec_model_dir, "./inference/ch_PP-OCRv4_rec_infer/", "Path of rec inference model.");
DEFINE_string(rec_char_dict_path, "./inference/ppocr_keys_v1.txt",

ppocr_keys_v1.txt在 PaddleOCR/ppcor/utils 里

测试代码

 #include <include/args.h>
 #include <include/paddleocr.h>
 using namespace PaddleOCR;

 PPOCR* ocr = nullptr;

 ocr = new PPOCR();
 try {
        
        std::vector<cv::String> cv_all_img_names;
        cv_all_img_names.push_back("./2.jpg");
        std::vector<std::vector<OCRPredictResult>> ocr_results =
                ocr->ocr(cv_all_img_names, true, true, true);
        
        //std::vector<std::vector<OCRPredictResult>> ocr_results =
        //ocr.ocr(cv_all_img_names, FLAGS_det, FLAGS_rec, FLAGS_cls);
        
        for (int i = 0; i < cv_all_img_names.size(); ++i) {
            Utility::print_result(ocr_results[i]);
            std::vector<OCRPredictResult> ocr_result = ocr_results[i];
            for (int i = 0; i < ocr_result.size(); i++) {
                // rec
                if (ocr_result[i].score != -1.0) {
                    QString strShow = QString("text:[%1] score:[%2] ").arg(ocr_result[i].text.c_str()).arg(ocr_result[i].score);
                    //ui->textEdit->append(strShow);
                }
                
            }
        }
        
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return ;
    }

delete ocr;
ocr = nullptr;

效果展示

inference 模型文件夹放到bin目录
在这里插入图片描述

结束语

本文在虚拟机银河麒麟x86环境验证通过,涉及到opencv源码编译,paddleocr源码编译,paddle_inference使用官网编译库(补充:paddleocrv2.9链接paddle_inference编译不过,从而使用v2.5)

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐