Skip to content

Commit 25c6f91

Browse files
committed
Add V6 ONNX directory loading for PaddleOCR models
- Route V6 detection/classification directory models to ONNX file loaders - Add explicit `RecognizationModel.FromV6Directory` for V6 ONNX recognition - Keep recognition `FromDirectory` semantics tied to label-based Paddle models - Remove obsolete `FullOcrModel.FromDirectory` overloads - Add focused tests for V6 and non-V6 directory model selection
1 parent df3e37e commit 25c6f91

5 files changed

Lines changed: 107 additions & 39 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Sdcb.OpenVINO.PaddleOCR.Models;
2+
using Sdcb.OpenVINO.PaddleOCR.Models.Details;
3+
4+
namespace Sdcb.OpenVINO.PaddleOCR.Tests;
5+
6+
public class FromDirectoryModelTest
7+
{
8+
[Fact]
9+
public void DetectionV6FromDirectoryUsesOnnxModel()
10+
{
11+
DetectionModel model = DetectionModel.FromDirectory("models/det", ModelVersion.V6);
12+
13+
Assert.IsType<FileOnnxDetectionModel>(model);
14+
}
15+
16+
[Fact]
17+
public void DetectionNonV6FromDirectoryUsesPaddleModel()
18+
{
19+
DetectionModel model = DetectionModel.FromDirectory("models/det", ModelVersion.V4);
20+
21+
Assert.IsType<FileDetectionModel>(model);
22+
}
23+
24+
[Fact]
25+
public void ClassificationV6FromDirectoryUsesOnnxDefaults()
26+
{
27+
ClassificationModel model = ClassificationModel.FromDirectory("models/cls", ModelVersion.V6);
28+
29+
FileOnnxClassificationModel onnxModel = Assert.IsType<FileOnnxClassificationModel>(model);
30+
Assert.Equal(new NCHW(-1, 3, 80, 160), onnxModel.Shape);
31+
Assert.Equal(ClassificationPreprocessMode.ImageNetRgb, onnxModel.PreprocessMode);
32+
Assert.Equal(ClassificationResizeMode.DirectResize, onnxModel.ResizeMode);
33+
}
34+
35+
[Fact]
36+
public void ClassificationNonV6FromDirectoryUsesPaddleModel()
37+
{
38+
ClassificationModel model = ClassificationModel.FromDirectory("models/cls", ModelVersion.V2);
39+
40+
Assert.IsType<FileClassificationModel>(model);
41+
}
42+
43+
[Fact]
44+
public void RecognizationFromV6DirectoryUsesOnnxInferenceYml()
45+
{
46+
string directoryPath = CreateV6RecognitionDirectory();
47+
48+
RecognizationModel model = RecognizationModel.FromV6Directory(directoryPath);
49+
50+
FileOnnxRecognizationModel onnxModel = Assert.IsType<FileOnnxRecognizationModel>(model);
51+
Assert.Equal("A", onnxModel.GetLabelByIndex(1));
52+
}
53+
54+
[Fact]
55+
public void RecognizationNonV6FromDirectoryUsesPaddleModel()
56+
{
57+
string directoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
58+
Directory.CreateDirectory(directoryPath);
59+
string labelPath = Path.Combine(directoryPath, "labels.txt");
60+
File.WriteAllText(labelPath, "A");
61+
62+
RecognizationModel model = RecognizationModel.FromDirectory(directoryPath, labelPath, ModelVersion.V4);
63+
64+
Assert.IsType<FileRecognizationModel>(model);
65+
}
66+
67+
[Fact]
68+
public void RecognizationFromDirectoryKeepsPaddleModelSemanticsForV6()
69+
{
70+
string directoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
71+
Directory.CreateDirectory(directoryPath);
72+
string labelPath = Path.Combine(directoryPath, "labels.txt");
73+
File.WriteAllText(labelPath, "A");
74+
75+
RecognizationModel model = RecognizationModel.FromDirectory(directoryPath, labelPath, ModelVersion.V6);
76+
77+
Assert.IsType<FileRecognizationModel>(model);
78+
}
79+
80+
private static string CreateV6RecognitionDirectory()
81+
{
82+
string directoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
83+
Directory.CreateDirectory(directoryPath);
84+
File.WriteAllText(Path.Combine(directoryPath, "inference.yml"),
85+
"PostProcess:\n" +
86+
" character_dict:\n" +
87+
" - A\n");
88+
return directoryPath;
89+
}
90+
}

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR/Models/ClassificationModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ protected ClassificationModel(ModelVersion version) : base(version)
4444
/// <param name="directoryPath">The path to the directory containing the model files</param>
4545
/// <param name="version">The version of the classification model.</param>
4646
/// <returns>a new ClassificationModel object</returns>
47-
public static ClassificationModel FromDirectory(string directoryPath, ModelVersion version = ModelVersion.V2) => new FileClassificationModel(directoryPath, version);
47+
public static ClassificationModel FromDirectory(string directoryPath, ModelVersion version = ModelVersion.V2) => version switch
48+
{
49+
ModelVersion.V6 => new FileOnnxClassificationModel(directoryPath, version),
50+
_ => new FileClassificationModel(directoryPath, version),
51+
};
4852

4953
/// <inheritdoc/>
5054
public override void AfterReadModel(Model model)

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR/Models/DetectionModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ public DetectionModel(ModelVersion version) : base(version)
2323
/// <param name="directoryPath">The directory path where model files are located.</param>
2424
/// <param name="version">The version of detection model.</param>
2525
/// <returns>An instance of the DetectionModel class.</returns>
26-
public static DetectionModel FromDirectory(string directoryPath, ModelVersion version) => new FileDetectionModel(directoryPath, version);
26+
public static DetectionModel FromDirectory(string directoryPath, ModelVersion version) => version switch
27+
{
28+
ModelVersion.V6 => new FileOnnxDetectionModel(directoryPath, version),
29+
_ => new FileDetectionModel(directoryPath, version),
30+
};
2731
}

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR/Models/FullOcrModel.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System;
2-
using System.IO;
3-
41
namespace Sdcb.OpenVINO.PaddleOCR.Models;
52

63
/// <summary>
@@ -81,38 +78,4 @@ public FullOcrModel(
8178
/// Gets or sets the document orientation classification model.
8279
/// </summary>
8380
public DocumentOrientationClassificationModel? DocumentOrientationModel { get; init; }
84-
85-
/// <summary>
86-
/// Creates an instance of the <see cref="FullOcrModel"/> class from a directory that contains OCR model files.
87-
/// </summary>
88-
/// <param name="modelFolderPath">The path to the model directory.</param>
89-
/// <param name="labelFilePath">The path to the label file.</param>
90-
/// <param name="version">The OCR model version.</param>
91-
/// <returns>A new instance of <see cref="FullOcrModel"/>.</returns>
92-
[Obsolete("This method is deprecated, use 'FromDirectory(string detectionModelDir, string classificationModelDir, string recognitionModelDir, string labelFilePath, ModelVersion version)' instead.")]
93-
public static FullOcrModel FromDirectory(string modelFolderPath, string labelFilePath, ModelVersion version)
94-
{
95-
return new FullOcrModel(
96-
DetectionModel.FromDirectory(Path.Combine(modelFolderPath, "det"), version),
97-
ClassificationModel.FromDirectory(Path.Combine(modelFolderPath, "cls")),
98-
RecognizationModel.FromDirectory(Path.Combine(modelFolderPath, "rec"), labelFilePath, version));
99-
}
100-
101-
/// <summary>
102-
/// Creates an instance of the <see cref="FullOcrModel"/> class from directories that contain OCR model files.
103-
/// </summary>
104-
/// <param name="detectionModelDir">The path to the detection model directory.</param>
105-
/// <param name="classificationModelDir">The path to the classification model directory.</param>
106-
/// <param name="recognitionModelDir">The path to the recognition model directory.</param>
107-
/// <param name="labelFilePath">The path to the label file.</param>
108-
/// <param name="version">The OCR model version.</param>
109-
/// <returns>A new instance of <see cref="FullOcrModel"/>.</returns>
110-
[Obsolete("This method is deprecated, use 'FromDirectory(string detectionModelPath, string classificationModelPath, string recognitionModelPath, string labelFilePath, ModelVersion version)' instead.")]
111-
public static FullOcrModel FromDirectory(string detectionModelDir, string classificationModelDir, string recognitionModelDir, string labelFilePath, ModelVersion version)
112-
{
113-
return new FullOcrModel(
114-
DetectionModel.FromDirectory(detectionModelDir, version),
115-
ClassificationModel.FromDirectory(classificationModelDir),
116-
RecognizationModel.FromDirectory(recognitionModelDir, labelFilePath, version));
117-
}
11881
}

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR/Models/RecognizationModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,11 @@ public static string GetLabelByIndex(int i, IReadOnlyList<string> labels)
6060
/// <param name="version">The version of recognition model.</param>
6161
/// <returns>The RecognizationModel object created with the specified directory path, label path and model version.</returns>
6262
public static RecognizationModel FromDirectory(string directoryPath, string labelPath, ModelVersion version) => new FileRecognizationModel(directoryPath, labelPath, version);
63+
64+
/// <summary>
65+
/// Create a V6 ONNX RecognizationModel object with the specified directory path.
66+
/// </summary>
67+
/// <param name="directoryPath">The directory path of the V6 ONNX recognition model.</param>
68+
/// <returns>The V6 ONNX RecognizationModel object created with the specified directory path.</returns>
69+
public static RecognizationModel FromV6Directory(string directoryPath) => new FileOnnxRecognizationModel(directoryPath, ModelVersion.V6);
6370
}

0 commit comments

Comments
 (0)