Skip to content

Commit

Permalink
Added augmentations prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
iXab3r committed Aug 17, 2024
1 parent 9e61ef0 commit 128f49f
Show file tree
Hide file tree
Showing 52 changed files with 1,436 additions and 146 deletions.
20 changes: 20 additions & 0 deletions Sources/YoloEase.Cvat.Shared/CvatAnnotationImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Xml.Serialization;

namespace YoloEase.Cvat.Shared;

public record CvatAnnotationImage
{
[XmlAttribute("id")] public int Id { get; set; }

[XmlAttribute("name")] public string Name { get; set; }

[XmlAttribute("subset")] public string Subset { get; set; }

[XmlAttribute("task_id")] public int TaskId { get; set; }

[XmlAttribute("width")] public int Width { get; set; }

[XmlAttribute("height")] public int Height { get; set; }

[XmlElement("box")] public List<CvatBox> Boxes { get; set; }
}
9 changes: 9 additions & 0 deletions Sources/YoloEase.Cvat.Shared/CvatAnnotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Xml.Serialization;

namespace YoloEase.Cvat.Shared;

[XmlRoot("annotations")]
public record CvatAnnotations
{
[XmlElement("image")] public List<CvatAnnotationImage> Images { get; set; }
}
31 changes: 31 additions & 0 deletions Sources/YoloEase.Cvat.Shared/CvatBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Xml.Serialization;

namespace YoloEase.Cvat.Shared;

public record CvatBox
{
[XmlAttribute("label")] public string Label { get; set; }

[XmlAttribute("source")] public string Source { get; set; }

[XmlAttribute("occluded")] public int Occluded { get; set; }

[XmlAttribute("xtl")] public double Xtl { get; set; }

[XmlAttribute("ytl")] public double Ytl { get; set; }

[XmlAttribute("xbr")] public double Xbr { get; set; }

[XmlAttribute("ybr")] public double Ybr { get; set; }

[XmlAttribute("z_order")] public int ZOrder { get; set; }

public double Width() => Xbr - Xtl;
public double Height() => Ybr - Ytl;

public double Area(){
var width = Xbr - Xtl;
var height = Ybr - Ytl;
return width * height;
}
}
38 changes: 38 additions & 0 deletions Sources/YoloEase.Cvat.Shared/CvatRectangleD.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace YoloEase.Cvat.Shared;

public readonly record struct CvatRectangleD
{
public CvatRectangleD(double x, double y, double width, double height)
{
X = x;
Y = y;
Width = width;
Height = height;
}

public double X { get; init; }
public double Y { get; init; }
public double Width { get; init; }
public double Height { get; init; }

public double CenterX => X + Width / 2;

public double CenterY => Y + Height / 2;

/// <summary>
/// The first number represents the normalized x-coordinate of the center of the bounding box relative to the width of the image. In your example, the x-coordinate is approximately 0.695, indicating that the center of the bounding box is located around 69.5% of the image width.
/// The second number represents the normalized y-coordinate of the center of the bounding box relative to the height of the image.In your example, the y-coordinate is approximately 0.645, indicating that the center of the bounding box is located around 64.5% of the image height.
/// The third number represents the normalized width of the bounding box relative to the width of the image.In your example, the width is approximately 0.0325, indicating that the bounding box width is around 3.25% of the image width.
/// The fourth number represents the normalized height of the bounding box relative to the height of the image. In your example, the height is approximately 0.00664, indicating that the bounding box height is around 0.664% of the image height.
/// These numbers together specify the position and size of the bounding box that encloses an object within an image. The normalized values allow the annotations to be consistent across different image sizes and resolutions.
/// e.g.
/// 0.6949948072433472 0.6448148488998413 0.03246873617172241 0.006638884544372559
/// </summary>
public static CvatRectangleD FromYolo(double centerX, double centerY, double width, double height) =>
new CvatRectangleD(x: centerX - width / 2, y: centerY - height / 2, width, height);

public static CvatRectangleD FromLTRB(double left, double top, double right, double bottom) =>
new CvatRectangleD(left, top, right - left, bottom - top);
}

// Class definitions for XML deserialization
5 changes: 5 additions & 0 deletions Sources/YoloEase.Cvat.Shared/YoloEase.Cvat.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
<ProjectReference Include="..\PoeShared\PoeShared.csproj" />
</ItemGroup>


<ItemGroup>
<Folder Include="Scaffolding\" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions Sources/YoloEase.Tests/UI/Augmentations/FlipImageEffectFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using PoeShared.Tests.Helpers;
using Shouldly;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using YoloEase.UI.Augmentations;

namespace YoloEase.Tests.UI.Augmentations;

public class FlipImageEffectFixture
{
[Test]
[TestCaseSource(nameof(ShouldMutateBoundingBoxCases))]
public void ShouldMutateBoundingBox(FlipMode flipMode, Size imageSize, RectangleF initialBox, RectangleF expected)
{
//Given
var instance = CreateInstance();
instance.FlipMode = flipMode;

//When
var result = instance.Mutate(imageSize, initialBox);


//Then
result.ShouldBe(expected);
}

public static IEnumerable<NamedTestCaseData> ShouldMutateBoundingBoxCases()
{
// Flip horizontal
yield return new NamedTestCaseData(FlipMode.Horizontal, new Size(10, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "Horizontal - In-place" };
yield return new NamedTestCaseData(FlipMode.Horizontal, new Size(20, 10), new RectangleF(0, 0, 10, 10), new RectangleF(10, 0, 10, 10)) { TestName = "Horizontal - Landscape" };
yield return new NamedTestCaseData(FlipMode.Horizontal, new Size(20, 20), new RectangleF(0, 0, 10, 10), new RectangleF(10, 0, 10, 10)) { TestName = "Horizontal - Square" };
yield return new NamedTestCaseData(FlipMode.Horizontal, new Size(10, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "Horizontal - Portrait" };

// Flip vertical
yield return new NamedTestCaseData(FlipMode.Vertical, new Size(10, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "Vertical - In-place" };
yield return new NamedTestCaseData(FlipMode.Vertical, new Size(20, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "Vertical - Landscape" };
yield return new NamedTestCaseData(FlipMode.Vertical, new Size(20, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 10, 10, 10)) { TestName = "Vertical - Square" };
yield return new NamedTestCaseData(FlipMode.Vertical, new Size(10, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 10, 10, 10)) { TestName = "Vertical - Portrait" };

// No flip
yield return new NamedTestCaseData(FlipMode.None, new Size(10, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "None - No rotation" };
yield return new NamedTestCaseData(FlipMode.None, new Size(20, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "None - Landscape" };
yield return new NamedTestCaseData(FlipMode.None, new Size(20, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "None - Square" };
yield return new NamedTestCaseData(FlipMode.None, new Size(10, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "None - Portrait" };
}

private FlipImageEffect CreateInstance()
{
return new FlipImageEffect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using PoeShared.Tests.Helpers;
using Shouldly;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using YoloEase.UI.Augmentations;

namespace YoloEase.Tests.UI.Augmentations;

public class RotateImageEffectFixture
{
[Test]
[TestCaseSource(nameof(ShouldMutateBoundingBoxCases))]
public void ShouldMutateBoundingBox(RotateMode rotateMode, Size imageSize, RectangleF initialBox, RectangleF expected)
{
//Given
var instance = CreateInstance();
instance.Rotation = rotateMode;

//When
var result = instance.Mutate(imageSize, initialBox);


//Then
result.ShouldBe(expected);
}

public static IEnumerable<NamedTestCaseData> ShouldMutateBoundingBoxCases()
{
// Rotate 90 degrees
yield return new NamedTestCaseData(RotateMode.Rotate90, new Size(10, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "90 degrees - In-place" };
yield return new NamedTestCaseData(RotateMode.Rotate90, new Size(20, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "90 degrees - Landscape" };
yield return new NamedTestCaseData(RotateMode.Rotate90, new Size(20, 20), new RectangleF(0, 0, 10, 10), new RectangleF(10, 0, 10, 10)) { TestName = "90 degrees - Square" };
yield return new NamedTestCaseData(RotateMode.Rotate90, new Size(10, 20), new RectangleF(0, 0, 10, 10), new RectangleF(10, 0, 10, 10)) { TestName = "90 degrees - Portrait" };

// Rotate 180 degrees
yield return new NamedTestCaseData(RotateMode.Rotate180, new Size(10, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "180 degrees - In-place" };
yield return new NamedTestCaseData(RotateMode.Rotate180, new Size(20, 10), new RectangleF(0, 0, 10, 10), new RectangleF(10, 0, 10, 10)) { TestName = "180 degrees - Landscape" };
yield return new NamedTestCaseData(RotateMode.Rotate180, new Size(20, 20), new RectangleF(0, 0, 10, 10), new RectangleF(10, 10, 10, 10)) { TestName = "180 degrees - Square" };
yield return new NamedTestCaseData(RotateMode.Rotate180, new Size(10, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 10, 10, 10)) { TestName = "180 degrees - Portrait" };

// Rotate 270 degrees
yield return new NamedTestCaseData(RotateMode.Rotate270, new Size(10, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "270 degrees - In-place" };
yield return new NamedTestCaseData(RotateMode.Rotate270, new Size(20, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 10, 10, 10)) { TestName = "270 degrees - Landscape" };
yield return new NamedTestCaseData(RotateMode.Rotate270, new Size(20, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 10, 10, 10)) { TestName = "270 degrees - Square" };
yield return new NamedTestCaseData(RotateMode.Rotate270, new Size(10, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "270 degrees - Portrait" };

// Rotate 0 degrees (no rotation)
yield return new NamedTestCaseData(RotateMode.None, new Size(10, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "0 degrees - No rotation" };
yield return new NamedTestCaseData(RotateMode.None, new Size(20, 10), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "0 degrees - Landscape" };
yield return new NamedTestCaseData(RotateMode.None, new Size(20, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "0 degrees - Square" };
yield return new NamedTestCaseData(RotateMode.None, new Size(10, 20), new RectangleF(0, 0, 10, 10), new RectangleF(0, 0, 10, 10)) { TestName = "0 degrees - Portrait" };
}

private RotateImageEffect CreateInstance()
{
return new RotateImageEffect();
}
}
4 changes: 3 additions & 1 deletion Sources/YoloEase.Tests/YoloEase.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(_EyeAurasDotNetTfm)</TargetFramework>
<TargetFramework>$(_EyeAurasDotNetWinRTTfm)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -20,7 +20,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PoeShared.Tests\PoeShared.Tests.csproj" />
<ProjectReference Include="..\YoloEase.Cvat.Shared\YoloEase.Cvat.Shared.csproj" />
<ProjectReference Include="..\YoloEase.UI\YoloEase.UI.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 128f49f

Please sign in to comment.