From 4b158894e7b95fd5165cb2fb41f772d14b1172a2 Mon Sep 17 00:00:00 2001 From: Joseph Lennox Date: Sun, 2 Aug 2020 12:41:23 -0700 Subject: [PATCH 1/2] Remove allocation/copy from `Detect(byte[])` --- src/Alturos.Yolo/Alturos.Yolo.csproj | 1 + src/Alturos.Yolo/YoloWrapper.cs | 31 ++++++++++++---------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Alturos.Yolo/Alturos.Yolo.csproj b/src/Alturos.Yolo/Alturos.Yolo.csproj index 118256f..be343a6 100644 --- a/src/Alturos.Yolo/Alturos.Yolo.csproj +++ b/src/Alturos.Yolo/Alturos.Yolo.csproj @@ -16,6 +16,7 @@ 3.0.6-alpha 3.0.6 3.0.6 + true diff --git a/src/Alturos.Yolo/YoloWrapper.cs b/src/Alturos.Yolo/YoloWrapper.cs index 7dc344f..4196de5 100644 --- a/src/Alturos.Yolo/YoloWrapper.cs +++ b/src/Alturos.Yolo/YoloWrapper.cs @@ -205,7 +205,7 @@ public IEnumerable Detect(string filepath) /// /// Thrown when the yolo_cpp dll is wrong compiled /// Thrown when the byte array is not a valid image - public IEnumerable Detect(byte[] imageData) + public unsafe IEnumerable Detect(byte[] imageData) { if (!this._imageAnalyzer.IsValidImageFormat(imageData)) { @@ -213,33 +213,28 @@ public IEnumerable Detect(byte[] imageData) } var container = new BboxContainer(); - var size = Marshal.SizeOf(imageData[0]) * imageData.Length; - var pnt = Marshal.AllocHGlobal(size); + var size = imageData.Length; var count = 0; try { - // Copy the array to unmanaged memory. - Marshal.Copy(imageData, 0, pnt, imageData.Length); - 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; + fixed (byte* pnt = imageData) + { + switch (this.DetectionSystem) + { + case DetectionSystem.CPU: + count = DetectImageCpu((IntPtr)pnt, imageData.Length, ref container); + break; + case DetectionSystem.GPU: + count = DetectImageGpu((IntPtr)pnt, imageData.Length, ref container); + break; + } } } catch (Exception) { return null; } - finally - { - // Free the unmanaged memory. - Marshal.FreeHGlobal(pnt); - } if (count == -1) { From 5f3d1a87b62b246fa8f8a018cf333283aac97831 Mon Sep 17 00:00:00 2001 From: Joseph Lennox Date: Sun, 2 Aug 2020 12:41:43 -0700 Subject: [PATCH 2/2] Simplify usage of some linq expressions. --- src/Alturos.Yolo.UnitTest/BasicTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Alturos.Yolo.UnitTest/BasicTest.cs b/src/Alturos.Yolo.UnitTest/BasicTest.cs index ac4110e..5cd7ea5 100644 --- a/src/Alturos.Yolo.UnitTest/BasicTest.cs +++ b/src/Alturos.Yolo.UnitTest/BasicTest.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.IO; +using System.IO; using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Alturos.Yolo.UnitTest { @@ -27,7 +27,7 @@ public void DetectFromFilePath() using (var yoloWrapper = new YoloWrapper(configuration)) { var items = yoloWrapper.Detect(this._imagePath); - Assert.IsTrue(items.Count() > 0); + Assert.IsTrue(items.Any()); } } @@ -39,7 +39,7 @@ public void DetectFromFileData() { var imageData = File.ReadAllBytes(this._imagePath); var items = yoloWrapper.Detect(imageData); - Assert.IsTrue(items.Count() > 0); + Assert.IsTrue(items.Any()); } } }