private IEnumerable <YoloItem> Convert(BboxContainer container) { var yoloItems = new List <YoloItem>(); foreach (var item in container.candidates.Where(o => o.h > 0 || o.w > 0)) { if (!this._objectType.TryGetValue((int)item.obj_id, out var objectType)) { objectType = "unknown key"; } var yoloItem = new YoloItem { X = (int)item.x, Y = (int)item.y, Height = (int)item.h, Width = (int)item.w, Confidence = item.prob, Type = objectType }; yoloItems.Add(yoloItem); } return(yoloItems); }
public IEnumerable <YoloItem> Detect(string filepath) { if (!File.Exists(filepath)) { throw new FileNotFoundException("Cannot find the file", filepath); } var container = new BboxContainer(); var count = 0; switch (this.DetectionSystem) { case DetectionSystem.CPU: count = DetectImageCpu(filepath, ref container); break; case DetectionSystem.GPU: count = DetectImageGpu(filepath, ref container); break; } if (count == -1) { throw new NotImplementedException("C++ dll compiled incorrectly"); } return(this.Convert(container)); }
public IEnumerable <YoloItem> Detect(byte[] imageData) { if (!this._imageAnalyzer.IsValidImageFormat(imageData)) { throw new Exception("Invalid image data, wrong image format"); } var container = new BboxContainer(); var size = Marshal.SizeOf(imageData[0]) * imageData.Length; var pnt = Marshal.AllocHGlobal(size); try { // Copy the array to unmanaged memory. Marshal.Copy(imageData, 0, pnt, imageData.Length); var count = 0; switch (this.DetectionSystem) { case DetectionSystem.CPU: count = DetectImageCpu(pnt, imageData.Length, ref container); break; case DetectionSystem.GPU: count = DetectImageGpu(pnt, imageData.Length, ref container); break; } if (count == -1) { throw new NotImplementedException("C++ dll compiled incorrectly"); } } catch (Exception exception) { return(null); } finally { // Free the unmanaged memory. Marshal.FreeHGlobal(pnt); } return(this.Convert(container)); }
internal static extern int DetectImageGpu(IntPtr pArray, int nSize, ref BboxContainer container);
internal static extern int DetectImageGpu(string filename, ref BboxContainer container);