百度驾驶证C++离线SDK V1.1 C#接入

百度驾驶证C++离线SDK V1.1 C#接入

目录

说明

效果

项目

代码

下载


说明

自己根据SDK封装了动态库,然后C#调用。

SDK包结构

效果

项目

代码

using Newtonsoft.Json;

using OpenCvSharp;

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Drawing;

using System.IO;

using System.Windows.Forms;

using 驾驶证测试.Commom;

namespace 驾驶证测试

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

string image_path = "";

bool isDraw = false;

private void Form1_Load(object sender, EventArgs e)

{

image_path = Application.StartupPath + "\\front_images\\1.png";

pictureBox1.Image = new Bitmap(image_path);

}

private void button4_Click(object sender, EventArgs e)

{

image_path = Application.StartupPath + "\\front_images\\1.png";

Mat image = new Mat(image_path);

pictureBox1.Image = new Bitmap(image_path);

}

private void button5_Click(object sender, EventArgs e)

{

image_path = Application.StartupPath + "\\back_images\\1.png";

pictureBox1.Image = new Bitmap(image_path);

}

private void button2_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = fileFilter;

if (ofd.ShowDialog() != DialogResult.OK) return;

pictureBox1.Image = null;

image_path = ofd.FileName;

pictureBox1.Image = new Bitmap(image_path);

textBox1.Text = "";

}

private void button1_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

textBox1.Text = "";

Application.DoEvents();

string ocr_result1 = "";

string ocr_result2 = "";

Mat image = new Mat(image_path);

Stopwatch stopwatch = new Stopwatch();

stopwatch.Start();

int res = DL_OCR_Helper.dl_front_ocr2(image, out ocr_result1, out ocr_result2);

stopwatch.Stop();

double totalTime = stopwatch.Elapsed.TotalSeconds;

textBox1.Text += $"耗时: {totalTime:F2}s";

textBox1.Text += "\r\n-------------------\r\n";

if (res == 0)

{

Object jsonObject = JsonConvert.DeserializeObject(ocr_result1.ToString());

textBox1.Text += JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented);

textBox1.Text += "\r\n-------------------\r\n";

Object jsonObject2 = JsonConvert.DeserializeObject(ocr_result2.ToString());

textBox1.Text += JsonConvert.SerializeObject(jsonObject2, Newtonsoft.Json.Formatting.Indented);

List<OcrRes2> lt = JsonConvert.DeserializeObject<List<OcrRes2>>(ocr_result2.ToString());

foreach (OcrRes2 item in lt)

{

string\[\] pts = item.coordinator.Split(' ');

//多边形的顶点

OpenCvSharp.Point\[\] points = new OpenCvSharp.Point\[\]

{

new OpenCvSharp.Point(Convert.ToDouble( pts0), Convert.ToDouble( pts1)),

new OpenCvSharp.Point(Convert.ToDouble( pts2), Convert.ToDouble( pts3)),

new OpenCvSharp.Point(Convert.ToDouble( pts4), Convert.ToDouble( pts5)),

new OpenCvSharp.Point(Convert.ToDouble( pts6), Convert.ToDouble( pts7)),

};

// 绘制多边形

if (isDraw)

{

Cv2.Polylines(image, new OpenCvSharp.Point\[\]\[\] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 3);

}

}

if (pictureBox1.Image != null)

{

pictureBox1.Image.Dispose();

pictureBox1.Image = null;

}

pictureBox1.Image = new Bitmap(image.ToMemoryStream());

image.Dispose();

}

else

{

textBox1.Text = "识别失败";

}

}

private void button3_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

textBox1.Text = "";

Mat image = new Mat(image_path);

Application.DoEvents();

string ocr_result1 = "";

string ocr_result2 = "";

Stopwatch stopwatch = new Stopwatch();

stopwatch.Start();

int res = DL_OCR_Helper.dl_back_ocr2(image, out ocr_result1, out ocr_result2);

stopwatch.Stop();

double totalTime = stopwatch.Elapsed.TotalSeconds;

textBox1.Text += $"耗时: {totalTime:F2}s";

textBox1.Text += "\r\n-------------------\r\n";

if (res == 0)

{

Object jsonObject = JsonConvert.DeserializeObject(ocr_result1.ToString());

textBox1.Text += JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented);

textBox1.Text += "\r\n-------------------\r\n";

Object jsonObject2 = JsonConvert.DeserializeObject(ocr_result2.ToString());

textBox1.Text += JsonConvert.SerializeObject(jsonObject2, Newtonsoft.Json.Formatting.Indented);

List<OcrRes2> lt = JsonConvert.DeserializeObject<List<OcrRes2>>(ocr_result2.ToString());

foreach (OcrRes2 item in lt)

{

string\[\] pts = item.coordinator.Split(' ');

//多边形的顶点

OpenCvSharp.Point\[\] points = new OpenCvSharp.Point\[\]

{

new OpenCvSharp.Point(Convert.ToDouble( pts0), Convert.ToDouble( pts1)),

new OpenCvSharp.Point(Convert.ToDouble( pts2), Convert.ToDouble( pts3)),

new OpenCvSharp.Point(Convert.ToDouble( pts4), Convert.ToDouble( pts5)),

new OpenCvSharp.Point(Convert.ToDouble( pts6), Convert.ToDouble( pts7)),

};

// 绘制多边形

if (isDraw)

{

Cv2.Polylines(image, new OpenCvSharp.Point\[\]\[\] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 3);

}

}

if (pictureBox1.Image != null)

{

pictureBox1.Image.Dispose();

pictureBox1.Image = null;

}

pictureBox1.Image = new Bitmap(image.ToMemoryStream());

image.Dispose();

}

else

{

textBox1.Text = "识别失败";

}

}

private void button6_Click(object sender, EventArgs e)

{

DL_OCR_Helper.InitStatus();

//授权校验 初始化引擎

string key = "";

string licenseKeyPath = Application.StartupPath + "\\license\\license.key";

string licenseFile = Application.StartupPath + "\\license\\license.ini";

int res = -1;

string ini_path = "";

key = File.ReadAllText(licenseKeyPath);

res = DL_OCR_Helper.dl_front_init_license(key, licenseFile, false);

if (res != 0)

{

MessageBox.Show(DL_OCR_Helper.GetMsg(res));

return;

}

res = DL_OCR_Helper.dl_front_create();

if (res != 0)

{

MessageBox.Show("驾驶证正页创建引擎失败!");

return;

}

ini_path = Application.StartupPath + "\\front_resource";

res = DL_OCR_Helper.dl_front_init(ini_path);

if (res != 0)

{

MessageBox.Show(DL_OCR_Helper.GetMsg(res));

return;

}

res = DL_OCR_Helper.dl_back_init_license(key, licenseFile, false);

if (res != 0)

{

MessageBox.Show(DL_OCR_Helper.GetMsg(res));

return;

}

res = DL_OCR_Helper.dl_back_create();

if (res != 0)

{

MessageBox.Show("驾驶证副页创建引擎失败!");

return;

}

ini_path = Application.StartupPath + "\\back_resource";

res = DL_OCR_Helper.dl_back_init(ini_path);

if (res != 0)

{

MessageBox.Show(DL_OCR_Helper.GetMsg(res));

return;

}

MessageBox.Show("初始化成功!");

button1.Enabled = true;

button3.Enabled = true;

}

}

}

复制代码
using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using 驾驶证测试.Commom;

namespace 驾驶证测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        bool isDraw = false;

        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = Application.StartupPath + "\\front_images\\1.png";
            pictureBox1.Image = new Bitmap(image_path);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            image_path = Application.StartupPath + "\\front_images\\1.png";

            Mat image = new Mat(image_path);

            pictureBox1.Image = new Bitmap(image_path);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            image_path = Application.StartupPath + "\\back_images\\1.png";
            pictureBox1.Image = new Bitmap(image_path);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            textBox1.Text = "";
            Application.DoEvents();

            string ocr_result1 = "";
            string ocr_result2 = "";
            Mat image = new Mat(image_path);
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            int res = DL_OCR_Helper.dl_front_ocr2(image, out ocr_result1, out ocr_result2);
            stopwatch.Stop();
            double totalTime = stopwatch.Elapsed.TotalSeconds;
            textBox1.Text += $"耗时: {totalTime:F2}s";
            textBox1.Text += "\r\n-------------------\r\n";

            if (res == 0)
            {
                Object jsonObject = JsonConvert.DeserializeObject(ocr_result1.ToString());
                textBox1.Text += JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented);

                textBox1.Text += "\r\n-------------------\r\n";

                Object jsonObject2 = JsonConvert.DeserializeObject(ocr_result2.ToString());
                textBox1.Text += JsonConvert.SerializeObject(jsonObject2, Newtonsoft.Json.Formatting.Indented);

                List<OcrRes2> lt = JsonConvert.DeserializeObject<List<OcrRes2>>(ocr_result2.ToString());

                foreach (OcrRes2 item in lt)
                {
                    string[] pts = item.coordinator.Split(' ');

                    //多边形的顶点
                    OpenCvSharp.Point[] points = new OpenCvSharp.Point[]
                    {
                        new OpenCvSharp.Point(Convert.ToDouble( pts[0]), Convert.ToDouble( pts[1])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[2]), Convert.ToDouble( pts[3])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[4]), Convert.ToDouble( pts[5])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[6]), Convert.ToDouble( pts[7])),
                    };

                    // 绘制多边形
                    if (isDraw)
                    {
                        Cv2.Polylines(image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 3);

                    }
                }
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                    pictureBox1.Image = null;
                }

                pictureBox1.Image = new Bitmap(image.ToMemoryStream());
                image.Dispose();
            }
            else
            {
                textBox1.Text = "识别失败";
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            textBox1.Text = "";
            Mat image = new Mat(image_path);
            Application.DoEvents();
            string ocr_result1 = "";
            string ocr_result2 = "";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            int res = DL_OCR_Helper.dl_back_ocr2(image, out ocr_result1, out ocr_result2);
            stopwatch.Stop();
            double totalTime = stopwatch.Elapsed.TotalSeconds;
            textBox1.Text += $"耗时: {totalTime:F2}s";
            textBox1.Text += "\r\n-------------------\r\n";
            if (res == 0)
            {
                Object jsonObject = JsonConvert.DeserializeObject(ocr_result1.ToString());
                textBox1.Text += JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented);

                textBox1.Text += "\r\n-------------------\r\n";

                Object jsonObject2 = JsonConvert.DeserializeObject(ocr_result2.ToString());
                textBox1.Text += JsonConvert.SerializeObject(jsonObject2, Newtonsoft.Json.Formatting.Indented);

                List<OcrRes2> lt = JsonConvert.DeserializeObject<List<OcrRes2>>(ocr_result2.ToString());

                foreach (OcrRes2 item in lt)
                {

                    string[] pts = item.coordinator.Split(' ');

                    //多边形的顶点
                    OpenCvSharp.Point[] points = new OpenCvSharp.Point[]
                    {
                        new OpenCvSharp.Point(Convert.ToDouble( pts[0]), Convert.ToDouble( pts[1])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[2]), Convert.ToDouble( pts[3])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[4]), Convert.ToDouble( pts[5])),
                        new OpenCvSharp.Point(Convert.ToDouble( pts[6]), Convert.ToDouble( pts[7])),
                    };

                    // 绘制多边形
                    if (isDraw)
                    {
                        Cv2.Polylines(image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 3);
                    }
                }
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                    pictureBox1.Image = null;
                }
                pictureBox1.Image = new Bitmap(image.ToMemoryStream());
                image.Dispose();
            }
            else
            {
                textBox1.Text = "识别失败";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DL_OCR_Helper.InitStatus();

            //授权校验 初始化引擎
            string key = "";
            string licenseKeyPath = Application.StartupPath + "\\license\\license.key";
            string licenseFile = Application.StartupPath + "\\license\\license.ini";
            int res = -1;
            string ini_path = "";

            key = File.ReadAllText(licenseKeyPath);

            res = DL_OCR_Helper.dl_front_init_license(key, licenseFile, false);
            if (res != 0)
            {
                MessageBox.Show(DL_OCR_Helper.GetMsg(res));
                return;
            }

            res = DL_OCR_Helper.dl_front_create();
            if (res != 0)
            {
                MessageBox.Show("驾驶证正页创建引擎失败!");
                return;
            }

            ini_path = Application.StartupPath + "\\front_resource";
            res = DL_OCR_Helper.dl_front_init(ini_path);
            if (res != 0)
            {
                MessageBox.Show(DL_OCR_Helper.GetMsg(res));
                return;
            }

            res = DL_OCR_Helper.dl_back_init_license(key, licenseFile, false);
            if (res != 0)
            {
                MessageBox.Show(DL_OCR_Helper.GetMsg(res));
                return;
            }

            res = DL_OCR_Helper.dl_back_create();
            if (res != 0)
            {
                MessageBox.Show("驾驶证副页创建引擎失败!");
                return;
            }

            ini_path = Application.StartupPath + "\\back_resource";
            res = DL_OCR_Helper.dl_back_init(ini_path);
            if (res != 0)
            {
                MessageBox.Show(DL_OCR_Helper.GetMsg(res));
                return;
            }
            MessageBox.Show("初始化成功!");
            button1.Enabled = true;
            button3.Enabled = true;
        }
    }
}

下载

C++封装源码下载

C#调用源码下载

SDK下载

相关推荐
WarPigs8 小时前
C# dll笔记
c#
淡笑沐白8 小时前
C# HttpClient完整使用指南
c#·httpclient
JaydenAI8 小时前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
小满Autumn9 小时前
MVVM Light 架构笔记:定位器、命令、消息与 IoC 实践
笔记·学习·架构·c#·上位机·mvvm
小欣加油10 小时前
leetcode3751 范围内总波动值I
java·数据结构·c++·算法·leetcode
代码中介商10 小时前
C++左值与右值:核心判断法则详解
开发语言·c++
玖玥拾10 小时前
C/C++ 基础笔记(七)
c语言·c++
小满Autumn10 小时前
CommunityToolkit.Mvvm 架构笔记:现代 MVVM、源生成器与工程化实践
笔记·架构·c#·.net·wpf·mvvm
加号311 小时前
【C#】 JSON 序列化与反序列化:从入门到最佳实践
c#·json
珊瑚里的鱼11 小时前
手撕单例模式中的饿汉模式和懒汉模式,懒汉模式还要再多加一个C++11版本的
开发语言·c++·单例模式