detect/detect.gui/Services/OCRService.cs

166 lines
6.1 KiB
C#
Raw Normal View History

2025-02-07 12:16:01 +08:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
2025-04-21 13:08:40 +08:00
using System.Text.RegularExpressions;
2025-02-07 12:16:01 +08:00
using System.Text.Unicode;
using System.Threading.Tasks;
using detect.gui.Models;
using PaddleOCRSharp;
namespace detect.gui.Services;
public class OCRService
{
private readonly PaddleOCREngine? _engine;
public OCRService()
{
OCRModelConfig? config = null;
var parameter = new OCRParameter();
_engine = new PaddleOCREngine(config, parameter);
}
2025-04-23 15:55:51 +08:00
public Task<string> ImportByOCR(string base64ImageString)
2025-02-07 12:16:01 +08:00
{
return Task.Run(() =>
{
2025-04-23 15:55:51 +08:00
var image = GetImage(base64ImageString);
if (image is null) return "";
var result = _engine?.DetectText(image);
2025-02-07 12:16:01 +08:00
if (result == null) return "";
var resultList = JsonHelper.DeserializeObject<List<OCRResultEntity>>(result.JsonText);
resultList = resultList.Select((d, index) =>
{
d.Index = index;
return d;
}).ToList();
2025-04-29 12:15:18 +08:00
// 获取预埋件信息
2025-04-23 15:55:51 +08:00
var tempList = resultList.Where(d => !string.IsNullOrEmpty(d.Text) &&
2025-04-29 12:15:18 +08:00
(Enumerable.Range('A', 'Z' - 'A' + 1)
.Select(i => "P" + (char)i)
.Any(substring => d.Text.ToUpper().Contains(substring)) ||
d.Text.ToUpper().Contains("VB")))
2025-04-23 15:55:51 +08:00
.ToList();
2025-02-07 12:16:01 +08:00
var data = new List<dynamic>();
foreach (var item in tempList)
{
try
{
2025-03-05 11:41:37 +08:00
var w = "0";
var h = "0";
var type = ReplaceChars(resultList[item.Index + 1].Text);
if (string.IsNullOrWhiteSpace(type)) continue;
2025-04-21 13:08:40 +08:00
type = Regex.Replace(type, "[A-Z]", string.Empty);
if (type.Contains('-', StringComparison.CurrentCultureIgnoreCase))
2025-03-05 11:41:37 +08:00
{
2025-04-21 13:08:40 +08:00
type = type.Split("-")[0];
if (type.Contains('x', StringComparison.CurrentCultureIgnoreCase))
2025-03-05 11:41:37 +08:00
{
2025-04-23 11:00:11 +08:00
w = type.Split("x")[0];
h = type.Split("x")[1];
2025-04-21 13:08:40 +08:00
}
else
{
w = type;
h = type;
}
2025-03-05 11:41:37 +08:00
}
2025-04-23 15:55:51 +08:00
else if (type.Contains('x', StringComparison.CurrentCultureIgnoreCase))
{
w = type.Split("x")[0];
h = type.Split("x")[1];
}
2025-03-05 11:41:37 +08:00
else
2025-04-21 13:08:40 +08:00
continue;
2025-02-07 12:16:01 +08:00
data.Add(new
{
2025-04-23 15:55:51 +08:00
sn = ReplaceChars(resultList[item.Index - 1].Text),
2025-02-14 14:09:10 +08:00
code = ReplaceChars(resultList[item.Index].Text),
type = ReplaceChars(resultList[item.Index + 1].Text),
2025-02-07 12:16:01 +08:00
x = ReplaceChars(resultList[item.Index + 2].Text),
y = ReplaceChars(resultList[item.Index + 3].Text),
center = ReplaceChars(resultList[item.Index + 4].Text),
2025-03-05 11:41:37 +08:00
w = w,
h = h,
2025-02-07 12:16:01 +08:00
angle = ReplaceChars(resultList[item.Index + 5].Text),
});
}
2025-04-29 12:15:18 +08:00
catch {} // 出错则忽略
2025-02-07 12:16:01 +08:00
}
2025-04-23 15:55:51 +08:00
2025-02-07 12:16:01 +08:00
return data.Count > 0
? JsonSerializer.Serialize(data,
new JsonSerializerOptions
{ WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) })
: "";
});
}
private string ReplaceChars(string? text)
{
2025-02-14 14:09:10 +08:00
return string.IsNullOrEmpty(text) ? "" : text.Replace("\u00d7", "x").Replace("_", "").Replace(",", "").Replace(":", "").Replace(";", "").Replace(" ", "").Replace("", "");
2025-02-07 12:16:01 +08:00
}
2025-04-29 12:15:18 +08:00
// 根据类型 返回图片
2025-04-23 15:55:51 +08:00
private Image? GetImage(string base64ImageString)
2025-02-07 12:16:01 +08:00
{
var imageType = System.Drawing.Imaging.ImageFormat.Gif;
if (base64ImageString.IndexOf("data:image/jpeg", StringComparison.Ordinal) >= 0)
{
imageType = System.Drawing.Imaging.ImageFormat.Jpeg;
base64ImageString = base64ImageString.Replace("data:image/jpeg;base64,", "");
2025-04-23 15:55:51 +08:00
// fileName += ".jpg";
2025-02-07 12:16:01 +08:00
}
if (base64ImageString.IndexOf("data:image/png", StringComparison.Ordinal) >= 0)
{
imageType = System.Drawing.Imaging.ImageFormat.Png;
base64ImageString = base64ImageString.Replace("data:image/png;base64,", "");
2025-04-23 15:55:51 +08:00
// fileName += ".png";
2025-02-07 12:16:01 +08:00
}
if (base64ImageString.IndexOf("data:image/bmp", StringComparison.Ordinal) >= 0)
{
imageType = System.Drawing.Imaging.ImageFormat.Bmp;
base64ImageString = base64ImageString.Replace("data:image/bmp;base64,", "");
2025-04-23 15:55:51 +08:00
// fileName += ".bmp";
2025-02-07 12:16:01 +08:00
}
if (Equals(imageType, System.Drawing.Imaging.ImageFormat.Gif))
{
2025-04-23 15:55:51 +08:00
return null;
2025-02-07 12:16:01 +08:00
}
try
{
var imageBytes = Convert.FromBase64String(base64ImageString.Replace("data:image/jpeg;base64,", ""));
using var ms = new MemoryStream(imageBytes);
var image = System.Drawing.Image.FromStream(ms);
2025-04-23 15:55:51 +08:00
using var bmpStream = new MemoryStream();
image.Save(bmpStream, imageType);
return image;
2025-02-07 12:16:01 +08:00
}
catch
{
2025-04-23 15:55:51 +08:00
return null;
2025-02-07 12:16:01 +08:00
}
}
public class OCRResultEntity
{
public int Index { get; set; }
public List<BoxPoint>? BoxPoints { get; set; }
public double Score { get; set; }
public string? Text { get; set; }
public int cls_label { get; set; }
public double cls_score { get; set; }
}
public class BoxPoint
{
public int X { get; set; }
public int Y { get; set; }
}
}