//检测错误保存文件 public bool savepic(Bitmap bitmap, String path) { bbox_t_container boxlist = new bbox_t_container(); bool ng; byte[] byteImg = Bitmap2Byte(bitmap); long intime = DateTimeUtil.DateTimeToLongTimeStamp(DateTime.Now); int n = AITestSDK.detect_opencv_mat(byteImg, byteImg.Length, ref boxlist, (float)typenum); long endtime = DateTimeUtil.DateTimeToLongTimeStamp(DateTime.Now); long gettime = endtime - intime; Console.WriteLine(gettime); int j = 0; if (n == -1) { Loghelper.WriteLog("调用失败,请检测目录是否包含opencv的dll"); } for (int i = 0; i < boxlist.bboxlist.Length; i++) { if (boxlist.bboxlist[i].h == 0) { break; } else { DrawRectangleInPicture(bitmap, (int)boxlist.bboxlist[i].x, (int)boxlist.bboxlist[i].y, (int)boxlist.bboxlist[i].w, (int)boxlist.bboxlist[i].h, Color.Red, 2, DashStyle.Solid); j++; } } if (j > 0) { ng = true; } else { ng = false; } bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); return(ng); }
/// <summary> /// AI大图检测 /// </summary> /// <param name="isFront">是否是正面</param> /// <param name="bitmapInfo">图片信息</param> /// <param name="scaleRect">已经缩放的矩形框在缩放大图的位置</param> /// <param name="scale">缩放的尺度</param> /// <param name="confidence">置信度</param> /// <param name="savePath">图像保存地址</param> public void aiDetect(bool isFront, OneStitchSidePcb.BitmapInfo bitmapInfo, Rectangle scaleRect, double scale, float confidence, string savePath) { //Console.WriteLine(allDetectNum); MySmartThreadPool.InstanceSmall().QueueWorkItem((name, bmp) => { try { bbox_t_container boxlist = new bbox_t_container(); using (MemoryStream stream = new MemoryStream()) { bmp.Save(stream, ImageFormat.Jpeg); byte[] byteImg = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(byteImg, 0, Convert.ToInt32(stream.Length)); int n = -1; lock (detectKey) { n = AITestSDK.detect_opencv_mat(byteImg, byteImg.Length, ref boxlist, confidence); } if (n == -1) { Console.WriteLine("AI调用失败"); } else { resultJoin(name, isFront, scale, scaleRect, boxlist, new List <string>() { "NG" }, new Point(0, 0)); } } } catch (Exception er) { } finally { bmp.Dispose(); allDetectNum++; aiDone(savePath); } //最总检测的结果还是放在这里发送吧 }, bitmapInfo.name, (Bitmap)bitmapInfo.bitmap.Clone()); }
public static extern int detect_image_path(string filename, ref bbox_t_container bbox_T_Container, float thresh = (float)0.1);
public static extern int detect_opencv_mat(byte[] data, long data_length, ref bbox_t_container bbox_T_Container, float thresh = (float)0.1);
public void resultJoin(string imgName, bool isFront, double scale, Rectangle scaleRect, bbox_t_container boxlist, List <string> names, Point startPoint) { Snowflake snowflake = new Snowflake(2); if (boxlist.bboxlist.Length > 0) { for (int i = 0; i < boxlist.bboxlist.Length; i++) { if (boxlist.bboxlist[i].h == 0) { break; } else { string id = imgName.Replace(".jpg", "") + "(" + snowflake.nextId().ToString() + ")"; bbox_t bbox = boxlist.bboxlist[i]; bbox.x = (uint)((bbox.x + startPoint.X) * scale) + (uint)scaleRect.X; // + (uint)scaleRect.X; bbox.y = (uint)((bbox.y + startPoint.Y) * scale) + (uint)scaleRect.Y; // + (uint)scaleRect.Y; bbox.w = (uint)(bbox.w * scale); bbox.h = (uint)(bbox.h * scale); Result result = new Result() { Id = id, IsBack = Convert.ToInt32(!isFront), score = bbox.prob, PcbId = nowPcb.Id, Area = "", Region = bbox.x + "," + bbox.y + "," + bbox.w + "," + bbox.h, NgType = names[(int)bbox.obj_id], PartImagePath = id + ".jpg", CreateTime = DateTime.Now, }; lock (nowPcb.results) { nowPcb.results.Add(result); } } } } }