Skip to content

Commit

Permalink
support for multi threads
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Jun 14, 2024
1 parent 9051cbf commit 6a2a4a1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/Sdcb.PaddleOCR/PaddleOcrClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,19 @@ public bool ShouldRotate180(Mat src)
using Mat resized = ResizePadding(src, Shape);
using Mat normalized = Normalize(resized);

using (PaddleTensor input = _p.GetInputTensor(_p.InputNames[0]))
using PaddlePredictor predictor = _p.Clone();
using (PaddleTensor input = predictor.GetInputTensor(predictor.InputNames[0]))
{
input.Shape = new[] { 1, 3, normalized.Rows, normalized.Cols };
float[] data = PaddleOcrDetector.ExtractMat(normalized);
input.SetData(data);
}
if (!_p.Run())
if (!predictor.Run())
{
throw new Exception("PaddlePredictor(Classifier) run failed.");
}

using (PaddleTensor output = _p.GetOutputTensor(_p.OutputNames[0]))
using (PaddleTensor output = predictor.GetOutputTensor(predictor.OutputNames[0]))
{
float[] softmax = output.GetData<float>();
float score = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/Sdcb.PaddleOCR/PaddleOcrDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,21 @@ public Mat RunRaw(Mat src, out Size resizedSize)
normalized = Normalize(padded);
}

using PaddlePredictor predictor = _p.Clone();
using (Mat _ = normalized)
using (PaddleTensor input = _p.GetInputTensor(_p.InputNames[0]))
using (PaddleTensor input = predictor.GetInputTensor(predictor.InputNames[0]))
{
input.Shape = new[] { 1, 3, normalized.Rows, normalized.Cols };
float[] data = ExtractMat(normalized);
input.SetData(data);
}

if (!_p.Run())
if (!predictor.Run())
{
throw new Exception("PaddlePredictor(Detector) run failed.");
}

using (PaddleTensor output = _p.GetOutputTensor(_p.OutputNames[0]))
using (PaddleTensor output = predictor.GetOutputTensor(predictor.OutputNames[0]))
{
float[] data = output.GetData<float>();
int[] shape = output.Shape;
Expand Down
7 changes: 4 additions & 3 deletions src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private PaddleOcrRecognizerResult[] RunMulti(Mat[] srcs)
return 1.0 * size.Width / size.Height * modelHeight;
}));

using PaddlePredictor predictor = _p.Clone();
Mat[] normalizeds = null!;
try
{
Expand All @@ -131,7 +132,7 @@ private PaddleOcrRecognizerResult[] RunMulti(Mat[] srcs)
})
.ToArray();

using (PaddleTensor input = _p.GetInputTensor(_p.InputNames[0]))
using (PaddleTensor input = predictor.GetInputTensor(predictor.InputNames[0]))
{
int channel = normalizeds[0].Channels();
input.Shape = new[] { normalizeds.Length, channel, modelHeight, maxWidth };
Expand All @@ -147,12 +148,12 @@ private PaddleOcrRecognizerResult[] RunMulti(Mat[] srcs)
}
}

if (!_p.Run())
if (!predictor.Run())
{
throw new Exception($"PaddlePredictor(Recognizer) run failed.");
}

using (PaddleTensor output = _p.GetOutputTensor(_p.OutputNames[0]))
using (PaddleTensor output = predictor.GetOutputTensor(predictor.OutputNames[0]))
{
float[] data = output.GetData<float>();
int[] shape = output.Shape;
Expand Down
11 changes: 6 additions & 5 deletions src/Sdcb.PaddleOCR/PaddleOcrTableRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ public TableDetectionResult Run(Mat src)
Size rawSize = src.Size();
float[] inputData = TablePreprocess(src);

using (PaddleTensor input = _p.GetInputTensor(_p.InputNames[0]))
using PaddlePredictor predictor = _p.Clone();
using (PaddleTensor input = predictor.GetInputTensor(predictor.InputNames[0]))
{
input.Shape = new[] { 1, 3, MaxEdgeSize, MaxEdgeSize };
input.SetData(inputData);
}
if (!_p.Run())
if (!predictor.Run())
{
throw new Exception("PaddlePredictor(Table) run failed.");
}

string[] outputNames = _p.OutputNames;
using (PaddleTensor output0 = _p.GetOutputTensor(outputNames[0]))
using (PaddleTensor output1 = _p.GetOutputTensor(outputNames[1]))
string[] outputNames = predictor.OutputNames;
using (PaddleTensor output0 = predictor.GetOutputTensor(outputNames[0]))
using (PaddleTensor output1 = predictor.GetOutputTensor(outputNames[1]))
{
float[] locations = output0.GetData<float>();
int[] locationShape = output0.Shape;
Expand Down

0 comments on commit 6a2a4a1

Please sign in to comment.