Skip to content

Commit

Permalink
Add new logic and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
asiryan committed Apr 25, 2024
1 parent 09ffcf0 commit 037cd3a
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 29 deletions.
6 changes: 3 additions & 3 deletions netstandard/ClothONNX.Gpu/ClothONNX.Gpu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>ClothONNX.Gpu</AssemblyName>
<LangVersion>8.0</LangVersion>
<Version>1.0.2.1</Version>
<FileVersion>1.0.2.1</FileVersion>
<Version>1.1.0.1</Version>
<FileVersion>1.1.0.1</FileVersion>
<Authors>Valery Asiryan</Authors>
<Company>Valery Asiryan</Company>
<Description>Cloth segmentation library based on deep neural networks and ONNX runtime.</Description>
<Copyright>Valery Asiryan, © 2023</Copyright>
<RepositoryType>Open-source</RepositoryType>
<PackageId>ClothONNX.Gpu</PackageId>
<Product>ClothONNX.Gpu</Product>
<AssemblyVersion>1.0.2.1</AssemblyVersion>
<AssemblyVersion>1.1.0.1</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>cloth detection segmentation recognition onnx neural-networks unet</PackageTags>
<RepositoryUrl>https://github.com/FaceONNX/ClothONNX</RepositoryUrl>
Expand Down
30 changes: 22 additions & 8 deletions netstandard/ClothONNX.Gpu/ClothSegmentator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,61 @@ public class ClothSegmentator : IClothSegmentator

/// <summary>
/// Initializes cloth segmentator.
/// <param name="clothSegmentatorQuality">Cloth segmentator quality</param>
/// </summary>
public ClothSegmentator()
public ClothSegmentator(ClothSegmentatorQuality clothSegmentatorQuality = ClothSegmentatorQuality.Medium)
{
ClothSegmentatorQuality = clothSegmentatorQuality;
_session = new InferenceSession(Resources.cloth_segmentation_unet);
}

/// <summary>
/// Initializes cloth segmentator.
/// </summary>
/// <param name="clothSegmentatorQuality">Cloth segmentator quality</param>
/// <param name="options">Session options</param>
public ClothSegmentator(SessionOptions options)
public ClothSegmentator(SessionOptions options, ClothSegmentatorQuality clothSegmentatorQuality = ClothSegmentatorQuality.Medium)
{
ClothSegmentatorQuality = clothSegmentatorQuality;
_session = new InferenceSession(Resources.cloth_segmentation_unet, options);
}

#endregion

#region Properties

/// <summary>
/// Gets or sets cloth segmentator quality.
/// </summary>
public ClothSegmentatorQuality ClothSegmentatorQuality { get; set; }

#endregion

#region Methods

/// <inheritdoc/>
public float[,] Forward(Bitmap image)
public float[,] Forward(Bitmap image, InterpolationMode interpolationMode = InterpolationMode.Bilinear)
{
var rgb = image.ToRGB(false);
return Forward(rgb);
return Forward(rgb, interpolationMode);
}

/// <inheritdoc/>
public float[,] Forward(float[][,] image)
public float[,] Forward(float[][,] image, InterpolationMode interpolationMode = InterpolationMode.Bilinear)
{
if (image.Length != 3)
throw new ArgumentException("Image must be in BGR terms");

var width = image[0].GetLength(1);
var height = image[0].GetLength(0);
var size = new Size(768, 768);
var length = (int)ClothSegmentatorQuality;
var size = new Size(length, length);

var resized = new float[3][,];

for (int i = 0; i < image.Length; i++)
{
resized[i] = image[i].ResizePreserved(size.Height, size.Width, 0.0f, InterpolationMode.Bilinear);
resized[i] = image[i].ResizePreserved(size.Height, size.Width, 0.0f, interpolationMode);
}

var dimentions = new int[] { 1, 3, size.Width, size.Height };
Expand Down Expand Up @@ -113,7 +127,7 @@ public ClothSegmentator(SessionOptions options)
}
}

return mask.ResizePreserved(height, width, InterpolationMode.Bilinear);
return mask.ResizePreserved(height, width, interpolationMode);
}

#endregion
Expand Down
21 changes: 21 additions & 0 deletions netstandard/ClothONNX.Gpu/ClothSegmentatorQuality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace ClothONNX
{
/// <summary>
/// Defines a cloth segmentator quality.
/// </summary>
public enum ClothSegmentatorQuality
{
/// <summary>
/// Log quality.
/// </summary>
Low = 512,
/// <summary>
/// Medium quality.
/// </summary>
Medium = 768,
/// <summary>
/// High quality.
/// </summary>
High = 1024
}
}
7 changes: 5 additions & 2 deletions netstandard/ClothONNX.Gpu/IClothSegmentator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using UMapx.Core;

namespace ObjectONNX
{
Expand All @@ -14,15 +15,17 @@ public interface IClothSegmentator : IDisposable
/// Returns cloth segmentation results.
/// </summary>
/// <param name="image">Bitmap</param>
/// <param name="interpolationMode">Interpolation mode</param>
/// <returns>Result</returns>
float[,] Forward(Bitmap image);
float[,] Forward(Bitmap image, InterpolationMode interpolationMode = InterpolationMode.Bilinear);

/// <summary>
/// Returns cloth segmentation results.
/// </summary>
/// <param name="image">Image in BGR terms</param>
/// <param name="interpolationMode">Interpolation mode</param>
/// <returns>Result</returns>
float[,] Forward(float[][,] image);
float[,] Forward(float[][,] image, InterpolationMode interpolationMode = InterpolationMode.Bilinear);

#endregion
}
Expand Down
6 changes: 3 additions & 3 deletions netstandard/ClothONNX/ClothONNX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>ClothONNX</AssemblyName>
<LangVersion>8.0</LangVersion>
<Version>1.0.2.1</Version>
<FileVersion>1.0.2.1</FileVersion>
<Version>1.1.0.1</Version>
<FileVersion>1.1.0.1</FileVersion>
<Authors>Valery Asiryan</Authors>
<Company>Valery Asiryan</Company>
<Description>Cloth segmentation library based on deep neural networks and ONNX runtime.</Description>
<Copyright>Valery Asiryan, © 2023</Copyright>
<RepositoryType>Open-source</RepositoryType>
<PackageId>ClothONNX</PackageId>
<Product>ClothONNX</Product>
<AssemblyVersion>1.0.2.1</AssemblyVersion>
<AssemblyVersion>1.1.0.1</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>cloth detection segmentation recognition onnx neural-networks unet</PackageTags>
<RepositoryUrl>https://github.com/FaceONNX/ClothONNX</RepositoryUrl>
Expand Down
30 changes: 22 additions & 8 deletions netstandard/ClothONNX/ClothSegmentator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,61 @@ public class ClothSegmentator : IClothSegmentator

/// <summary>
/// Initializes cloth segmentator.
/// <param name="clothSegmentatorQuality">Cloth segmentator quality</param>
/// </summary>
public ClothSegmentator()
public ClothSegmentator(ClothSegmentatorQuality clothSegmentatorQuality = ClothSegmentatorQuality.Medium)
{
ClothSegmentatorQuality = clothSegmentatorQuality;
_session = new InferenceSession(Resources.cloth_segmentation_unet);
}

/// <summary>
/// Initializes cloth segmentator.
/// </summary>
/// <param name="clothSegmentatorQuality">Cloth segmentator quality</param>
/// <param name="options">Session options</param>
public ClothSegmentator(SessionOptions options)
public ClothSegmentator(SessionOptions options, ClothSegmentatorQuality clothSegmentatorQuality = ClothSegmentatorQuality.Medium)
{
ClothSegmentatorQuality = clothSegmentatorQuality;
_session = new InferenceSession(Resources.cloth_segmentation_unet, options);
}

#endregion

#region Properties

/// <summary>
/// Gets or sets cloth segmentator quality.
/// </summary>
public ClothSegmentatorQuality ClothSegmentatorQuality { get; set; }

#endregion

#region Methods

/// <inheritdoc/>
public float[,] Forward(Bitmap image)
public float[,] Forward(Bitmap image, InterpolationMode interpolationMode = InterpolationMode.Bilinear)
{
var rgb = image.ToRGB(false);
return Forward(rgb);
return Forward(rgb, interpolationMode);
}

/// <inheritdoc/>
public float[,] Forward(float[][,] image)
public float[,] Forward(float[][,] image, InterpolationMode interpolationMode = InterpolationMode.Bilinear)
{
if (image.Length != 3)
throw new ArgumentException("Image must be in BGR terms");

var width = image[0].GetLength(1);
var height = image[0].GetLength(0);
var size = new Size(768, 768);
var length = (int)ClothSegmentatorQuality;
var size = new Size(length, length);

var resized = new float[3][,];

for (int i = 0; i < image.Length; i++)
{
resized[i] = image[i].ResizePreserved(size.Height, size.Width, 0.0f, InterpolationMode.Bilinear);
resized[i] = image[i].ResizePreserved(size.Height, size.Width, 0.0f, interpolationMode);
}

var dimentions = new int[] { 1, 3, size.Width, size.Height };
Expand Down Expand Up @@ -113,7 +127,7 @@ public ClothSegmentator(SessionOptions options)
}
}

return mask.ResizePreserved(height, width, InterpolationMode.Bilinear);
return mask.ResizePreserved(height, width, interpolationMode);
}

#endregion
Expand Down
21 changes: 21 additions & 0 deletions netstandard/ClothONNX/ClothSegmentatorQuality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace ClothONNX
{
/// <summary>
/// Defines a cloth segmentator quality.
/// </summary>
public enum ClothSegmentatorQuality
{
/// <summary>
/// Log quality.
/// </summary>
Low = 512,
/// <summary>
/// Medium quality.
/// </summary>
Medium = 768,
/// <summary>
/// High quality.
/// </summary>
High = 1024
}
}
7 changes: 5 additions & 2 deletions netstandard/ClothONNX/IClothSegmentator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using UMapx.Core;

namespace ObjectONNX
{
Expand All @@ -14,15 +15,17 @@ public interface IClothSegmentator : IDisposable
/// Returns cloth segmentation results.
/// </summary>
/// <param name="image">Bitmap</param>
/// <param name="interpolationMode">Interpolation mode</param>
/// <returns>Result</returns>
float[,] Forward(Bitmap image);
float[,] Forward(Bitmap image, InterpolationMode interpolationMode = InterpolationMode.Bilinear);

/// <summary>
/// Returns cloth segmentation results.
/// </summary>
/// <param name="image">Image in BGR terms</param>
/// <param name="interpolationMode">Interpolation mode</param>
/// <returns>Result</returns>
float[,] Forward(float[][,] image);
float[,] Forward(float[][,] image, InterpolationMode interpolationMode = InterpolationMode.Bilinear);

#endregion
}
Expand Down
10 changes: 7 additions & 3 deletions netstandard/Examples/ClothSegmentation/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ClothONNX;
using UMapx.Core;
using UMapx.Imaging;

namespace ClothSegmentation
Expand All @@ -17,7 +18,7 @@ public Form1()
AllowDrop = true;
Text = "ClothONNX: Cloth Segmentation";

_clothSegmentator = new ClothSegmentator();
_clothSegmentator = new ClothSegmentator(ClothSegmentatorQuality.High);
var image = new Bitmap("example.png");
Process(image);
}
Expand All @@ -38,9 +39,12 @@ private void Form1_DragDrop(object sender, DragEventArgs e)

private void Process(Bitmap image)
{
var results = _clothSegmentator.Forward(image);
var results = _clothSegmentator.Forward(
image: image,
interpolationMode: InterpolationMode.Bicubic);

using var mask = results.FromGrayscale();
var maskColorFilter = new MaskColorFilter(Color.Red);
var maskColorFilter = new MaskColorFilter(Color.Yellow);
maskColorFilter.Apply(image, mask);

BackgroundImage?.Dispose();
Expand Down

0 comments on commit 037cd3a

Please sign in to comment.