本文主要是介绍PaddleOCR-2.1.1集成到opencv项目,在C#中调用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、下载这3个
https://github.91chifun.workers.dev//https://github.com/PaddlePaddle/PaddleOCR/archive/refs/tags/v2.1.1.zip
https://paddle-wheel.bj.bcebos.com/2.0.2/win-infer/mkl/cpu/paddle_inference.zip
PaddleOCR/quickstart.md at release/2.0 · PaddlePaddle/PaddleOCR (github.com)
二、 解压v2.1.1.zip,把这两个目录复制到你的项目中,并在源码中添加它们:
三、解压paddle_inference.zip,在项目中引用其中的.lib
四、修改一些代码,让它能返回识别到的文本,位置盒子
paddleocr.h
#pragma once
#if def CREATEDELL_API
#else
#define CREATEDELL_API _declspec(dllimport)
#endif #include "opencv2/core.hpp"cv::Mat CREATEDELL_API GoOCR(cv::Mat& srcimg, std::vector<std::vector<std::vector<int>>>& boxes, std::vector<std::string>& results);
paddleocr.cpp
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.#include "glog/logging.h"
#include "omp.h"
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"#include <include/config.h>
#include <include/paddleocr.h>
#include <include/ocr_det.h>
#include <include/ocr_rec.h>
#include "../opencvcli.h"
using namespace std;
using namespace cv;
using namespace PaddleOCR;
using namespace opencvcli;void GOCW::GoOCR(Mat& srcimg, std::vector<std::vector<std::vector<int>>>& boxes, std::vector<std::string>& results)
{OCRConfig config("config.txt");//config.PrintConfigInfo();DBDetector det(config.det_model_dir, config.use_gpu, config.gpu_id,config.gpu_mem, config.cpu_math_library_num_threads,config.use_mkldnn, config.max_side_len, config.det_db_thresh,config.det_db_box_thresh, config.det_db_unclip_ratio,config.use_polygon_score, config.visualize,config.use_tensorrt, config.use_fp16);Classifier* cls = nullptr;if (config.use_angle_cls == true) {cls = new Classifier(config.cls_model_dir, config.use_gpu, config.gpu_id,config.gpu_mem, config.cpu_math_library_num_threads,config.use_mkldnn, config.cls_thresh,config.use_tensorrt, config.use_fp16);}CRNNRecognizer rec(config.rec_model_dir, config.use_gpu, config.gpu_id,config.gpu_mem, config.cpu_math_library_num_threads,config.use_mkldnn, config.char_list_file,config.use_tensorrt, config.use_fp16);det.Run(srcimg, boxes);rec.Run(boxes, srcimg, cls, results);for (int i = 0; i < boxes.size(); i++){cv::Point p1 = cv::Point(boxes[i][0][0], boxes[i][0][1]);cv::Point p2 = cv::Point(boxes[i][1][0], boxes[i][1][1]);cv::Point p3 = cv::Point(boxes[i][2][0], boxes[i][2][1]);cv::Point p4 = cv::Point(boxes[i][3][0], boxes[i][3][1]);line(srcimg, p1, p2, Scalar(0, 0, 255));line(srcimg, p2, p3, Scalar(0, 0, 255));line(srcimg, p3, p4, Scalar(0, 0, 255));line(srcimg, p4, p1, Scalar(0, 0, 255));}
}
五、模型和运行时
从PaddleOCR-2.1.1\deploy\cpp_infer\tools拿到config.txt 修改里面的内容,指定模型和字典:
从paddle_inference里取出所有的dll文件,主要有这几个
把以上所有文件放入运行目录,完成。
在我的开源项目中演示了C# 调用paddleocr的方法,欢迎关注;
OpenCVSharpHelper: 使用opencvsharp搭建的测试工具,可以方便地从海康相机,USB相机采集图像,测试各个函数不同参数的处理效果。 - Gitee.com
这篇关于PaddleOCR-2.1.1集成到opencv项目,在C#中调用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!