概述
PaddleOCR 是一款基于 PaddlePaddle 深度学习平台的开源 OCR 工具。PP-OCR是PaddleOCR自研的实用的超轻量OCR系统。它是一个两阶段的OCR系统,其中文本检测算法选用DB,文本识别算法选用CRNN,并在检测和识别模块之间添加文本方向分类器,以应对不同方向的文本识别。
PP-OCRv4 在速度可比的情况下,中文场景端到端 hmean 指标相比于 PP-OCRv3 提升 4.25%。英文数字场景,相比于 PP-OCRv3 英文模型提升 6%。在有评估集的四种语系识别准确率平均提升 5% 以上。对已支持的 80 余种语言识别模型进行了升级更新,优化了多语言场景下的识别效果,平均准确率提升超 8%。
官方代码:https://github.com/PaddlePaddle/PaddleOCR
模型下载
模型地址:https://paddlepaddle.github.io/PaddleOCR/latest/ppocr/model_list.html
PP-OCRv4提供了版面分析、表格识别、文本检测模型、文本方向分类器、文本识别等模型。在这里,笔者只下载检测、方向、识别三种轻量版本的推理模型。
下载完成后,解压文件。
inference.pdparams:模型的参数文件,存储了模型的权重和偏置等信息,用于推理时加载模型的权重。
inference.pdmodel:模型的结构文件,存储了神经网络的架构信息(例如层的定义和计算方式),推理时通过此文件定义模型结构。
模型转换
首先将下载的 paddle 模型转换为 onnx模型。可以从这个地址https://github.com/paddlepaddle/paddle2onnx下载源码进行编译转换。
或者直接安装环境去转换:
conda create -n paddle2onnx python=3.8
activate paddle2onnx
pip install PaddlePaddle==2.6.0
pip install onnxruntime>=1.10.0
pip install paddle2onnx
paddle2onnx --model_dir ch_PP-OCRv4_det_infer --model_filename inference.pdmodel
--params_filename inference.pdiparams --save_file ch_PP-OCRv4_det_infer.onnx
模型部署
#include<iostream>
#include <io.h>
#include <fcntl.h>
#include<opencv2/opencv.hpp>
#include<onnxruntime_cxx_api.h>
#include"text_det.h"
#include"text_angle_cls.h"
#include"text_rec.h"
#include "utils.h"int main()
{std::string img_path = "images/1.jpg";cv::Mat src_img = cv::imread(img_path);cv::rotate(src_img, src_img, 1);const std::string det_model = "model/ch_PP-OCRv4_det_infer.onnx";const std::string cls_model = "model/ch_ppocr_mobile_v2.0_cls_infer.onnx";const std::string rec_model = "model/ch_PP-OCRv4_rec_infer.onnx";bool isGPU = true;TextDetector text_det(det_model, isGPU);TextClassifier text_cls(cls_model, isGPU);TextRecognizer text_rec(rec_model, isGPU);std::vector<std::vector<cv::Point2f>> results = text_det.detect(src_img);std::sort(results.begin(), results.end(), utils::compareBoxes);cv::Mat det_img = src_img.clone();for (const auto& polygon : results) {std::vector<cv::Point> intPolygon;for (const auto& point : polygon) {intPolygon.emplace_back(cv::Point(static_cast<int>(point.x), static_cast<int>(point.y)));}cv::polylines(det_img, intPolygon, true, cv::Scalar(0, 0, 255), 1);}//text_det.draw_pred(src_img, results);cv::imshow("Detected Text Boxes", det_img);cv::waitKey(0);for (size_t i = 0; i < results.size(); i++) {cv::Mat textimg = text_det.get_rotate_crop_image(src_img, results[i]);cv::imshow("single_text_box", textimg);cv::waitKey(0);if (text_cls.predict(textimg) == 1) {cv::rotate(textimg, textimg, 1); }cv::imshow("single_text_rotate", textimg);cv::waitKey(0);int textWidth = textimg.cols;std::string full_text = "";if (textWidth < 250) {std::string text = text_rec.predict_text(textimg);full_text = text;}else {int segmentWidth = 250; int numSegments = std::ceil((float)textWidth / segmentWidth); for (int seg = 0; seg < numSegments; ++seg) {int startX = seg * segmentWidth;int endX = std::min(startX + segmentWidth, textWidth); cv::Rect roi(startX, 0, endX - startX, textimg.rows);cv::Mat segment = textimg(roi);std::string segment_text = text_rec.predict_text(segment);full_text += segment_text;}}_setmode(_fileno(stdout), _O_U8TEXT);std::wstring w_text = utils::charToWstring(full_text.c_str());std::wcout << w_text << std::endl;}
}