基本思想:项目需求,用c++写了一个关于paddleOCR和zbar的条形码检测,最后封装dll文件,利用c#调用。
1、 创建一个c++工程,这里需要opencv库,ncnn库,zbar库。
代码结构需要迎合c#调用风格。
main.cpp
#include "connect.h"
int main() {
cv::Mat image = cv::imread("C:/Users/xxx/source/repos/ocr_barcode/ocr_barcode/captrue.png");
unsigned char* src = image.data; // C# -> C++ 不能直接传递opencv格式
const char* dbnet_param = "C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_det-op.param";
const char* dbnet_bin = "C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_det-op.bin";
const char* crnnnet_param = "C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_rec-op.param";
const char* crnnnet_bin = "C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_rec-op.bin";
const char* paddle_text = "C:/Users/xxx/Desktop/barcode_ocr(1)/model/paddleocr_keys.txt";
float boxscorethresh = 0.4;
float boxthresh = 0.3;
float unclipratio = 2.0;
char result_text[10240] = { 0 };
init_model(dbnet_param, dbnet_bin, crnnnet_param, crnnnet_bin, paddle_text);
detect_image(src, image.rows, image.cols, boxscorethresh, boxthresh, unclipratio, result_text);
printf("%s\n", result_text);
return 0;
}
connect.h
#ifndef BARCODE_OCR_CONNECT_H
#define BARCODE_OCR_CONNECT_H
#include "dbnet.h"
#include "crnnnet.h"
#include "zbarcode.h"
extern "C" __declspec (dllexport) int __stdcall init_model(const char* dbnet_param, const char* dbnet_bin, const char* crnnnet_param,
const char* crnnnet_bin, const char* paddle_text);
extern "C" __declspec (dllexport) int __stdcall detect_image(unsigned char* ImageBuffer, int height, int wight,
float boxscorethresh, float boxthresh, float unclipratio, char* result_text);
#endif //BARCODE_OCR_CONNECT_H
connect.cpp
#include "connect.h"
auto item_dbnet = new Dbnet();
auto item_crnnnet = new Crnnnet();
auto zbardecode = new Zbarcode();
int __stdcall init_model(const char* dbnet_param, const char* dbnet_bin, const char* crnnnet_param,
const char* crnnnet_bin, const char* paddle_text) {
int ret = item_dbnet->init_model(dbnet_param, dbnet_bin);
if (ret != 0) {
return -1;
}
ret = item_crnnnet->init_model(crnnnet_param, crnnnet_bin, paddle_text);
if (ret != 0) {
return -1;
}
return 0;
}
int __stdcall detect_image(unsigned char* ImageBuffer_ocr, int height, int wight,
float boxscorethresh, float boxthresh, float unclipratio, char* result_text) {
cv::Mat rgb = cv::Mat(height, wight, CV_8UC3, ImageBuffer_ocr);
// ocr检测
std::vector<TextBox> objects = item_dbnet->getTextBoxes(rgb, boxscorethresh, boxthresh, unclipratio);
// ocr识别
std::vector<std::string> ocr_str = item_crnnnet->getPartImages(rgb, objects);
// zbar条形码检测
printf("----------条形码识别结果---------\n");
cv::Mat image = rgb.clone();
std::string zbar_str;
// zbar条形码识别
int ret = zbardecode->zbarDetector(image, zbar_str);
if (ret != 0) {
printf("条形码识别失败,请重新检测!\n");
return -1;
}
rapidjson::Document item_doc;
item_doc.SetObject();
rapidjson::Document::AllocatorType& allocator = item_doc.GetAllocator();
for (auto& str : ocr_str) {
std::string result;
for (char c : str) {
// ASCII 0 - 9 A - Z a - z
if ((static_cast<int>(c) >= 48 && static_cast<int>(c) <= 57) ||
(static_cast<int>(c) >= 65 && static_cast<int>(c) <= 90) ||
(static_cast<int>(c) >= 97 && static_cast<int>(c) <= 122)) {
result += c;
}
}
if (result == zbar_str) {
printf("\n信息对比成功!\n");
rapidjson::Value result_kstr(rapidjson::kStringType);
result_kstr.SetString(result.c_str(), result.length(), allocator);
item_doc.AddMember("result", result_kstr, allocator);
break;
}
else {
printf("OCR识别信息与条形码信息不一致,请重新检测!\n");
}
}
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> write_json(buffer);
item_doc.Accept(write_json);
std::string buf_json_str = buffer.GetString();
allocator.Clear();
memcpy(result_text, buf_json_str.c_str(), buf_json_str.size());
return 0;
}
篇幅有限,中间包含的类和相关函数暂时就不一一展示。
运行一下,注意:运行环境为Relaese x64环境下!
2、 生成dll动态链接库(Relaese x64)
C#传递Mat数据给C++动态包处理,并将处理结果Mat返回给C#显示、保存_参考此处。
3、创建.NET工程,实现dll文件的调用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Text;
using System.Drawing.Imaging;
namespace test_dll
{
class Program
{
[DllImport(@"C:\Users\xxx\source\repos\ocr_barcode\x64\Release\ocr_barcode.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int init_model(StringBuilder dbnet_param, StringBuilder dbnet_bin, StringBuilder crnnnet_param, StringBuilder crnnnet_bin, StringBuilder paddle_text);
[DllImport(@"C:\Users\xxx\source\repos\ocr_barcode\x64\Release\ocr_barcode.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int detect_image(byte[] ImageBuffer, int height, int width, float boxscorethresh, float boxthresh, float unclipratio, StringBuilder data);
static void Main()
{
// 加载模型
StringBuilder dbnet_param = new StringBuilder("C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_det-op.param");
StringBuilder dbnet_bin = new StringBuilder("C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_det-op.bin");
StringBuilder crnnnet_param = new StringBuilder("C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_rec-op.param");
StringBuilder crnnnet_bin = new StringBuilder("C:/Users/xxx/Desktop/barcode_ocr(1)/model/pdocrv2.0_rec-op.bin");
StringBuilder paddle_text = new StringBuilder("C:/Users/xxx/Desktop/barcode_ocr(1)/model/paddleocr_keys.txt");
// 模型初始化
int ret = init_model(dbnet_param, dbnet_bin, crnnnet_param, crnnnet_bin, paddle_text);
if (ret == 0)
{
Console.WriteLine("successfully\n");
}
else
{
Console.WriteLine("failed\n");
}
// 加载图像
Mat frame = new Mat(@"C:\Users\xxx\Desktop\barcode_ocr(1)\captrue.png", ImreadModes.Color);
Bitmap bmp = frame.ToBitmap();
byte[] source = GetBGRValues(bmp);
// 图像推理
StringBuilder textaa = new StringBuilder(10240);
detect_image(source, bmp.Height, bmp.Width, 0.3f, 0.2f, 2.0f, textaa);
}
public static byte[] GetBGRValues(Bitmap bmp)
{
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
var bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
var rowBytes = bmpData.Width * Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
var imgBytes = bmp.Height * rowBytes;
byte[] rgbValues = new byte[imgBytes];
IntPtr ptr = bmpData.Scan0;
for (var i = 0; i < bmp.Height; i++)
{
Marshal.Copy(ptr, rgbValues, i * rowBytes, rowBytes);
ptr += bmpData.Stride;
}
bmp.UnlockBits(bmpData);
return rgbValues;
}
public static Bitmap Byte2Bitmap(Byte[] data, int width, int height)
{
Bitmap image = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bmData = image.LockBits(rect, ImageLockMode.ReadWrite, image.PixelFormat);
IntPtr ptr = bmData.Scan0;
for (int i = 0; i < image.Height; i++)
{
Marshal.Copy(data, i * image.Width * 3, ptr, image.Width * 3);
ptr = (IntPtr)(ptr.ToInt64() + bmData.Stride);
}
image.UnlockBits(bmData);
return image;
}
}
}
运行测试一下,通过!
注意:运行环境为Relaese x64环境下!