Skip to content

Commit

Permalink
Merge from upsream
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Jun 30, 2023
2 parents 2190d16 + 16607fe commit 3e91cb8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Please checkout [this page 📄](https://github.com/sdcb/PaddleSharp/releases).

### Infrastructure packages 🏗️

| NuGet Package 💼 | Version 📌 | Description 📚 |
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------- |
| NuGet Package 💼 | Version 📌 | Description 📚 |
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------- |
| Sdcb.PaddleInference | [![NuGet](https://img.shields.io/nuget/v/Sdcb.PaddleInference.svg)](https://nuget.org/packages/Sdcb.PaddleInference) | Paddle Inference C API .NET binding ⚙️ |
| Sdcb.PaddleInference.runtime.win64.openblas | [![NuGet](https://img.shields.io/nuget/v/Sdcb.PaddleInference.runtime.win64.openblas.svg)](https://nuget.org/packages/Sdcb.PaddleInference.runtime.win64.openblas) | Paddle Inference native windows-x64-openblas binding 🔗 |
| Sdcb.PaddleInference.runtime.win64.mkl | [![NuGet](https://img.shields.io/nuget/v/Sdcb.PaddleInference.runtime.win64.mkl.svg)](https://nuget.org/packages/Sdcb.PaddleInference.runtime.win64.mkl) | Paddle Inference native windows-x64-mkldnn binding 🔗 |
| Sdcb.PaddleInference.runtime.win64.openblas | [![NuGet](https://img.shields.io/nuget/v/Sdcb.PaddleInference.runtime.win64.openblas.svg)](https://nuget.org/packages/Sdcb.PaddleInference.runtime.win64.openblas) | Paddle Inference native windows-x64-openblas binding 🔗 |

**Note**: Linux does not need a native binding `NuGet` package like windows(`Sdcb.PaddleInference.runtime.win64.mkl`), instead, you can/should based from a [Dockerfile 🐳](https://hub.docker.com/r/sdflysha/dotnet6-focal-paddle2.2.2) to development:

Expand Down
8 changes: 4 additions & 4 deletions build/00-common.linq
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ static ProjectVersion[] Projects = new[]
{
new ProjectVersion("Sdcb.PaddleInference", "2.4.1.3"),
new ProjectVersion("Sdcb.PaddleOCR", "2.6.0.5"),
new ProjectVersion("Sdcb.PaddleOCR.Models.Online", "2.6.0.3"),
new ProjectVersion("Sdcb.PaddleOCR.Models.LocalV3", "2.6.0.3"),
new ProjectVersion("Sdcb.PaddleDetection", "2.3.0"),
new ProjectVersion("Sdcb.RotationDetector", "1.0.0"),
new ProjectVersion("Sdcb.PaddleOCR.Models.Online", "2.6.0.5"),
new ProjectVersion("Sdcb.PaddleOCR.Models.LocalV3", "2.6.0.5"),
new ProjectVersion("Sdcb.PaddleDetection", "2.3.1"),
new ProjectVersion("Sdcb.RotationDetector", "1.0.1"),
};

static async Task DownloadFile(Uri uri, string localFile, CancellationToken cancellationToken = default)
Expand Down
25 changes: 20 additions & 5 deletions src/Sdcb.RotationDetector/Rotation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
namespace Sdcb.RotationDetector;

/// <summary>
/// Enum representing the degrees of rotation.
/// </summary>
public enum RotationDegree
{
_0,
_90,
_180,
_270,
}
/// <summary>
/// Represents the 0-degree rotation angle.
/// </summary>
_0,
/// <summary>
/// Represents the 90-degree rotation angle.
/// </summary>
_90,
/// <summary>
/// Represents the 180-degree rotation angle.
/// </summary>
_180,
/// <summary>
/// Represents the 270-degree rotation angle.
/// </summary>
_270,
}
22 changes: 22 additions & 0 deletions src/Sdcb.RotationDetector/RotationDetectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@

namespace Sdcb.RotationDetector;

/// <summary>
/// A base abstract class for rotation detection models.
/// </summary>
public abstract class RotationDetectionModel
{
/// <summary>
/// Gets the shape of the input.
/// </summary>
public abstract InputShape Shape { get; }

/// <summary>
/// Creates a PaddleConfig.
/// </summary>
/// <returns>A PaddleConfig instance suitable for the model.</returns>
public abstract PaddleConfig CreateConfig();

/// <summary>
/// The default shape of the input.
/// </summary>
public static InputShape DefaultShape = new(3, 224, 224);

/// <summary>
/// Load a RotationDetectionModel from a directory.
/// </summary>
/// <param name="directoryPath">The full path to the directory where the model resides.</param>
/// <returns>A rotation detection model.</returns>
public static RotationDetectionModel FromDirectory(string directoryPath) => new FileRotationDetectionModel(directoryPath);

/// <summary>
/// An embedded resource rotation detection model.
/// </summary>
public static RotationDetectionModel EmbeddedDefault => new EmbeddedResourceDetectionModel();
}
16 changes: 12 additions & 4 deletions src/Sdcb.RotationDetector/RotationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@

namespace Sdcb.RotationDetector;

/// <summary>
/// Represents a rotation result containing the <see cref="Rotation"/> degree and the confidence <see cref="Score"/> of the rotation prediction.
/// </summary>
public record RotationResult(RotationDegree Rotation, float Score)
{
/// <summary>
/// Implicitly converts the <see cref="RotationResult"/> to a <see cref="RotationDegree"/>.
/// </summary>
/// <param name="r">The <see cref="RotationResult"/> to convert.</param>
/// <returns>The <see cref="RotationDegree"/> value of the <paramref name="r"/> parameter.</returns>
public static implicit operator RotationDegree(RotationResult r) => r.Rotation;

/// <summary>
/// Restore src back to non-rotated based on <see cref="Rotation"/>
/// Restores the image the <paramref name="src"/> parameter is pointing to to its original non-rotated state based on the <see cref="Rotation"/> property of the <see cref="RotationResult"/>.
/// </summary>
/// <param name="src">Note: src will changed if it's rotated.</param>
/// <returns>The original reference to src parameter.</returns>
/// <param name="src">The image to restore.</param>
/// <returns>The original reference to the <paramref name="src"/> parameter.</returns>
public Mat RestoreRotationInPlace(Mat src)
{
if (src.Empty())
Expand All @@ -35,4 +43,4 @@ public Mat RestoreRotationInPlace(Mat src)

return src;
}
}
}
21 changes: 9 additions & 12 deletions tests/Sdcb.PaddleOCR.Tests/OfflineModelsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public async Task FastCheckOCR()

using (PaddleOcrAll all = new(model)
{
AllowRotateDetection = true, /* 允许识别有角度的文字 */
Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */
AllowRotateDetection = true,
Enable180Classification = false,
})
{
// Load local file by following code:
Expand Down Expand Up @@ -60,16 +60,13 @@ public async Task QueuedOCR()
sampleImageData = await http.GetByteArrayAsync(sampleImageUrl);
}

using QueuedPaddleOcrAll all = new (() => new PaddleOcrAll(model) // 如果使用GPU,请用改成:PaddleOcrAll(model, PaddleDevice.Gpu())
{
AllowRotateDetection = true, /* 允许识别有角度的文字 */
Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */
},
consumerCount: 4, // 消费者线程数量
boundedCapacity: 64 // 队列最大数量
);
all.WaitFactoryReady(); // 可以不需要,表示等待所有的消费者被创建

using QueuedPaddleOcrAll all = new(() => new PaddleOcrAll(model)
{
AllowRotateDetection = true,
Enable180Classification = false,
}, consumerCount: 1, boundedCapacity: 64);
all.WaitFactoryReady();

{
// Load local file by following code:
// using (Mat src2 = Cv2.ImRead(@"C:\test.jpg"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="OpenCvSharp4" Version="4.7.0.20230115" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.6.0.20220608" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.7.0.20230115" />
<PackageReference Include="Sdcb.PaddleInference.runtime.win64.mkl" Version="2.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit 3e91cb8

Please sign in to comment.