0
点赞
收藏
分享

微信扫一扫

《c++并发编程》中无锁栈的实现为什么要用双引用计数器

香小蕉 2天前 阅读 3
ocrc++c#

 百度OCR身份证识别C++离线SDKV3.0 C#对接

目录

说明

效果

问题 

项目

代码

下载


说明

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

SDK 简介

        本 SDK 适应于于 Windows 平台下的⾝份证识别系统,⽀持 C++接⼜开发的 SDK,开发者可在VS2015 下⾯进⾏开发(推荐使⽤,不保证其他版本 VS 都兼容)。SDK 采⽤ C++的动态库 DLL 的⽅式,另外随 SDK 附带⼀个鉴权激活⼯具(LicenseTool.exe,在license_tool ⽬录),通过该激活⼯具可⽣成正常接 ⼊SDK 的激活 license ⽂件 license.zip(解压后可⽣成两个⽂件 license.ini 和license.key)达到通过鉴权, 正常使⽤SDK 的⽬的。

激活工具授权

        鉴权采⽤ SDK 附带的鉴权⼯具 LicenseTool.exe、双击打开 exe 后,输⼊申请获取到的授权序列号,执⾏按钮激活后会⽣成⼀个 license.zip ⽂件,把这个⽂件解压后会⽣成 license.ini 和 license.key两个⽂件,把这 2 个⽂件放置到 SDK 的 license ⽂件夹,即可通过授权激活。另外⽀持鉴权⽂件路径定制化及模型⽂件路径定制化,可参考 SDK 示例(鉴权⽂件 license.key 和 license.ini 的路径可参考SDK 代码示例,也可以⽤ SDK 现成的默认路径)。鉴权⼯具 LicenseTool.exe 如下所示,在⼯具中输⼊申请得到的 license 系列号即可⽣成鉴权 zip ⽂件。解压 zip 后可⽣成 license.ini 和 license.key 两个⽂件。

SDK包结构

效果

问题 

返回的坐标位置有问题,猜测可能是内部缩放了图片导致,后续等官方修复。

项目

代码

using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using WinFormTest.Common;

namespace WinFormTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        IntPtr IDCard;
        int res = -1;

        private void Form1_Load(object sender, EventArgs e)
        {
            IDCard = Native.create();

            string key = "";
            string licenseKeyPath = Application.StartupPath + "\\license\\license.key";
            string licenseFile = Application.StartupPath + "\\license\\license.ini";
            key = File.ReadAllText(licenseKeyPath);

            res = Native.auth_from_file(IDCard, key, licenseFile, false);

            string model_folder = Application.StartupPath + "\\resource";
            res = Native.sdk_init(IDCard, model_folder);

            image_path = Application.StartupPath + "\\idcard_.jpg";
            pictureBox1.Image = new Bitmap(image_path);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (image_path == "")
            {
                return;
            }

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

            Mat image = new Mat(image_path);

            StringBuilder ocr_result1 = new StringBuilder(1024);
            StringBuilder ocr_result2 = new StringBuilder(2048);
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            res = Native.ocr2(IDCard, image.CvPtr, ocr_result1, ocr_result2);
            string s = ocr_result1.ToString();
            string s2 = ocr_result2.ToString();
            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);

                IDCardRes iDCardResponse = JsonConvert.DeserializeObject<IDCardRes>(ocr_result1.ToString());
                IDCardCoordRes iDCardCoordResponse = JsonConvert.DeserializeObject<IDCardCoordRes>(ocr_result2.ToString());

                //if (iDCardResponse.name == "") iDCardCoordResponse.name_coord = "";
                //if (iDCardResponse.gender == "") iDCardCoordResponse.gender_coord = "";
                //if (iDCardResponse.ethnicity == "") iDCardCoordResponse.ethnicity_coord = "";
                //if (iDCardResponse.birth == "") iDCardCoordResponse.birth_coord = "";
                //if (iDCardResponse.address == "") iDCardCoordResponse.address_coord = "";
                //if (iDCardResponse.id_number == "") iDCardCoordResponse.id_number_coord = "";
                //if (iDCardResponse.authority == "") iDCardCoordResponse.authority_coord = "";
                //if (iDCardResponse.issuing_date == "") iDCardCoordResponse.issuing_date_coord = "";
                //if (iDCardResponse.expiry_date == "") iDCardCoordResponse.expiry_date_coord = "";

                if (iDCardResponse.name != "")
                {
                    DrawRes(image, iDCardCoordResponse.name_coord);
                }
                if (iDCardResponse.gender != "")
                {
                    DrawRes(image, iDCardCoordResponse.gender_coord);
                }
                if (iDCardResponse.ethnicity != "")
                {
                    DrawRes(image, iDCardCoordResponse.ethnicity_coord);
                }
                if (iDCardResponse.birth != "")
                {
                    DrawRes(image, iDCardCoordResponse.birth_coord);
                }
                if (iDCardResponse.address != "")
                {
                    DrawRes(image, iDCardCoordResponse.address_coord);
                }
                if (iDCardResponse.id_number != "")
                {
                    DrawRes(image, iDCardCoordResponse.id_number_coord);
                }
                if (iDCardResponse.authority != "")
                {
                    DrawRes(image, iDCardCoordResponse.authority_coord);
                }
                if (iDCardResponse.issuing_date != "")
                {
                    DrawRes(image, iDCardCoordResponse.issuing_date_coord);
                }
                if (iDCardResponse.expiry_date != "")
                {
                    DrawRes(image, iDCardCoordResponse.expiry_date_coord);
                }

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

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


        }

        void DrawRes(Mat res_image, string ptsStr)
        {
            string[] pts = ptsStr.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])),
            };
            // 绘制多边形
            Cv2.Polylines(res_image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 3);
        }

        void DrawRes2(Mat res_image, float[] pts)
        {
            //多边形的顶点
            OpenCvSharp.Point[] points = new OpenCvSharp.Point[]
            {
                new OpenCvSharp.Point(pts[0], pts[1]),
                new OpenCvSharp.Point(pts[2], pts[3]),
                new OpenCvSharp.Point(pts[4], pts[5]),
                new OpenCvSharp.Point(pts[6], pts[7]),
            };
            // 绘制多边形
            Cv2.Polylines(res_image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 3);
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string 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 button3_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

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

            Mat image = new Mat(image_path);
            IDCardResponse final_result = new IDCardResponse();
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            res = Native.ocr(IDCard, image.CvPtr, ref final_result);
            stopwatch.Stop();
            double totalTime = stopwatch.Elapsed.TotalSeconds;
            textBox1.Text += $"耗时: {totalTime:F2}s";
            textBox1.Text += "\r\n-------------------\r\n";

            IDCardRes iDCardResponse = new IDCardRes();
            iDCardResponse.name = Encoding.UTF8.GetString(final_result.name).Replace("\u0000", "");
            iDCardResponse.gender = Encoding.UTF8.GetString(final_result.gender).Replace("\u0000", "");
            iDCardResponse.ethnicity = Encoding.UTF8.GetString(final_result.ethnicity).Replace("\u0000", "");
            iDCardResponse.birth = Encoding.UTF8.GetString(final_result.birth).Replace("\u0000", "");
            iDCardResponse.address = Encoding.UTF8.GetString(final_result.address).Replace("\u0000", "");
            iDCardResponse.id_number = Encoding.UTF8.GetString(final_result.id_number).Replace("\u0000", "");
            iDCardResponse.authority = Encoding.UTF8.GetString(final_result.authority).Replace("\u0000", "");
            iDCardResponse.issuing_date = Encoding.UTF8.GetString(final_result.issuing_date).Replace("\u0000", "");
            iDCardResponse.expiry_date = Encoding.UTF8.GetString(final_result.expiry_date).Replace("\u0000", "");

            textBox1.Text += JsonConvert.SerializeObject(iDCardResponse, Newtonsoft.Json.Formatting.Indented);
            textBox1.Text += "\r\n-------------------\r\n";

            IDCardCoordRes2 iDCardCoordRes2 = new IDCardCoordRes2();
            iDCardCoordRes2.name_coord = final_result.name_coord;
            iDCardCoordRes2.gender_coord = final_result.gender_coord;
            iDCardCoordRes2.birth_coord = final_result.birth_coord;
            iDCardCoordRes2.address_coord = final_result.address_coord;
            iDCardCoordRes2.id_number_coord = final_result.id_number_coord;
            iDCardCoordRes2.ethnicity_coord = final_result.ethnicity_coord;
            iDCardCoordRes2.authority_coord = final_result.authority_coord;
            iDCardCoordRes2.issuing_date_coord = final_result.issuing_date_coord;
            iDCardCoordRes2.expiry_date_coord = final_result.expiry_date_coord;

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

            DrawRes2(image, iDCardCoordRes2.name_coord);
            DrawRes2(image, iDCardCoordRes2.gender_coord);
            DrawRes2(image, iDCardCoordRes2.birth_coord);
            DrawRes2(image, iDCardCoordRes2.address_coord);
            DrawRes2(image, iDCardCoordRes2.id_number_coord);
            DrawRes2(image, iDCardCoordRes2.ethnicity_coord);
            DrawRes2(image, iDCardCoordRes2.authority_coord);
            DrawRes2(image, iDCardCoordRes2.issuing_date_coord);
            DrawRes2(image, iDCardCoordRes2.expiry_date_coord);

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

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

下载

C++封装源码下载

C#调用源码下载

SDK下载

举报

相关推荐

0 条评论