update paddle ocr 6.0.0

This commit is contained in:
2025-12-23 16:27:02 +08:00
parent fed944a3cf
commit b460906b45
7 changed files with 244 additions and 100 deletions

View File

@@ -563,30 +563,9 @@ namespace TetraPackOCR
try
{
//模型初始化
OCRModelConfig config = new OCRModelConfig(); //创建一个模型参数设置对象
string rootPath = System.IO.Path.GetDirectoryName(typeof(OCRModelConfig).Assembly.Location); //程序的根目录
config.det_infer = rootPath + @"\Data\OCRModel\det_infer"; //文字检测模型路径
config.cls_infer = rootPath + @"\Data\OCRModel\cls_infer"; //文本角度模型路径
config.rec_infer = rootPath + @"\Data\OCRModel\rec_infer"; //文字内容识别模型路径
//以上三个模型参数的文件路径只需要写到存放的文件夹名称
//对于字典来说需要写全加上后缀
config.keys = rootPath + @"\Data\OCRModel\keys\ppocr_keys.txt"; //词典路径
//OCR参数设置
OCRParameter OcrParameter = new OCRParameter(); //创建一个检测参数设置对象
OcrParameter.cpu_math_library_num_threads = 8;//预测并发线程数
OcrParameter.enable_mkldnn = true;//web部署该值建议设置为0,否则出错内存如果使用很大建议该值也设置为0.
OcrParameter.cls = true; //是否执行文字方向分类默认false
OcrParameter.det = true;//是否开启方向检测用于检测识别180旋转
OcrParameter.rec = true;
OcrParameter.use_angle_cls = true;//是否开启方向检测用于检测识别180旋转
OcrParameter.det_db_score_mode = true;//是否使用多段线,即文字区域是用多段线还是用矩形,
OcrParameter.det_db_unclip_ratio = 1.6f;
OcrParameter.max_side_len = 4600;
OCRModelConfig config = null;
//初始化OCR
Engine = new PaddleOCREngine(config, OcrParameter);
Engine = new PaddleOCREngine(config, "");
log.Info("OCR模型初始化完成");
}
catch (Exception ex)
@@ -1341,67 +1320,91 @@ namespace TetraPackOCR
NoticePLCCompleteOCR();
return;
}
Mat hsv = new Mat();
Cv2.CvtColor(bgr, hsv, ColorConversionCodes.BGR2HSV); // 1. 转 HSV
Mat[] channels = Cv2.Split(hsv); // 2. 拆通道
Mat h = channels[0]; // Hue 0-180
Mat s = channels[1]; // Saturation 0-255
Mat v = channels[2]; // Value 0-255
bool matchOK = false;
OCRTextResult rgbResult = null;
List<PointF[]> pointRGB = null;
rgbResult = OCRBytes(bgr.ToBytes());
if (rgbResult == null)
{
log.Error("彩色通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text = rgbResult.text;
log.Info("彩色通道字符读取结果:" + rgbResult.text);
matchOK = rgbResult.match;
pointRGB = rgbResult.points;
Mat hsv = null;
OCRTextResult hResult = null;
OCRTextResult sResult = null;
OCRTextResult vResult = null;
bool matchOK = false;
List<PointF[]> pointS = null;
List<PointF[]> pointH = null;
List<PointF[]> pointV = null;
sResult = OCRBytes(s.ImEncode(".png"));
if (sResult == null)
if (!matchOK)
{
log.Error("s通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text = sResult.text;
log.Info("s通道字符读取结果" + sResult.text);
matchOK = sResult.match;
pointS = sResult.points;
hsv = new Mat();
Cv2.CvtColor(bgr, hsv, ColorConversionCodes.BGR2HSV); // 1. 转 HSV
Mat[] channels = Cv2.Split(hsv); // 2. 拆通道
Mat h = channels[0]; // Hue 0-180
Mat s = channels[1]; // Saturation 0-255
Mat v = channels[2]; // Value 0-255
if (!matchOK)
{
hResult = OCRBytes(h.ImEncode(".png"));
if (hResult == null)
sResult = OCRBytes(s.ImEncode(".png"));
if (sResult == null)
{
log.Error("h通道OCR字符识别失败");
log.Error("s通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text += "\n" + hResult.text;
log.Info("h通道字符读取结果:" + hResult.text);
matchOK = hResult.match;
pointH = hResult.points;
}
if (!matchOK)
{
vResult = OCRBytes(v.ImEncode(".png"));
if (vResult == null)
txt_readOcrResultShow.Text = "\n" + sResult.text;
log.Info("s通道字符读取结果:" + sResult.text);
matchOK = sResult.match;
pointS = sResult.points;
if (!matchOK)
{
log.Error("v通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
hResult = OCRBytes(h.ImEncode(".png"));
if (hResult == null)
{
log.Error("h通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text += "\n" + hResult.text;
log.Info("h通道字符读取结果" + hResult.text);
matchOK = hResult.match;
pointH = hResult.points;
}
if (!matchOK)
{
vResult = OCRBytes(v.ImEncode(".png"));
if (vResult == null)
{
log.Error("v通道OCR字符识别失败");
NoticePLCCompleteOCR();
return;
}
txt_readOcrResultShow.Text += "\n" + vResult.text;
log.Info("v通道字符读取结果" + vResult.text);
matchOK = vResult.match;
pointV = vResult.points;
}
txt_readOcrResultShow.Text += "\n" + vResult.text;
log.Info("v通道字符读取结果" + vResult.text);
matchOK = vResult.match;
pointV = vResult.points;
}
byte[] ocrimagebyte = File.ReadAllBytes(file.FileName);
Bitmap Bmp = new Bitmap(new MemoryStream(ocrimagebyte));
if ((pointS != null && pointS.Count > 0) || (pointH != null && pointH.Count > 0) || (pointV != null && pointV.Count > 0))
if ((pointRGB != null && pointRGB.Count > 0) || (pointS != null && pointS.Count > 0) || (pointH != null && pointH.Count > 0) || (pointV != null && pointV.Count > 0))
{
using (Graphics g = Graphics.FromImage(Bmp))
{
if (pointRGB != null && pointRGB.Count > 0)
{
foreach (var item in pointRGB)
g.DrawPolygon(new Pen(Brushes.Red, 2), item.Select(x => new PointF() { X = x.X, Y = x.Y }).ToArray());
}
if (pointS != null && pointS.Count > 0)
{
foreach (var item in pointS)
@@ -1556,6 +1559,7 @@ namespace TetraPackOCR
{
readOCR[i] = readOCR[i].Replace("_", "");
readOCR[i] = readOCR[i].Replace(" ", "");
readOCR[i] = readOCR[i].Replace(".", "");
}
MatchCollection matchResults;