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

 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易极

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值