當前位置: 妍妍網 > 碼農

C# PaddleOCR 單字辨識效果

2024-06-02碼農

效果

說明

根據【百度辦公文件辨識C++離線SDKV1.2使用者接入文件.pdf】,使用C++封裝DLL,C#呼叫。

背景

為使客戶、第三方開發者等能夠更快速、方便的接入使用百度辦公文件辨識 SDK、促進百度 OCR產品賦能更多客戶,特設計支持 c++語言的 Windows 高精通用文字辨識 SDK,該 SDK 提供 pdf 轉圖文的能力和透過 pdf 辨識文字並可以轉存成 word 的能力。

SDK 簡介

本 SDK 適應於 Windows 平台下的人臉辨識系統, ,開發者可在 vs2015 下⾯進⾏開發(推薦使⽤,不保證其他版本 vs 都相容)。SDK 采⽤ c++的動態庫 dll 的⽅式。上層 UI 框架支持主流框架如QT,MFC 等。

自動批次授權

鑒權采用自動啟用的方式進行授權,可參考 SDK 範例中,把申請到的授權 key 串碼(僅支持批次授權)填入到 license 資料夾的 license.key 檔中,執行 SDK,即可自動啟用生成授權檔 license.ini 在license 資料夾中。SDK 授權是透過介面方法 auth_from_file 實作,該方法參數分別是傳入授權 key 的串碼和授權檔 license.ini 的絕對路徑。確保參數正確後,在 SDK 中執行了該方法,就會生成授權license.ini 檔。若授權正確,該方法的返回值為 0,若非 0,則為授權失敗,錯誤原因可根據錯誤碼參考後續文件檢視。

離線授權

離線授權,采用從 sdk 附帶的 license_tool 工具,bin 資料夾的 license_tool 下,雙擊 LicenseTool.exe,再點選拷貝,把裝置指紋拷貝到剪貼簿中,到百度 OCR 官網進行離線啟用,填入得到的裝置指紋後,從官網下載離線授權檔,解壓,形成 license.key 和 license.ini 兩個檔,替換到 SDK 中的 license 資料夾中,執行 SDK,若在 SDK 的授權方法 auth_from_file 中返回 0,則為透過了授權。(具體可參考SDK 中的授權程式碼範例)


其他



計畫

程式碼

using HightOCRTest.Common;
using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace HightOCRTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
bool isDraw = false;
static IntPtr engine;
private void button6_Click(object sender, EventArgs e)
{
//授權校驗 初始化引擎
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 = Native.init_license(key, licenseFile);
if (res != 0)
{
MessageBox.Show(res.ToString());
return;
}
engine = Native.create();
if (engine == null)
{
MessageBox.Show("建立引擎失敗!");
return;
}
ini_path = Application.StartupPath + "\\resource";
res = Native.init(engine, "", 6);
if (res != 0)
{
MessageBox.Show(res.ToString());
return;
}
MessageBox.Show("初始化成功!");
button1.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button6.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
//image_path = Application.StartupPath + "\\images\\1.jpg";
image_path = Application.StartupPath + "\\test2.jpg";
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 = "";
}
StringBuilder ocr_result_texts = new StringBuilder(1024 * 10);
StringBuilder ocr_result_words = new StringBuilder(1024 * 100);
private void button1_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
textBox1.Text = "";
Application.DoEvents();
ocr_result_texts.Clear();
ocr_result_words.Clear();
Mat image = new Mat(image_path);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int res = Native.ocr(engine, image.CvPtr, ocr_result_texts, ocr_result_words);
stopwatch.Stop();
double totalTime = stopwatch.Elapsed.TotalSeconds;
textBox1.Text += $"耗時: {totalTime:F2}s";
textBox1.Text += "\r\n-------------------\r\n";
if (res == 0)
{
textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented);
textBox1.Text += "\r\n-------------------\r\n";
textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented);
}
else
{
textBox1.Text = "辨識失敗";
}
}
//繪制文字區域
private void button3_Click(object sender, EventArgs e)
{
if (ocr_result_texts.Length == 0)
{
return;
}
Mat image = new Mat(image_path);
List<OcrResTexts> lt = JsonConvert.DeserializeObject<List<OcrResTexts>>(ocr_result_texts.ToString());
foreach (OcrResTexts 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])),
};
// 繪制多邊形
Cv2.Polylines(image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 2);
}
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
pictureBox1.Image = new Bitmap(image.ToMemoryStream());
image.Dispose();
}
//繪制單字區域
private void button4_Click(object sender, EventArgs e)
{
if (ocr_result_words.Length == 0)
{
return;
}
Mat image = new Mat(image_path);
List<OcrResWords> lt = JsonConvert.DeserializeObject<List<OcrResWords>>(ocr_result_words.ToString());
foreach (OcrResWords item in lt)
{
string[] pts = item.coordinator.Split(' ');
//left top width height
OpenCvSharp.Rect rect = new Rect((int)Convert.ToDouble(pts[0]), (int)Convert.ToDouble(pts[1]), (int)Convert.ToDouble(pts[2]), (int)Convert.ToDouble(pts[3]));
Cv2.Rectangle(image, rect, color: new Scalar(255, 0, 0), thickness: 1);
}
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
pictureBox1.Image = new Bitmap(image.ToMemoryStream());
image.Dispose();
}
//辨識小語種→
private void button5_Click(object sender, EventArgs e)
{
//if (image_path == "")
//{
// return;
//}
//textBox1.Text = "";
//Application.DoEvents();
//ocr_result_texts.Clear();
//ocr_result_words.Clear();
//Mat image = new Mat(image_path);
//Stopwatch stopwatch = new Stopwatch();
//stopwatch.Start();
//int res = Native.ocr_other(engine, image.CvPtr, ocr_result_texts, ocr_result_words);
//stopwatch.Stop();
//double totalTime = stopwatch.Elapsed.TotalSeconds;
//textBox1.Text += $"耗時: {totalTime:F2}s";
//textBox1.Text += "\r\n-------------------\r\n";
//if (res == 0)
//{
// textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented);
// textBox1.Text += "\r\n-------------------\r\n";
// textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented);
//}
//else
//{
// textBox1.Text = "辨識失敗";
//}
}
}
}