Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extra allocation and copy. #149

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Alturos.Yolo.UnitTest/BasicTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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());
}
}

Expand All @@ -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());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Alturos.Yolo/Alturos.Yolo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Version>3.0.6-alpha</Version>
<AssemblyVersion>3.0.6</AssemblyVersion>
<FileVersion>3.0.6</FileVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<!-- Only for .netstandard 2.0 -->
Expand Down
31 changes: 13 additions & 18 deletions src/Alturos.Yolo/YoloWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,41 +205,36 @@ public IEnumerable<YoloItem> Detect(string filepath)
/// <returns></returns>
/// <exception cref="NotImplementedException">Thrown when the yolo_cpp dll is wrong compiled</exception>
/// <exception cref="Exception">Thrown when the byte array is not a valid image</exception>
public IEnumerable<YoloItem> Detect(byte[] imageData)
public unsafe 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);
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)
{
Expand Down