一府蛇、平臺(tái):.net 6.0 控制臺(tái)應(yīng)用程序
二、插件安裝
EPPlus 7.2.1
OpenCvSharp4.runtime.win 4.10.0.20240616
Sdcb.PaddleInference 2.5.0.1
Sdcb.PaddleInference.runtime.win64.mkl 2.6.1
Sdcb.PaddleOCR 2.7.0.3
Sdcb.PaddleOCR.Models.Local 2.7.0
System.Drawing.Common 8.0.7
三、實(shí)現(xiàn)代碼
using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR.Models.Local;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR;
using System.Diagnostics;
using System;
using System.Drawing;
using System.IO;
using OfficeOpenXml;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("【啟動(dòng)圖片掃描程序】");
Console.WriteLine("請(qǐng)指定一個(gè)文件根目錄:");
string rootFolder = Console.ReadLine();
if (!Directory.Exists(rootFolder))
{
Console.WriteLine("【錯(cuò)誤】路徑不存在...");
return;
}
try
{
Console.WriteLine("開始遍歷文件夾...");
string runDir = AppDomain.CurrentDomain.BaseDirectory;
string savePath = $"{runDir}output.xlsx";
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("結(jié)果表");
var sheet2 = package.Workbook.Worksheets.Add("錯(cuò)誤表");
sheet1.SetValue(1, 1, "文件夾名稱");
sheet1.SetValue(1, 2, "圖片名稱");
sheet1.SetValue(1, 3, "時(shí)間");
sheet1.SetValue(1, 4, "經(jīng)度");
sheet1.SetValue(1, 5, "緯度");
sheet1.SetValue(1, 6, "地址");
sheet1.SetValue(1, 7, "路徑");
sheet2.SetValue(1, 1, "文件夾名稱");
sheet2.SetValue(1, 2, "圖片名稱");
sheet2.SetValue(1, 3, "路徑");
sheet2.SetValue(1, 4, "識(shí)別錯(cuò)誤原因");
TraverseDirectory(rootFolder, sheet1, sheet2, 2, 2);
package.SaveAs(savePath);
}
Console.WriteLine("識(shí)別完成,請(qǐng)查看輸出文件!");
}
catch (Exception ex)
{
Console.WriteLine();
}
}
static void TraverseDirectory(string path, ExcelWorksheet sheet1, ExcelWorksheet sheet2, int rowIndex1, int rowIndex2)
{
var rdir = new DirectoryInfo(path);
Console.WriteLine($"***【文件夾{rdir.Name}】***");
foreach (var fileInfo in rdir.GetFiles())
{
if (fileInfo.Extension == ".jpg" || fileInfo.Extension == ".png")
{
Console.WriteLine($"正在識(shí)別文件:{fileInfo.Name}");
try
{
FullOcrModel model = LocalFullModels.ChineseV3;
using (PaddleOcrAll all = new PaddleOcrAll(model, PaddleDevice.Mkldnn())
{
AllowRotateDetection = false,
Enable180Classification = false,
})
{
using (Mat src = Cv2.ImRead(fileInfo.FullName))
{
PaddleOcrResult result = all.Run(src);
// 此處是我自定義將文字內(nèi)容提取的方法芋类,可以自己替換
var txt1 = result.Text.Replace("\r", "").Replace("\n", "").Replace(":", ":");
var index = txt1.IndexOf("間:");
if (index > -1)
{
var txt2 = txt1.Substring(index);
var i1 = txt2.IndexOf("度:");
var i2 = txt2.LastIndexOf("度:");
var i3 = txt2.IndexOf("址:");
sheet1.SetValue(rowIndex1, 1, rdir.Name);
sheet1.SetValue(rowIndex1, 2, fileInfo.Name);
sheet1.SetValue(rowIndex1, 3, txt2.Substring(2, i1 - 2));
sheet1.SetValue(rowIndex1, 4, txt2.Substring(i1 + 2, i2 - i1 - 2));
sheet1.SetValue(rowIndex1, 5, txt2.Substring(i2 + 2, i3 - i2 - 2));
sheet1.SetValue(rowIndex1, 6, txt2.Substring(i3 + 2));
sheet1.SetValue(rowIndex1, 7, fileInfo.FullName);
rowIndex1 += 1;
Console.WriteLine($"識(shí)別成功...");
//Console.WriteLine($"時(shí)間:{txt2.Substring(2, i1 - 2)}");
//Console.WriteLine($"經(jīng)度:{txt2.Substring(i1 + 2, i2 - i1 - 2)}");
//Console.WriteLine($"緯度:{txt2.Substring(i2 + 2, i3 - i2 - 2)}");
//Console.WriteLine($"地址:{txt2.Substring(i3 + 2)}");
}
}
}
}
catch(Exception ex)
{
sheet2.SetValue(rowIndex2, 1, rdir.Name);
sheet2.SetValue(rowIndex2, 2, fileInfo.Name);
sheet2.SetValue(rowIndex2, 3, fileInfo.FullName);
sheet2.SetValue(rowIndex2, 4, ex.Message);
rowIndex2 += 1;
Console.WriteLine($"識(shí)別異常:{ex.Message}");
}
}
}
foreach (string dir in Directory.GetDirectories(path))
{
TraverseDirectory(dir, sheet1, sheet2, rowIndex1, rowIndex2);
}
}
}
}
效果:
原圖: