版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

263 lines
8.9 KiB

  1. using HalconDotNet;
  2. using Microsoft.ML.OnnxRuntime;
  3. using Microsoft.ML.OnnxRuntime.Tensors;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using Yolo5;
  16. namespace AssistClient.Device
  17. {
  18. public class SizeLib : IDisposable
  19. {
  20. public Action<WarningEnum, string> WarningEvent;
  21. /// <summary>
  22. /// 检测结果JSON(原图,结果)
  23. /// </summary>
  24. public Action<SizeTask> finishEvent;
  25. /// <summary>
  26. /// 是否打开设备成功
  27. /// </summary>
  28. public bool IsInit { get; private set; } = false;
  29. //private System.Timers.Timer timer = new System.Timers.Timer();
  30. //配置
  31. HDevEngine MyEngine = new HDevEngine();
  32. private Thread t_task;
  33. public SizeLib()
  34. {
  35. }
  36. public bool start(string enginePath)
  37. {
  38. try
  39. {
  40. IsInit = true;
  41. taskList.Clear();
  42. MyEngine.SetProcedurePath(enginePath);
  43. t_task = new System.Threading.Thread(run);
  44. t_task.IsBackground = true;
  45. t_task.Start();
  46. return true;
  47. }
  48. catch (Exception ex)
  49. {
  50. WarningEvent?.Invoke(WarningEnum.High, ex.Message);
  51. return false;
  52. }
  53. }
  54. public void stop()
  55. {
  56. if (!IsInit) return;
  57. try
  58. {
  59. IsInit = false;
  60. //timer.Elapsed -= Timer_Elapsed;
  61. MyEngine.Dispose();
  62. }
  63. catch { }
  64. }
  65. private bool _debug = false;
  66. public bool setDebug
  67. {
  68. get { return _debug; }
  69. set
  70. {
  71. if (!IsInit) return;
  72. _debug = value;
  73. if (_debug)
  74. MyEngine.StartDebugServer();
  75. else
  76. MyEngine.StopDebugServer();
  77. }
  78. }
  79. private void run()
  80. {
  81. int taskCount;
  82. while (IsInit)
  83. {
  84. lock (taskList)
  85. {
  86. taskCount = taskList.Count;
  87. }
  88. if (taskCount < 1)
  89. {
  90. Thread.Sleep(10);
  91. continue;
  92. }
  93. ////
  94. int step = 0;
  95. var task = pop();
  96. try
  97. {
  98. if (task != null)
  99. {
  100. var Program1 = new HDevProcedure(task.engineName);
  101. HDevProcedureCall ProcCall1_PI_PT = new HDevProcedureCall(Program1);
  102. step = 1;
  103. //
  104. HObject image;
  105. if (task.bmp != null)
  106. Bitmap2HObjectBpp24(out image, task.bmp);
  107. else
  108. HOperatorSet.ReadImage(out image, task.file_path);
  109. step = 2;
  110. //设置外部函数输入
  111. ProcCall1_PI_PT.SetInputIconicParamObject("Image_Repair", image);
  112. ProcCall1_PI_PT.SetInputCtrlParamTuple("Index", task.index);//参数0-9
  113. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseX", task.posX);
  114. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseY", task.posY);
  115. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkX_Detect_1", task.MarkPointList[0]);
  116. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkY_Detect_1", task.MarkPointList[1]);
  117. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkX_Detect_2", task.MarkPointList[2]);
  118. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkY_Detect_2", task.MarkPointList[3]);
  119. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkX_Detect_3", task.MarkPointList[4]);
  120. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkY_Detect_3", task.MarkPointList[5]);
  121. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkX_Detect_4", task.MarkPointList[6]);
  122. ProcCall1_PI_PT.SetInputCtrlParamTuple("PoseMarkY_Detect_4", task.MarkPointList[7]);
  123. step = 3;
  124. ProcCall1_PI_PT.Execute();//执行外部函数
  125. step = 4;
  126. //获取外部函数输出
  127. step = 5;
  128. task.isSucceed = true;
  129. task.resultInfo = "成功";
  130. callback(task);
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. WarningEvent?.Invoke(WarningEnum.Low, "SizeLib task err(" + step + "):" + ex.Message);
  136. task.isSucceed = false;
  137. task.resultInfo = $"(errcode:{step}):{ex.Message}";
  138. callback(task);
  139. }
  140. }
  141. }
  142. private void callback(SizeTask task)
  143. {
  144. //返回成功/失败,异步调用
  145. if (task.finishEvent != null || (task.finishEvent = finishEvent) != null)
  146. //task.finishEvent.BeginInvoke(result, errInfo, res => task.finishEvent.EndInvoke(res), null);
  147. System.Threading.ThreadPool.QueueUserWorkItem(waitCallback, task);
  148. }
  149. //异步回调
  150. WaitCallback waitCallback = new WaitCallback(o => {
  151. var task = (SizeTask)o;
  152. task.finishEvent(task);
  153. });
  154. //=======task list
  155. private List<SizeTask> taskList = new List<SizeTask>();
  156. public class SizeTask
  157. {
  158. public int stepIndex;//库内部不用,回调时引用
  159. public string engineName;
  160. /// <summary>
  161. /// 源文件
  162. /// </summary>
  163. public string file_path;
  164. public double posX, posY;
  165. public Bitmap bmp;
  166. /// <summary>
  167. /// 比对(index=777); 计算Mark(111/222/333/444); 尺寸(0-9); 轴偏移调整(10,20,30...)
  168. /// </summary>
  169. public int index; //index=8 PT1=PT2
  170. //MARK点
  171. public double[] MarkPointList = { 0, 0, 0, 0, 0, 0, 0, 0 };
  172. /// <summary>
  173. /// 完成后回调
  174. /// </summary>
  175. public Action<SizeTask> finishEvent;
  176. public long createTime = DateTime.Now.Ticks;
  177. //==结果返回
  178. /// <summary>
  179. /// 比对结果(index=777)
  180. /// </summary>
  181. public bool CompResult;
  182. public double PT1, PT2, Shanxian, Circle_Ymm, Circle_Xmm, offsetX, offsetY;
  183. public bool isSucceed;//转换是否成功
  184. public string resultInfo = "";//成功或失败信息
  185. }
  186. public void add(SizeTask task)
  187. {
  188. lock (taskList)
  189. taskList.Add(task);
  190. }
  191. private SizeTask pop()
  192. {
  193. lock (taskList)
  194. {
  195. if (taskList.Count < 1)
  196. return null;
  197. int index = 0;// taskList.FindIndex(p => { return p.isSync; });
  198. //if (index < 0) index = 0;
  199. var task = taskList[0];
  200. taskList.RemoveAt(0);
  201. return task;
  202. }
  203. }
  204. public void Dispose()
  205. {
  206. stop();
  207. }
  208. /// <summary>
  209. /// Bitmap转HObject灰度图
  210. /// </summary>
  211. /// <param name="bmp">Bitmap图像</param>
  212. /// <param name="image">HObject图像</param>
  213. private void Bitmap2HObjectBpp8(out HObject image, Bitmap bmp)
  214. {
  215. try
  216. {
  217. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  218. BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
  219. HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
  220. bmp.UnlockBits(srcBmpData);
  221. }
  222. catch (Exception)
  223. {
  224. image = null;
  225. }
  226. }
  227. public void Bitmap2HObjectBpp24(out HObject image, Bitmap bmp)
  228. {
  229. try
  230. {
  231. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  232. BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  233. HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
  234. bmp.UnlockBits(srcBmpData);
  235. }
  236. catch (Exception ex)
  237. {
  238. image = null;
  239. }
  240. }
  241. }
  242. }