网站工作室和网络公司,wordpress后台无法登陆,百度cdn wordpress,外贸综合服务网站建设概述
PaddleOCR 是一款基于 PaddlePaddle 深度学习平台的开源 OCR 工具。PP-OCR是PaddleOCR自研的实用的超轻量OCR系统。它是一个两阶段的OCR系统#xff0c;其中文本检测算法选用DB#xff0c;文本识别算法选用CRNN#xff0c;并在检测和识别模块之间添加文本方向分类器其中文本检测算法选用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 python3.8
activate paddle2onnx
pip install PaddlePaddle2.6.0
pip install onnxruntime1.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模型部署
#includeiostream
#include io.h
#include fcntl.h
#includeopencv2/opencv.hpp
#includeonnxruntime_cxx_api.h
#includetext_det.h
#includetext_angle_cls.h
#includetext_rec.h
#include utils.hint 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::vectorstd::vectorcv::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::vectorcv::Point intPolygon;for (const auto point : polygon) {intPolygon.emplace_back(cv::Point(static_castint(point.x), static_castint(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;}
}