Commit 5bd32f6f 刘韬

修改ocr定位 移动到条码左侧中间

1 个父辈 d035a2e3
...@@ -143,6 +143,9 @@ namespace BLL ...@@ -143,6 +143,9 @@ namespace BLL
} }
if (!string.IsNullOrEmpty(keydata) && !AllData.ContainsKey(keydata)) if (!string.IsNullOrEmpty(keydata) && !AllData.ContainsKey(keydata))
AllData.Add(keydata, rowdata); AllData.Add(keydata, rowdata);
if (string.IsNullOrEmpty(keydata))
emptyrow++;
currow++; currow++;
} }
} }
......
...@@ -119,13 +119,14 @@ class NiRenIO: IO_Interface ...@@ -119,13 +119,14 @@ class NiRenIO: IO_Interface
iomonitorrun = true; iomonitorrun = true;
while (iomonitorrun && systemrun) while (iomonitorrun && systemrun)
{ {
Thread.Sleep(2); Thread.Sleep(3);
try try
{ {
ReadDO(); ReadDO();
} }
catch (SocketException) catch (SocketException)
{ {
readerrtimes = 0;
if (tcpClient.Connected) if (tcpClient.Connected)
tcpClient.Close(); tcpClient.Close();
ConnectionState_Event?.Invoke(this, false); ConnectionState_Event?.Invoke(this, false);
...@@ -160,7 +161,7 @@ class NiRenIO: IO_Interface ...@@ -160,7 +161,7 @@ class NiRenIO: IO_Interface
try try
{ {
tcpClient.Client.Send(sendbyte); tcpClient.Client.Send(sendbyte);
Thread.Sleep(1); Thread.Sleep(2);
byte[] result = new byte[100]; byte[] result = new byte[100];
var ulength = tcpClient.Client.Receive(result); var ulength = tcpClient.Client.Receive(result);
if (ulength > 0) { if (ulength > 0) {
...@@ -190,6 +191,7 @@ class NiRenIO: IO_Interface ...@@ -190,6 +191,7 @@ class NiRenIO: IO_Interface
public bool[] DOstate = new bool[IoCount]; public bool[] DOstate = new bool[IoCount];
bool[] lastDIstate = new bool[IoCount]; bool[] lastDIstate = new bool[IoCount];
bool[] lastDOstate = new bool[IoCount]; bool[] lastDOstate = new bool[IoCount];
int readerrtimes = 0;
/// <summary> /// <summary>
/// 读全部IO /// 读全部IO
/// </summary> /// </summary>
...@@ -201,7 +203,7 @@ class NiRenIO: IO_Interface ...@@ -201,7 +203,7 @@ class NiRenIO: IO_Interface
const string CMD_ReadALL = CMD_ReadDI + CMD_ReadD0; const string CMD_ReadALL = CMD_ReadDI + CMD_ReadD0;
var sendbyte = Encoding.ASCII.GetBytes(CMD_ReadALL); var sendbyte = Encoding.ASCII.GetBytes(CMD_ReadALL);
Debug.WriteLine("CMD_ReadALL:"+ sendbyte); Debug.WriteLine("CMD_ReadALL:"+ CMD_ReadALL);
var resultbuffer = new byte[2048]; var resultbuffer = new byte[2048];
int ulength = 0; int ulength = 0;
lock (tcpClient) lock (tcpClient)
...@@ -209,14 +211,21 @@ class NiRenIO: IO_Interface ...@@ -209,14 +211,21 @@ class NiRenIO: IO_Interface
try try
{ {
tcpClient.Client.Send(sendbyte); tcpClient.Client.Send(sendbyte);
Thread.Sleep(1); Thread.Sleep(5);
ulength = tcpClient.Client.Receive(resultbuffer); ulength = tcpClient.Client.Receive(resultbuffer);
readerrtimes = 0;
} }
catch (SocketException se) catch (SocketException se)
{ {
readerrtimes++;
Thread.Sleep(100);
if (readerrtimes > 5)
{
Debug.WriteLine("Read IO Error");
throw se; throw se;
} }
return;
}
} }
if (ulength == 0) { if (ulength == 0) {
return; return;
...@@ -320,6 +329,14 @@ class NiRenIO: IO_Interface ...@@ -320,6 +329,14 @@ class NiRenIO: IO_Interface
public bool WriteDO(int add, Status sta) public bool WriteDO(int add, Status sta)
{ {
return WriteDO(add, sta == Status.On); for (int i = 0; i < 10; i++)
{
if (WriteDO(add, sta == Status.On))
{
return true;
}
Thread.Sleep(100);
}
return false;
} }
} }
...@@ -4,6 +4,8 @@ using System.Drawing; ...@@ -4,6 +4,8 @@ using System.Drawing;
using System.IO; using System.IO;
using System.Xml; using System.Xml;
using CameraVisionLib.Model; using CameraVisionLib.Model;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Math;
using Model; using Model;
using Newtonsoft.Json; using Newtonsoft.Json;
...@@ -134,10 +136,12 @@ namespace BLL ...@@ -134,10 +136,12 @@ namespace BLL
/// <returns></returns> /// <returns></returns>
public bool MatchingTemplate(List<BarcodeInfo> code, string firstMaterial,bool analyisMode, out string mateName, out Dictionary<string, string> keyword, out bool[] isCodeUsed) public bool MatchingTemplate(List<BarcodeInfo> code, string firstMaterial,bool analyisMode, out string mateName, out Dictionary<string, string> keyword, out bool[] isCodeUsed)
{ {
if (Directory.Exists("ocr"))
Directory.Delete("ocr", true);
mateName = ""; mateName = "";
keyword = null; keyword = null;
isCodeUsed = null; isCodeUsed = null;
code.ForEach((c)=> { code.ForEach((c)=> {
LogNet.log.Info($"扫描到 {c.CodeType},{c.Text}"); LogNet.log.Info($"扫描到 {c.CodeType},{c.Text}");
}); });
...@@ -416,6 +420,7 @@ namespace BLL ...@@ -416,6 +420,7 @@ namespace BLL
LogNet.log.Info("OCR匹配 " + ocrlist.Count); LogNet.log.Info("OCR匹配 " + ocrlist.Count);
if (ocrlist.Count > 0) if (ocrlist.Count > 0)
{ {
LogNet.log.Info("开始OCR匹配 "); LogNet.log.Info("开始OCR匹配 ");
//增加ocr匹配 //增加ocr匹配
// OCR oCR = new(); // OCR oCR = new();
...@@ -425,7 +430,7 @@ namespace BLL ...@@ -425,7 +430,7 @@ namespace BLL
matchCount += codeMatch.Count; matchCount += codeMatch.Count;
Bitmap bmp = CodeOcr(mateTemp[index].Name,ocrcode[i], ocrlist[i], CurrntBitmap); Bitmap bmp = CodeOcr(mateTemp[index].Name,ocrcode[i], ocrlist[i], CurrntBitmap,out string ocrname);
bool algro = ConfigHelper.Config.Get("UsePaddleOCR", false); bool algro = ConfigHelper.Config.Get("UsePaddleOCR", false);
string codeOcr = ""; string codeOcr = "";
if (algro) if (algro)
...@@ -452,7 +457,7 @@ namespace BLL ...@@ -452,7 +457,7 @@ namespace BLL
#endregion ocr文字提取结束 #endregion ocr文字提取结束
} }
LogNet.log.Info($"OCR匹配 {ocrname} = {codeOcr}");
var x = new BarcodeInfo() {Text=codeOcr,CodeType="OCR"}; var x = new BarcodeInfo() {Text=codeOcr,CodeType="OCR"};
Dictionary<string, string> matchKey = CodeMatch(x, codeMatch); Dictionary<string, string> matchKey = CodeMatch(x, codeMatch);
if (matchKey != null) if (matchKey != null)
...@@ -477,8 +482,9 @@ namespace BLL ...@@ -477,8 +482,9 @@ namespace BLL
} }
} }
private Bitmap CodeOcr(string templatename, BarcodeInfo code, MaterialCodeOCR ocr, Bitmap codeImage) private Bitmap CodeOcr(string templatename, BarcodeInfo code, MaterialCodeOCR ocr, Bitmap codeImage,out string ocrname)
{ {
ocrname = "";
int smside = ocr.Width < ocr.Height ? ocr.Width : ocr.Height; int smside = ocr.Width < ocr.Height ? ocr.Width : ocr.Height;
smside = (int)(smside * 0.2); smside = (int)(smside * 0.2);
float OffsetX = ocr.OffsetX - smside; float OffsetX = ocr.OffsetX - smside;
...@@ -486,8 +492,15 @@ namespace BLL ...@@ -486,8 +492,15 @@ namespace BLL
int Width = ocr.Width + smside * 2; int Width = ocr.Width + smside * 2;
int Height = ocr.Height + smside * 2; int Height = ocr.Height + smside * 2;
var a = code.Angle >= 0 ? 180+code.Angle*-1 : 180 + Math.Abs(code.Angle);
double centerX = code.Center.X; double centerX = code.Center.X;
double centerY = code.Center.Y; double centerY = code.Center.Y;
var cent = PointWithAngle(code.Center, a, code.Size.Width/2);
centerX=cent.X;
centerY=cent.Y;
double aa = Math.Atan2(centerY, centerX); double aa = Math.Atan2(centerY, centerX);
aa = aa * 180 / Math.PI; //转角度 aa = aa * 180 / Math.PI; //转角度
double cc = Math.Sqrt(centerX * centerX + centerY * centerY); double cc = Math.Sqrt(centerX * centerX + centerY * centerY);
...@@ -515,10 +528,10 @@ namespace BLL ...@@ -515,10 +528,10 @@ namespace BLL
g.Dispose(); g.Dispose();
ocrRotate.Save(@"ocr.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); ocrRotate.Save(@"ocr.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Directory.CreateDirectory("ocr"); Directory.CreateDirectory("ocr");
ocrRotate.Save($"ocr\\{templatename}_{ocr.OffsetX}_{ocr.OffsetY}.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); ocrname = $"{templatename}_{ocr.OffsetX}_{ocr.OffsetY}";
ocrRotate.Save($"ocr\\{ocrname}.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
return ocrRotate; return ocrRotate;
} }
private Dictionary<string, string> CodeMatch(BarcodeInfo codeinfo, List<MaterialCodeMatch> codeMatch) private Dictionary<string, string> CodeMatch(BarcodeInfo codeinfo, List<MaterialCodeMatch> codeMatch)
{ {
Dictionary<string, string> key = new(); Dictionary<string, string> key = new();
...@@ -724,7 +737,18 @@ namespace BLL ...@@ -724,7 +737,18 @@ namespace BLL
return tempIndex; return tempIndex;
} }
public static void OffsetCoordinate(float x, float y, double radian, double offsetX, out double newX, out double newY)
{
var aaa = PointWithAngle(new System.Drawing.PointF(x, y), radian+270, offsetX);
newX = aaa.X;
newY = aaa.Y;
}
public static System.Drawing.PointF PointWithAngle(System.Drawing.PointF p1, double angle, double distance)
{
var x2 = p1.X + distance * Math.Cos(angle / 180 * Math.PI);
var y2 = p1.Y + distance * Math.Sin(angle / 180 * Math.PI);
return new System.Drawing.Point((int)x2, (int)y2);
}
//public Dictionary<string, string> ExtractCode(List<MaterialCode> code, int mateIndex) //public Dictionary<string, string> ExtractCode(List<MaterialCode> code, int mateIndex)
//{ //{
// Dictionary<string, string> keyword = new Dictionary<string, string>(); // Dictionary<string, string> keyword = new Dictionary<string, string>();
......
...@@ -34,7 +34,7 @@ namespace Model ...@@ -34,7 +34,7 @@ namespace Model
RestRequest request = new(Method.GET); RestRequest request = new(Method.GET);
IRestResponse response = client.Execute(request); IRestResponse response = client.Execute(request);
string s = response.Content; string s = response.Content;
LogNet.log.Info($"[GET][URL:{url}][Return:{s}]"); ///LogNet.log.Info($"[GET][URL:{url}][Return:{s}]");
return FormatContent(s); return FormatContent(s);
} }
......
...@@ -138,6 +138,9 @@ namespace SmartScan ...@@ -138,6 +138,9 @@ namespace SmartScan
RectangleF src = new(0, 0, codeImage.Width, codeImage.Height); RectangleF src = new(0, 0, codeImage.Width, codeImage.Height);
g.DrawImage(codeImage, src, src, GraphicsUnit.Pixel); g.DrawImage(codeImage, src, src, GraphicsUnit.Pixel);
g.ResetTransform(); g.ResetTransform();
codeCenter.X -= mateCode.Width / 2;
//codeCenter.Y += mateCode.Height / 4;
g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y - 10, codeCenter.X + 10, codeCenter.Y + 10); g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y - 10, codeCenter.X + 10, codeCenter.Y + 10);
g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y + 10, codeCenter.X + 10, codeCenter.Y - 10); g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y + 10, codeCenter.X + 10, codeCenter.Y - 10);
g.Save(); g.Save();
...@@ -206,6 +209,8 @@ namespace SmartScan ...@@ -206,6 +209,8 @@ namespace SmartScan
string codeOcrs = getOcrString(); string codeOcrs = getOcrString();
codeOcr[ocrRectIndex].Text = codeOcrs; codeOcr[ocrRectIndex].Text = codeOcrs;
codeOcr[ocrRectIndex].OffsetX = ocrRect[ocrRectIndex].X-codeCenter.X;
codeOcr[ocrRectIndex].OffsetY = ocrRect[ocrRectIndex].Y-codeCenter.Y;
PicImage.Cursor = Cursors.Cross; PicImage.Cursor = Cursors.Cross;
//_ocr[ocrIndex[codeOcrIndex]].Offset = new Point(Convert.ToInt32(ocrRect[codeOcrIndex].X - codeCenter.X), Convert.ToInt32(ocrRect[codeOcrIndex].Y - codeCenter.Y)); //_ocr[ocrIndex[codeOcrIndex]].Offset = new Point(Convert.ToInt32(ocrRect[codeOcrIndex].X - codeCenter.X), Convert.ToInt32(ocrRect[codeOcrIndex].Y - codeCenter.Y));
} }
......
using System; using Model;
using System;
using System.IO.Ports; using System.IO.Ports;
namespace SmartScan namespace SmartScan
...@@ -42,8 +43,11 @@ namespace SmartScan ...@@ -42,8 +43,11 @@ namespace SmartScan
{ {
if (sourceFromIO) if (sourceFromIO)
{ {
if (Common.config.EnabledIO && Common.ioModule.IsConn) if (Common.config.EnabledIO)
Common.ioModule?.WriteDO(Common.config.IOLight, BLL.Status.On); {
var r=Common.ioModule?.WriteDO(Common.config.IOLight, BLL.Status.On);
LogNet.log.Info("Light TurnOn,IsConn:" + Common.ioModule?.IsConn+ ",WriteDO:" + r);
}
} }
else else
{ {
......
...@@ -274,11 +274,17 @@ namespace SmartScan ...@@ -274,11 +274,17 @@ namespace SmartScan
if (codeIndex == -1) return; if (codeIndex == -1) return;
var m = mateCopy[mateIndex].Match.Find(m=>m.CodeID==codeIndex); var m = mateCopy[mateIndex].Match.Find(m=>m.CodeID==codeIndex);
if (m == null) { if (m == null)
{
string text = Language.Dialog("OcrNeedCodeSetKey"); string text = Language.Dialog("OcrNeedCodeSetKey");
new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog(); new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
return; return;
} }
else if (m.CodeType == "QR Code" || m.CodeType == "Data Matrix ECC 200") {
string text = Language.Dialog("OcrNotSupport2DCode","二维码不能用于OCR定位基准");
new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
return;
}
List<MaterialCodeOCR> codeOcr = mateCopy[mateIndex].Ocr.FindAll(s => s.CodeID == mateCopy[mateIndex].Code[codeIndex].ID); List<MaterialCodeOCR> codeOcr = mateCopy[mateIndex].Ocr.FindAll(s => s.CodeID == mateCopy[mateIndex].Code[codeIndex].ID);
FrmCodeOCR frm = new(mateCopy[mateIndex].Code[codeIndex], codeOcr, mateCopy[mateIndex].Match, mateCopy[mateIndex].Image); FrmCodeOCR frm = new(mateCopy[mateIndex].Code[codeIndex], codeOcr, mateCopy[mateIndex].Match, mateCopy[mateIndex].Image);
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<appSettings> <appSettings>
<Language>简体中文</Language> <Language>English</Language>
<PrinterName>Microsoft Print to PDF</PrinterName> <PrinterName>Microsoft Print to PDF</PrinterName>
<PrintLandscape>False</PrintLandscape> <PrintLandscape>False</PrintLandscape>
<HistoryImage>Original</HistoryImage> <HistoryImage>Original</HistoryImage>
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Language Name="English"> <Language Name="English">
<Dialog> <Dialog>
<LoginWrong>The login password is incorrect.</LoginWrong> <LoginWrong>The login password is incorrect.</LoginWrong>
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
<filepatseerror>File read fails</filepatseerror> <filepatseerror>File read fails</filepatseerror>
<!--原文:文件格式不正确--> <!--原文:文件格式不正确-->
<fileformaterror>Incorrect file format</fileformaterror> <fileformaterror>Incorrect file format</fileformaterror>
<!--原文:二维码不能用于OCR定位基准-->
<OcrNotSupport2DCode>2D barcode cannot be used as OCR positioning benchmarks</OcrNotSupport2DCode>
</Dialog> </Dialog>
<FrmUsersLogin Text="Login" Font="Arial,24,B,"> <FrmUsersLogin Text="Login" Font="Arial,24,B,">
<LblUser Text="User name" Font="Arial,11,," /> <LblUser Text="User name" Font="Arial,11,," />
......
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
<filenotexists>文件状态:改文件不存在</filenotexists> <filenotexists>文件状态:改文件不存在</filenotexists>
<!--原文:文件格式不正确--> <!--原文:文件格式不正确-->
<fileformaterror>文件格式不正确</fileformaterror> <fileformaterror>文件格式不正确</fileformaterror>
<!--原文:-->
<OcrNotSupport2DCode>二维码不能用于OCR定位基准</OcrNotSupport2DCode>
</Dialog> </Dialog>
<FrmUsersLogin Text="登录" Font="微软雅黑,24,B,"> <FrmUsersLogin Text="登录" Font="微软雅黑,24,B,">
<LblUser Text="用户名:" Font="微软雅黑,11,," /> <LblUser Text="用户名:" Font="微软雅黑,11,," />
......
此文件的差异太大,无法显示。
...@@ -5,14 +5,10 @@ ...@@ -5,14 +5,10 @@
<level value="Info"/> <level value="Info"/>
<appender-ref ref="SmartScan"/> <appender-ref ref="SmartScan"/>
</logger> </logger>
<logger name="VisionLib"> <logger name="VisionLib" >
<level value="Debug"/> <level value="Debug"/>
<appender-ref ref="VisionLib"/> <appender-ref ref="VisionLib"/>
</logger> </logger>
<root>
<level value="Debug"/>
<appender-ref ref="VisionLib"/>
</root>
<appender name="SmartScan" type="log4net.Appender.RollingFileAppender"> <appender name="SmartScan" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\\SmartScan.log" /> <param name="File" value="Logs\\SmartScan.log" />
<param name="Encoding" value="UTF-8"/> <param name="Encoding" value="UTF-8"/>
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!