版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

477 wiersze
24 KiB

  1. using HalconDotNet;
  2. using MaiMuAOI.SysCtrl;
  3. using MaiMuControl.Device;
  4. using MaiMuControl.Device.CamDev;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using OpenCvSharp;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using System.Drawing.Imaging;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using Yolo5;
  19. namespace MaiMuAOI.ImageProcessing
  20. {
  21. public class SizeLib : IDisposable
  22. {
  23. public Action<WarningEnum, string> WarningEvent;
  24. /// <summary>
  25. /// 检测结果JSON(原图,结果)
  26. /// </summary>
  27. public Action<SizeTask> finishEvent;
  28. /// <summary>
  29. /// 是否打开设备成功
  30. /// </summary>
  31. public bool IsInit { get; private set; } = false;
  32. //private System.Timers.Timer timer = new System.Timers.Timer();
  33. //配置
  34. HDevEngine MyEngine = new HDevEngine();
  35. //777时用
  36. Yolo_Class yolo5 =new Yolo_Class();
  37. private Thread t_task;
  38. public SizeLib()
  39. {
  40. }
  41. public bool start(string enginePath)
  42. {
  43. try
  44. {
  45. IsInit = true;
  46. taskList.Clear();
  47. MyEngine.SetProcedurePath(enginePath);
  48. t_task = new System.Threading.Thread(run);
  49. t_task.IsBackground = true;
  50. t_task.Start();
  51. return true;
  52. }
  53. catch (Exception ex)
  54. {
  55. WarningEvent?.Invoke(WarningEnum.High,ex.Message);
  56. return false;
  57. }
  58. }
  59. public void stop()
  60. {
  61. if (!IsInit) return;
  62. try
  63. {
  64. IsInit = false;
  65. //timer.Elapsed -= Timer_Elapsed;
  66. MyEngine.Dispose();
  67. }
  68. catch { }
  69. }
  70. private bool _debug = false;
  71. public bool setDebug {
  72. get { return _debug; }
  73. set {
  74. if (!IsInit) return;
  75. _debug=value;
  76. if (_debug)
  77. MyEngine.StartDebugServer();
  78. else
  79. MyEngine.StopDebugServer();
  80. }
  81. }
  82. //流程:111,222,444,333,777,777,777,。。。
  83. private void run()
  84. {
  85. int taskCount;
  86. while (IsInit)
  87. {
  88. lock (taskList)
  89. {
  90. taskCount = taskList.Count;
  91. }
  92. if (taskCount < 1)
  93. {
  94. Thread.Sleep(10);
  95. continue;
  96. }
  97. ////
  98. int step = 0;
  99. var task = pop();
  100. try
  101. {
  102. if (task != null)
  103. {
  104. var Program1 = new HDevProcedure(task.engineName);
  105. HDevProcedureCall ProcCall1_PI_PT = new HDevProcedureCall(Program1);
  106. step = 1;
  107. //
  108. HObject image;
  109. if (task.Himage != null)
  110. image = task.Himage.Clone();
  111. //Bitmap2HObjectBpp24(out image, task.bmp);
  112. else
  113. HOperatorSet.ReadImage(out image, task.file_path);
  114. step = 2;
  115. //设置外部函数输入
  116. //if (task.index < 100)
  117. {
  118. ProcCall1_PI_PT.SetInputIconicParamObject("Image1", image.Clone());
  119. ProcCall1_PI_PT.SetInputCtrlParamTuple("index", task.index);//参数1-9
  120. ProcCall1_PI_PT.SetInputCtrlParamTuple("posX", task.posX);
  121. ProcCall1_PI_PT.SetInputCtrlParamTuple("posY", task.posY);
  122. ProcCall1_PI_PT.SetInputCtrlParamTuple("GerberPath",new HTuple( task.drawingPagePath));//美尚没有
  123. //设置外部函数输入
  124. if ((task.PTandLinePos != null) && (task.PTandLinePos[0] != 0))
  125. {
  126. double[] posarray = new double[23];
  127. posarray[0] = task.PTandLinePos[0];
  128. posarray[1] = task.PTandLinePos[2];
  129. posarray[2] = task.PTandLinePos[4];
  130. posarray[3] = task.PTandLinePos[6];
  131. posarray[4] = task.PTandLinePos[8];
  132. for (int i = 0; i < 18; i++)
  133. {
  134. posarray[5 + i] = task.PTandLinePos[10 + i];
  135. }
  136. ProcCall1_PI_PT.SetInputCtrlParamTuple("Adapter", task.Adapter);
  137. if (task.AdapterPos != null)
  138. {
  139. WarningEvent?.Invoke(WarningEnum.Normal, $"PTandLinePos1:" + $",{JsonConvert.SerializeObject(task.PTandLinePos)},||{JsonConvert.SerializeObject(task.Adapter)},||{JsonConvert.SerializeObject(task.AdapterPos)}");
  140. ProcCall1_PI_PT.SetInputCtrlParamTuple("UserPose", task.AdapterPos);
  141. }
  142. else
  143. {
  144. WarningEvent?.Invoke(WarningEnum.Normal, $"PTandLinePos2:" + $",{JsonConvert.SerializeObject(task.PTandLinePos)},||{JsonConvert.SerializeObject(task.Adapter)},||{JsonConvert.SerializeObject(task.AdapterPos)}");
  145. //ProcCall1_PI_PT.SetInputCtrlParamTuple("UserPose", task.AdapterPos);
  146. ProcCall1_PI_PT.SetInputCtrlParamTuple("UserPose", posarray);
  147. }
  148. /////
  149. ///
  150. //string directory = Config.LogPath + "\\" + DateTime.Now.ToString("yyyyMM") + "\\";
  151. //if (!System.IO.Directory.Exists(directory))
  152. // System.IO.Directory.CreateDirectory(directory);
  153. //File.AppendAllText(directory + "sizeData.log", "data:" + string.Join(",", posarray) + "\r\n");
  154. }
  155. else if (task.AdapterPos != null)
  156. {
  157. WarningEvent?.Invoke(WarningEnum.Normal, $"PTandLinePos3:" + $",{JsonConvert.SerializeObject(task.PTandLinePos)},||{JsonConvert.SerializeObject(task.Adapter)},||{JsonConvert.SerializeObject(task.AdapterPos)}");
  158. ProcCall1_PI_PT.SetInputCtrlParamTuple("UserPose", task.AdapterPos);
  159. ProcCall1_PI_PT.SetInputCtrlParamTuple("Adapter", task.Adapter);
  160. ProcCall1_PI_PT.SetInputCtrlParamTuple("UserPose", task.AdapterPos);
  161. }
  162. else
  163. {
  164. WarningEvent?.Invoke(WarningEnum.Normal, $"PTandLinePos4:" + $",{JsonConvert.SerializeObject(task.PTandLinePos)},||{JsonConvert.SerializeObject(task.Adapter)},||{JsonConvert.SerializeObject(task.AdapterPos)}");
  165. ProcCall1_PI_PT.SetInputCtrlParamTuple("UserPose", task.AdapterPos);
  166. double[] posarray = new double[23];
  167. task.Adapter = new int[ConfMgr.Instance.SizeItemParamsList.Count];
  168. task.Adapter[0] = 5;
  169. task.Adapter[1] = 18;
  170. ProcCall1_PI_PT.SetInputCtrlParamTuple("Adapter", task.Adapter);
  171. ProcCall1_PI_PT.SetInputCtrlParamTuple("UserPose", posarray);
  172. }
  173. }
  174. if (task.index == 777)
  175. {
  176. ProcCall1_PI_PT.SetInputIconicParamObject("ContoursAffineTrans1", task.ContoursAffineTrans1_Out);
  177. }
  178. else if (task.index == 77777)
  179. {
  180. ProcCall1_PI_PT.SetInputIconicParamObject("ContoursAffineTrans1", task.ContoursAffineTrans1_Out);
  181. }
  182. else
  183. {
  184. HObject ContoursAffineTrans1;
  185. HOperatorSet.GenEmptyObj(out ContoursAffineTrans1);
  186. ProcCall1_PI_PT.SetInputIconicParamObject("ContoursAffineTrans1", ContoursAffineTrans1);
  187. }
  188. step = 3;
  189. ProcCall1_PI_PT.Execute();//执行外部函数
  190. step = 4;
  191. //获取外部函数输出
  192. switch (task.index)
  193. {
  194. case 77777:
  195. case 777://比对结果
  196. step = 10;
  197. task.CompResult = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("RES") == 1;//结果,1为true,0为false
  198. task.ResSts = (int)ProcCall1_PI_PT.GetOutputCtrlParamTuple("RES");
  199. step = 11;
  200. var Defects_X = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_X");
  201. var Defects_Y = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_Y");
  202. var Defects_LeftTop_X = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_LeftTop_X");
  203. var Defects_LeftTop_Y = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_LeftTop_Y");
  204. var Defects_RightBottom_X = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_RightBottom_X");
  205. var Defects_RightBottom_Y = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_RightBottom_Y");
  206. var Contour_Image = ProcCall1_PI_PT.GetOutputIconicParamObject("Contour_Image");
  207. //var Zoom_Image = ProcCall1_PI_PT.GetOutputIconicParamObject("Zoom_Image");
  208. var Defects_Type = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_Type");
  209. var Defects_Index = ProcCall1_PI_PT.GetOutputCtrlParamTuple("Defects_Index");
  210. step = 12;
  211. //对比打标图:显示在UI上,以后修复台用
  212. //task.Zoom_Image_mat = yolo5.DrawContour_Opencv(Contour_Image, Zoom_Image, Defects_LeftTop_X, Defects_LeftTop_Y, Defects_RightBottom_X, Defects_RightBottom_Y);
  213. //新YOLO.DLL
  214. task.defectInfor2RestorationDesk = new List<List<string>>();
  215. task.DB_Image_mat = yolo5.DrawContour_Opencv(Contour_Image, image, Defects_LeftTop_X, Defects_LeftTop_Y,
  216. Defects_RightBottom_X, Defects_RightBottom_Y, Defects_Type, Defects_Index, Defects_X, Defects_Y, out task.defectInfor2RestorationDesk);
  217. //SysMgr.Instance.AddSizeDefectData(task.posX, task.posY, task.defectInfor2RestorationDesk);
  218. //WarningEvent?.Invoke(WarningEnum.Normal, $"SizeLib 777 call DrawContour_Opencv(),Defects_LeftTop_X={Defects_LeftTop_X}," +
  219. // $"Defects_LeftTop_Y={Defects_LeftTop_Y},Defects_RightBottom_X={Defects_RightBottom_X},Defects_RightBottom_Y={Defects_RightBottom_Y}," +
  220. // $"Defects_Type={Defects_Type},Defects_Index={Defects_Index},Defects_X={Defects_X},Defects_Y={Defects_Y}," +
  221. // $"defectInfor2RestorationDesk={JsonConvert.SerializeObject(task.defectInfor2RestorationDesk)}");
  222. step = 13;
  223. task.Zoom_Image_mat = yolo5.ResizesMat_4(task.DB_Image_mat);
  224. if (!task.CompResult)
  225. {
  226. if (ConfMgr.Instance.SysConfigParams.OpenFlawDistribution)
  227. {
  228. //大图缺陷坐标转换到图纸坐标
  229. step = 14;
  230. if (!string.IsNullOrWhiteSpace(task.drawingPagePath) && task.defectInfor2RestorationDesk != null && task.defectInfor2RestorationDesk.Count > 0)
  231. task.defectInfor2RestorationDeskPage = yolo5.DefectDrawGerberImage(task.drawingPagePath, task.defectInfor2RestorationDesk);
  232. }
  233. }
  234. break;
  235. case 111:
  236. task.MarkPointList[0] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkX_1");
  237. task.MarkPointList[1] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkY_1");
  238. break;
  239. case 222:
  240. task.MarkPointList[2] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkX_2");
  241. task.MarkPointList[3] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkY_2");
  242. break;
  243. case 333:
  244. task.MarkPointList[4] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkX_3");
  245. task.MarkPointList[5] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkY_3");//ContoursAffineTrans1
  246. task.ContoursAffineTrans1_Out = ProcCall1_PI_PT.GetOutputIconicParamObject("ContoursAffineTrans1_Out");
  247. break;
  248. case 444:
  249. task.MarkPointList[6] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkX_4");
  250. task.MarkPointList[7] = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("PoseMarkY_4");
  251. break;
  252. case 3333: //Tag使用,同时继续获取使用default时时校正和其它值
  253. step = 80;
  254. if (!string.IsNullOrWhiteSpace( task.sizeTag))
  255. task.posePT= ProcCall1_PI_PT.GetOutputCtrlParamTuple("posePT").DArr;
  256. goto default;
  257. default:
  258. step = 90;
  259. task.PT1 = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("DistancePT1");//index=8 PT1=PT2
  260. task.PT2 = ProcCall1_PI_PT.GetOutputCtrlParamTuple("DistancePT2");
  261. task.Shanxian = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("Distance3Median");
  262. //task.RowP = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("RowP");
  263. task.Circle_Ymm = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("Circle_Ymm");
  264. task.Circle_Xmm = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("Circle_Xmm");
  265. task.offsetX = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("offsetX");
  266. task.offsetY = (double)ProcCall1_PI_PT.GetOutputCtrlParamTuple("offsetY");
  267. break;
  268. }
  269. step = 5;
  270. //尺寸标注
  271. if (task.index != 3333)
  272. {
  273. try
  274. {
  275. step = 6;
  276. double[] posePTarr;
  277. try
  278. {
  279. if (ProcCall1_PI_PT.GetOutputCtrlParamTuple("posePT") != null &&
  280. ProcCall1_PI_PT.GetOutputCtrlParamTuple("posePT").DArr != null)
  281. posePTarr = ProcCall1_PI_PT.GetOutputCtrlParamTuple("posePT").DArr;
  282. else
  283. posePTarr = null;
  284. }
  285. catch
  286. {
  287. posePTarr = null;
  288. }
  289. step = 7;
  290. //Yolo_Class yolo = new Yolo_Class();
  291. step = 8;
  292. var mat = CamDev.HImageToMat(image);
  293. step = 9;
  294. //task.SaveMat = yolo.DisplayLines(mat, posePT, task.index).Clone();
  295. task.SaveMat = yolo5.DisplayLines(mat, posePTarr, task.index);
  296. }
  297. catch (Exception ex)
  298. {
  299. WarningEvent?.Invoke(WarningEnum.Low, $"SizeLib task err({step}) index({task.index}):" + ex.Message);
  300. task.SaveMat = null;
  301. }
  302. }
  303. step = 100;
  304. ProcCall1_PI_PT.Dispose();
  305. Program1.Dispose();
  306. step = 101;
  307. task.isSucceed = true;
  308. task.resultInfo = "成功";
  309. callback(task);
  310. step = 102;
  311. }
  312. Thread.Sleep(5);
  313. }
  314. catch (Exception ex)
  315. {
  316. WarningEvent?.Invoke(WarningEnum.Low, $"SizeLib task err({step}) index({task.index}):"+ ex.Message + $",{task.PTandLinePos},{task.Adapter},{task.AdapterPos}");
  317. task.isSucceed = false;
  318. task.resultInfo = $"(errcode:{step}):{ex.Message}";
  319. callback(task);
  320. }
  321. }
  322. }
  323. private void callback(SizeTask task)
  324. {
  325. //返回成功/失败,异步调用
  326. if (task.finishEvent != null || (task.finishEvent = finishEvent) != null)
  327. //task.finishEvent.BeginInvoke(result, errInfo, res => task.finishEvent.EndInvoke(res), null);
  328. System.Threading.ThreadPool.QueueUserWorkItem(waitCallback, task);
  329. }
  330. //异步回调
  331. WaitCallback waitCallback = new WaitCallback(o => {
  332. var task = (SizeTask)o;
  333. task.finishEvent(task);
  334. });
  335. //=======task list
  336. private List<SizeTask> taskList = new List<SizeTask>();
  337. public class SizeTask
  338. {
  339. public int stepIndex;//只为回调记录当前工序信息
  340. public string processName;//只为回调记录当前工序信息
  341. public string engineName,sizeTag;
  342. /// <summary>
  343. /// 源文件
  344. /// </summary>
  345. public string file_path;
  346. public double posX,posY;
  347. public HObject Himage;
  348. public HObject Himage_src;
  349. public string drawingPagePath = "";//.gbx图纸路径
  350. //2023-10-27 新增图纸选择pt点位与线宽点位
  351. public int[] Adapter;
  352. public double[] PTandLinePos;
  353. public double[] AdapterPos;
  354. /// <summary>
  355. /// 比对(index=777); 计算Mark(111/222/333/444); 尺寸(1-9); 轴偏移调整(10,20,30...)
  356. /// </summary>
  357. public int index; //index=8 PT1=PT2
  358. /// <summary>
  359. /// 完成后回调
  360. /// </summary>
  361. public Action<SizeTask> finishEvent;
  362. public long createTime = DateTime.Now.Ticks;
  363. //==结果返回
  364. /// <summary>
  365. /// 比对结果(index=777)
  366. /// </summary>
  367. public bool CompResult;
  368. public Mat Zoom_Image_mat;//对比打标图:777比对失败时计算得出,后面显示到UI 以后修复台用
  369. public Mat DB_Image_mat;//打标原图,显示使用
  370. public List<List<string>> defectInfor2RestorationDesk, defectInfor2RestorationDeskPage;//对比未通过坐标信息,合并到缺陷检测中的 //打标缺陷转为图纸的坐标;
  371. //MARK点
  372. public double[] MarkPointList = { 0, 0, 0, 0, 0, 0, 0, 0 };
  373. public HObject ContoursAffineTrans1_Out;//index=333时输出,供后面多个777比对时输入使用
  374. public double[] posePT;//index=3333 && !isEmpty(sizeTag) 才取此数组值,len为0时急停
  375. //
  376. public double PT1,PT2, Shanxian, Circle_Ymm, Circle_Xmm, offsetX, offsetY;
  377. public bool isSucceed;//转换是否成功
  378. public string resultInfo = "";//成功或失败信息
  379. public Mat SaveMat;
  380. public int ResSts;
  381. }
  382. public void add(SizeTask task)
  383. {
  384. lock (taskList)
  385. taskList.Add(task);
  386. }
  387. private SizeTask pop()
  388. {
  389. lock (taskList)
  390. {
  391. if (taskList.Count < 1)
  392. return null;
  393. var task = taskList[0];
  394. taskList.RemoveAt(0);
  395. return task;
  396. }
  397. }
  398. public void Dispose()
  399. {
  400. stop();
  401. }
  402. /// <summary>
  403. /// Bitmap转HObject灰度图
  404. /// </summary>
  405. /// <param name="bmp">Bitmap图像</param>
  406. /// <param name="image">HObject图像</param>
  407. private void Bitmap2HObjectBpp8( out HObject image,Bitmap bmp)
  408. {
  409. try
  410. {
  411. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  412. BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
  413. HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
  414. bmp.UnlockBits(srcBmpData);
  415. }
  416. catch (Exception)
  417. {
  418. image = null;
  419. }
  420. }
  421. public void Bitmap2HObjectBpp24( out HObject image, Bitmap bmp)
  422. {
  423. try
  424. {
  425. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  426. BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  427. HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
  428. bmp.UnlockBits(srcBmpData);
  429. }
  430. catch (Exception ex)
  431. {
  432. image = null;
  433. }
  434. }
  435. }
  436. }