mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-05 16:48:03 +08:00
[C#] add c sharp apis for ppocr (#1405)
* add c sharp apis for ppocr * add example * fix accroding to test * add ocr models * fix * update * update
This commit is contained in:
@@ -74,6 +74,21 @@ public struct FD_TwoDimArrayFloat {
|
||||
public IntPtr data; // FD_OneDimArrayFloat[]
|
||||
}
|
||||
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct FD_TwoDimArrayInt32 {
|
||||
public nuint size;
|
||||
public IntPtr data; // FD_OneDimArrayInt32[]
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct FD_ThreeDimArrayInt32 {
|
||||
public nuint size;
|
||||
public IntPtr data; // FD_TwoDimArrayInt32[]
|
||||
}
|
||||
|
||||
|
||||
public enum FD_ResultType {
|
||||
UNKNOWN_RESULT,
|
||||
CLASSIFY,
|
||||
@@ -135,6 +150,22 @@ public struct FD_OneDimDetectionResult {
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct FD_OCRResult {
|
||||
public FD_TwoDimArrayInt32 boxes;
|
||||
public FD_OneDimArrayCstr text;
|
||||
public FD_OneDimArrayFloat rec_scores;
|
||||
public FD_OneDimArrayFloat cls_scores;
|
||||
public FD_OneDimArrayInt32 cls_labels;
|
||||
public FD_ResultType type;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct FD_OneDimOCRResult {
|
||||
public nuint size;
|
||||
public IntPtr data; // FD_OCRResult[]
|
||||
}
|
||||
|
||||
public struct FD_SegmentationResult {
|
||||
public FD_OneDimArrayUint8 label_map;
|
||||
public FD_OneDimArrayFloat score_map;
|
||||
@@ -157,4 +188,4 @@ public struct FD_OneDimMat {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
748
csharp/fastdeploy/vision/ocr/model.cs
Normal file
748
csharp/fastdeploy/vision/ocr/model.cs
Normal file
@@ -0,0 +1,748 @@
|
||||
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections.Generic;
|
||||
using OpenCvSharp;
|
||||
using fastdeploy.types_internal_c;
|
||||
using fastdeploy.vision;
|
||||
using fastdeploy.vision.ocr;
|
||||
|
||||
namespace fastdeploy {
|
||||
namespace vision {
|
||||
namespace ocr {
|
||||
|
||||
// Recognizer
|
||||
|
||||
public class Recognizer {
|
||||
|
||||
public Recognizer(string model_file, string params_file,
|
||||
string label_path,
|
||||
RuntimeOption custom_option = null,
|
||||
ModelFormat model_format = ModelFormat.PADDLE) {
|
||||
if (custom_option == null) {
|
||||
custom_option = new RuntimeOption();
|
||||
}
|
||||
fd_recognizer_model_wrapper = FD_C_CreateRecognizerWrapper(
|
||||
model_file, params_file, label_path, custom_option.GetWrapperPtr(),
|
||||
model_format);
|
||||
}
|
||||
|
||||
~Recognizer() {
|
||||
FD_C_DestroyRecognizerWrapper(fd_recognizer_model_wrapper);
|
||||
}
|
||||
|
||||
|
||||
public string ModelName() {
|
||||
return "ppocr/ocr_rec";
|
||||
}
|
||||
|
||||
public OCRRecognizerResult Predict(Mat img) {
|
||||
OCRRecognizerResult ocr_recognizer_result = new OCRRecognizerResult();
|
||||
FD_Cstr text = new FD_Cstr();
|
||||
if(! FD_C_RecognizerWrapperPredict(
|
||||
fd_recognizer_model_wrapper, img.CvPtr,
|
||||
ref text, ref ocr_recognizer_result.rec_score))
|
||||
{
|
||||
return null;
|
||||
} // predict
|
||||
ocr_recognizer_result.text = text.data;
|
||||
FD_C_DestroyCstr(ref text);
|
||||
return ocr_recognizer_result;
|
||||
}
|
||||
|
||||
public List<OCRRecognizerResult> BatchPredict(List<Mat> imgs){
|
||||
FD_OneDimMat imgs_in = new FD_OneDimMat();
|
||||
imgs_in.size = (nuint)imgs.Count;
|
||||
// Copy data to unmanaged memory
|
||||
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
mat_ptrs[i] = imgs[i].CvPtr;
|
||||
}
|
||||
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
|
||||
imgs_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
|
||||
mat_ptrs.Length);
|
||||
FD_OneDimArrayCstr fd_texts_list = new FD_OneDimArrayCstr();
|
||||
FD_OneDimArrayFloat fd_rec_scores_list = new FD_OneDimArrayFloat();
|
||||
if (!FD_C_RecognizerWrapperBatchPredict(fd_recognizer_model_wrapper, imgs_in, ref fd_texts_list, ref fd_rec_scores_list)){
|
||||
return null;
|
||||
}
|
||||
|
||||
// copy texts
|
||||
string[] texts = ConvertResult.ConvertCOneDimArrayCstrToStringArray(fd_texts_list);
|
||||
// copy rec_scores
|
||||
float[] rec_scores = new float[fd_rec_scores_list.size];
|
||||
Marshal.Copy(fd_rec_scores_list.data, rec_scores, 0,
|
||||
rec_scores.Length);
|
||||
|
||||
List<OCRRecognizerResult> results_out = new List<OCRRecognizerResult>();
|
||||
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
OCRRecognizerResult result = new OCRRecognizerResult();
|
||||
result.text = texts[i];
|
||||
result.rec_score = rec_scores[i];
|
||||
results_out.Add(result);
|
||||
}
|
||||
FD_C_DestroyOneDimArrayCstr(ref fd_texts_list);
|
||||
FD_C_DestroyOneDimArrayFloat(ref fd_rec_scores_list);
|
||||
Marshal.FreeHGlobal(imgs_in.data);
|
||||
return results_out;
|
||||
}
|
||||
|
||||
public List<OCRRecognizerResult> BatchPredict(List<Mat> imgs, int start_index, int end_index, List<int> indices){
|
||||
FD_OneDimMat imgs_in = new FD_OneDimMat();
|
||||
imgs_in.size = (nuint)imgs.Count;
|
||||
// Copy data to unmanaged memory
|
||||
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
mat_ptrs[i] = imgs[i].CvPtr;
|
||||
}
|
||||
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
|
||||
imgs_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
|
||||
mat_ptrs.Length);
|
||||
FD_OneDimArrayCstr fd_texts_list = new FD_OneDimArrayCstr();
|
||||
FD_OneDimArrayFloat fd_rec_scores_list = new FD_OneDimArrayFloat();
|
||||
FD_OneDimArrayInt32 indices_in = new FD_OneDimArrayInt32();
|
||||
indices_in.size = (uint)indices.Count;
|
||||
int[] indices_array = new int[indices_in.size];
|
||||
indices.CopyTo(indices_array);
|
||||
// Copy data to unmanaged memory
|
||||
size = Marshal.SizeOf(indices_array[0]) * indices_array.Length;
|
||||
indices_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(indices_array, 0, indices_in.data,
|
||||
indices_array.Length);
|
||||
if (!FD_C_RecognizerWrapperBatchPredictWithIndex(fd_recognizer_model_wrapper, imgs_in, ref fd_texts_list, ref fd_rec_scores_list, start_index, end_index, indices_in)){
|
||||
return null;
|
||||
}
|
||||
|
||||
// copy texts
|
||||
string[] texts = ConvertResult.ConvertCOneDimArrayCstrToStringArray(fd_texts_list);
|
||||
// copy rec_scores
|
||||
float[] rec_scores = new float[fd_rec_scores_list.size];
|
||||
Marshal.Copy(fd_rec_scores_list.data, rec_scores, 0,
|
||||
rec_scores.Length);
|
||||
|
||||
List<OCRRecognizerResult> results_out = new List<OCRRecognizerResult>();
|
||||
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
OCRRecognizerResult result = new OCRRecognizerResult();
|
||||
result.text = texts[i];
|
||||
result.rec_score = rec_scores[i];
|
||||
results_out.Add(result);
|
||||
}
|
||||
FD_C_DestroyOneDimArrayCstr(ref fd_texts_list);
|
||||
FD_C_DestroyOneDimArrayFloat(ref fd_rec_scores_list);
|
||||
Marshal.FreeHGlobal(imgs_in.data);
|
||||
Marshal.FreeHGlobal(indices_in.data);
|
||||
return results_out;
|
||||
}
|
||||
|
||||
public bool Initialized() {
|
||||
return FD_C_RecognizerWrapperInitialized(fd_recognizer_model_wrapper);
|
||||
}
|
||||
|
||||
public IntPtr GetWrapperPtr(){
|
||||
return fd_recognizer_model_wrapper;
|
||||
}
|
||||
|
||||
// below are underlying C api
|
||||
private IntPtr fd_recognizer_model_wrapper;
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_CreateRecognizerWrapper")]
|
||||
private static extern IntPtr FD_C_CreateRecognizerWrapper(
|
||||
string model_file, string params_file,string label_path,
|
||||
IntPtr fd_runtime_option_wrapper, ModelFormat model_format);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyRecognizerWrapper")]
|
||||
private static extern void
|
||||
FD_C_DestroyRecognizerWrapper(IntPtr fd_recognizer_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_RecognizerWrapperPredict")]
|
||||
private static extern bool
|
||||
FD_C_RecognizerWrapperPredict(IntPtr fd_recognizer_model_wrapper,
|
||||
IntPtr img,
|
||||
ref FD_Cstr text,
|
||||
ref float rec_score);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyCstr")]
|
||||
private static extern void
|
||||
FD_C_DestroyCstr(ref FD_Cstr fd_cstr);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyOneDimArrayCstr")]
|
||||
private static extern void
|
||||
FD_C_DestroyOneDimArrayCstr(ref FD_OneDimArrayCstr fd_onedim_cstr);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyOneDimArrayFloat")]
|
||||
private static extern void
|
||||
FD_C_DestroyOneDimArrayFloat(ref FD_OneDimArrayFloat fd_onedim_float);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_RecognizerWrapperInitialized")]
|
||||
private static extern bool
|
||||
FD_C_RecognizerWrapperInitialized(IntPtr fd_recognizer_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_RecognizerWrapperBatchPredict")]
|
||||
private static extern bool
|
||||
FD_C_RecognizerWrapperBatchPredict(IntPtr fd_recognizer_model_wrapper,
|
||||
FD_OneDimMat imgs,
|
||||
ref FD_OneDimArrayCstr texts,
|
||||
ref FD_OneDimArrayFloat rec_scores);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_RecognizerWrapperBatchPredictWithIndex")]
|
||||
private static extern bool
|
||||
FD_C_RecognizerWrapperBatchPredictWithIndex(IntPtr fd_recognizer_model_wrapper,
|
||||
FD_OneDimMat imgs,
|
||||
ref FD_OneDimArrayCstr texts,
|
||||
ref FD_OneDimArrayFloat rec_scores,
|
||||
int start_index,
|
||||
int end_index,
|
||||
FD_OneDimArrayInt32 indices);
|
||||
|
||||
}
|
||||
|
||||
// Classifier
|
||||
|
||||
public class Classifier {
|
||||
|
||||
public Classifier(string model_file, string params_file,
|
||||
RuntimeOption custom_option = null,
|
||||
ModelFormat model_format = ModelFormat.PADDLE) {
|
||||
if (custom_option == null) {
|
||||
custom_option = new RuntimeOption();
|
||||
}
|
||||
fd_classifier_model_wrapper = FD_C_CreateClassifierWrapper(
|
||||
model_file, params_file, custom_option.GetWrapperPtr(),
|
||||
model_format);
|
||||
}
|
||||
|
||||
~Classifier() {
|
||||
FD_C_DestroyClassifierWrapper(fd_classifier_model_wrapper);
|
||||
}
|
||||
|
||||
|
||||
public string ModelName() {
|
||||
return "ppocr/ocr_cls";
|
||||
}
|
||||
|
||||
public OCRClassifierResult Predict(Mat img) {
|
||||
OCRClassifierResult ocr_classify_result = new OCRClassifierResult();
|
||||
if(! FD_C_ClassifierWrapperPredict(
|
||||
fd_classifier_model_wrapper, img.CvPtr,
|
||||
ref ocr_classify_result.cls_label, ref ocr_classify_result.cls_score))
|
||||
{
|
||||
return null;
|
||||
} // predict
|
||||
return ocr_classify_result;
|
||||
}
|
||||
|
||||
public List<OCRClassifierResult> BatchPredict(List<Mat> imgs){
|
||||
FD_OneDimMat imgs_in = new FD_OneDimMat();
|
||||
imgs_in.size = (nuint)imgs.Count;
|
||||
// Copy data to unmanaged memory
|
||||
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
mat_ptrs[i] = imgs[i].CvPtr;
|
||||
}
|
||||
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
|
||||
imgs_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
|
||||
mat_ptrs.Length);
|
||||
FD_OneDimArrayInt32 fd_cls_labels_list = new FD_OneDimArrayInt32();
|
||||
FD_OneDimArrayFloat fd_cls_scores_list = new FD_OneDimArrayFloat();
|
||||
if (!FD_C_ClassifierWrapperBatchPredict(fd_classifier_model_wrapper, imgs_in, ref fd_cls_labels_list, ref fd_cls_scores_list)){
|
||||
return null;
|
||||
}
|
||||
|
||||
// copy cls_labels
|
||||
int[] cls_labels = new int[fd_cls_labels_list.size];
|
||||
Marshal.Copy(fd_cls_labels_list.data, cls_labels, 0,
|
||||
cls_labels.Length);
|
||||
// copy cls_scores
|
||||
float[] cls_scores = new float[fd_cls_scores_list.size];
|
||||
Marshal.Copy(fd_cls_scores_list.data, cls_scores, 0,
|
||||
cls_scores.Length);
|
||||
|
||||
List<OCRClassifierResult> results_out = new List<OCRClassifierResult>();
|
||||
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
OCRClassifierResult result = new OCRClassifierResult();
|
||||
result.cls_label = cls_labels[i];
|
||||
result.cls_score = cls_scores[i];
|
||||
results_out.Add(result);
|
||||
}
|
||||
FD_C_DestroyOneDimArrayInt32(ref fd_cls_labels_list);
|
||||
FD_C_DestroyOneDimArrayFloat(ref fd_cls_scores_list);
|
||||
Marshal.FreeHGlobal(imgs_in.data);
|
||||
return results_out;
|
||||
}
|
||||
|
||||
public List<OCRClassifierResult> BatchPredict(List<Mat> imgs, int start_index, int end_index){
|
||||
FD_OneDimMat imgs_in = new FD_OneDimMat();
|
||||
imgs_in.size = (nuint)imgs.Count;
|
||||
// Copy data to unmanaged memory
|
||||
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
mat_ptrs[i] = imgs[i].CvPtr;
|
||||
}
|
||||
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
|
||||
imgs_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
|
||||
mat_ptrs.Length);
|
||||
FD_OneDimArrayInt32 fd_cls_labels_list = new FD_OneDimArrayInt32();
|
||||
FD_OneDimArrayFloat fd_cls_scores_list = new FD_OneDimArrayFloat();
|
||||
if (!FD_C_ClassifierWrapperBatchPredictWithIndex(fd_classifier_model_wrapper, imgs_in, ref fd_cls_labels_list, ref fd_cls_scores_list, start_index, end_index)){
|
||||
return null;
|
||||
}
|
||||
|
||||
// copy cls_labels
|
||||
int[] cls_labels = new int[fd_cls_labels_list.size];
|
||||
Marshal.Copy(fd_cls_labels_list.data, cls_labels, 0,
|
||||
cls_labels.Length);
|
||||
// copy cls_scores
|
||||
float[] cls_scores = new float[fd_cls_scores_list.size];
|
||||
Marshal.Copy(fd_cls_scores_list.data, cls_scores, 0,
|
||||
cls_scores.Length);
|
||||
|
||||
List<OCRClassifierResult> results_out = new List<OCRClassifierResult>();
|
||||
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
OCRClassifierResult result = new OCRClassifierResult();
|
||||
result.cls_label = cls_labels[i];
|
||||
result.cls_score = cls_scores[i];
|
||||
results_out.Add(result);
|
||||
}
|
||||
FD_C_DestroyOneDimArrayInt32(ref fd_cls_labels_list);
|
||||
FD_C_DestroyOneDimArrayFloat(ref fd_cls_scores_list);
|
||||
Marshal.FreeHGlobal(imgs_in.data);
|
||||
return results_out;
|
||||
}
|
||||
|
||||
public bool Initialized() {
|
||||
return FD_C_ClassifierWrapperInitialized(fd_classifier_model_wrapper);
|
||||
}
|
||||
|
||||
public IntPtr GetWrapperPtr(){
|
||||
return fd_classifier_model_wrapper;
|
||||
}
|
||||
|
||||
// below are underlying C api
|
||||
private IntPtr fd_classifier_model_wrapper;
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_CreateClassifierWrapper")]
|
||||
private static extern IntPtr FD_C_CreateClassifierWrapper(
|
||||
string model_file, string params_file,
|
||||
IntPtr fd_runtime_option_wrapper, ModelFormat model_format);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyClassifierWrapper")]
|
||||
private static extern void
|
||||
FD_C_DestroyClassifierWrapper(IntPtr fd_classifier_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_ClassifierWrapperPredict")]
|
||||
private static extern bool
|
||||
FD_C_ClassifierWrapperPredict(IntPtr fd_classifier_model_wrapper,
|
||||
IntPtr img,
|
||||
ref int cls_label,
|
||||
ref float cls_score);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_ClassifierWrapperInitialized")]
|
||||
private static extern bool
|
||||
FD_C_ClassifierWrapperInitialized(IntPtr fd_classifier_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_ClassifierWrapperBatchPredict")]
|
||||
private static extern bool
|
||||
FD_C_ClassifierWrapperBatchPredict(IntPtr fd_classifier_model_wrapper,
|
||||
FD_OneDimMat imgs,
|
||||
ref FD_OneDimArrayInt32 cls_labels,
|
||||
ref FD_OneDimArrayFloat cls_scores);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_ClassifierWrapperBatchPredictWithIndex")]
|
||||
private static extern bool
|
||||
FD_C_ClassifierWrapperBatchPredictWithIndex(IntPtr fd_classifier_model_wrapper,
|
||||
FD_OneDimMat imgs,
|
||||
ref FD_OneDimArrayInt32 cls_labels,
|
||||
ref FD_OneDimArrayFloat cls_scores,
|
||||
int start_index,
|
||||
int end_index);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyOneDimArrayFloat")]
|
||||
private static extern void
|
||||
FD_C_DestroyOneDimArrayFloat(ref FD_OneDimArrayFloat fd_onedim_float);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyOneDimArrayInt32")]
|
||||
private static extern void
|
||||
FD_C_DestroyOneDimArrayInt32(ref FD_OneDimArrayInt32 fd_onedim_int32);
|
||||
|
||||
}
|
||||
|
||||
// DBDetector
|
||||
|
||||
public class DBDetector {
|
||||
|
||||
public DBDetector(string model_file, string params_file,
|
||||
RuntimeOption custom_option = null,
|
||||
ModelFormat model_format = ModelFormat.PADDLE) {
|
||||
if (custom_option == null) {
|
||||
custom_option = new RuntimeOption();
|
||||
}
|
||||
fd_dbdetector_model_wrapper = FD_C_CreateDBDetectorWrapper(
|
||||
model_file, params_file, custom_option.GetWrapperPtr(),
|
||||
model_format);
|
||||
}
|
||||
|
||||
~DBDetector() {
|
||||
FD_C_DestroyDBDetectorWrapper(fd_dbdetector_model_wrapper);
|
||||
}
|
||||
|
||||
|
||||
public string ModelName() {
|
||||
return "ppocr/ocr_det";
|
||||
}
|
||||
|
||||
public OCRDBDetectorResult Predict(Mat img) {
|
||||
OCRDBDetectorResult ocr_detector_result = new OCRDBDetectorResult();
|
||||
FD_TwoDimArrayInt32 fd_box_result = new FD_TwoDimArrayInt32();
|
||||
if(! FD_C_DBDetectorWrapperPredict(
|
||||
fd_dbdetector_model_wrapper, img.CvPtr,
|
||||
ref fd_box_result))
|
||||
{
|
||||
return null;
|
||||
} // predict
|
||||
ocr_detector_result.boxes = new List<int[]>();
|
||||
FD_OneDimArrayInt32[] boxes =
|
||||
new FD_OneDimArrayInt32[fd_box_result.size];
|
||||
for (int i = 0; i < (int)fd_box_result.size; i++) {
|
||||
boxes[i] = (FD_OneDimArrayInt32)Marshal.PtrToStructure(
|
||||
fd_box_result.data + i * Marshal.SizeOf(boxes[0]),
|
||||
typeof(FD_OneDimArrayInt32));
|
||||
int[] box_i = new int[boxes[i].size];
|
||||
Marshal.Copy(boxes[i].data, box_i, 0, box_i.Length);
|
||||
ocr_detector_result.boxes.Add(box_i);
|
||||
}
|
||||
FD_C_DestroyTwoDimArrayInt32(ref fd_box_result);
|
||||
return ocr_detector_result;
|
||||
}
|
||||
|
||||
public List<OCRDBDetectorResult> BatchPredict(List<Mat> imgs){
|
||||
FD_OneDimMat imgs_in = new FD_OneDimMat();
|
||||
imgs_in.size = (nuint)imgs.Count;
|
||||
// Copy data to unmanaged memory
|
||||
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
mat_ptrs[i] = imgs[i].CvPtr;
|
||||
}
|
||||
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
|
||||
imgs_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
|
||||
mat_ptrs.Length);
|
||||
FD_ThreeDimArrayInt32 fd_det_results_list = new FD_ThreeDimArrayInt32();
|
||||
if (!FD_C_DBDetectorWrapperBatchPredict(fd_dbdetector_model_wrapper, imgs_in, ref fd_det_results_list)){
|
||||
return null;
|
||||
}
|
||||
|
||||
List<OCRDBDetectorResult> results_out = new List<OCRDBDetectorResult>();
|
||||
FD_TwoDimArrayInt32[] batch_boxes =
|
||||
new FD_TwoDimArrayInt32[fd_det_results_list.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
OCRDBDetectorResult result = new OCRDBDetectorResult();
|
||||
result.boxes = new List<int[]>();
|
||||
batch_boxes[i] = (FD_TwoDimArrayInt32)Marshal.PtrToStructure(
|
||||
fd_det_results_list.data + i * Marshal.SizeOf(batch_boxes[0]),
|
||||
typeof(FD_TwoDimArrayInt32));
|
||||
FD_OneDimArrayInt32[] boxes =
|
||||
new FD_OneDimArrayInt32[batch_boxes[i].size];
|
||||
for (int j = 0; j < (int)batch_boxes[i].size; j++) {
|
||||
boxes[j] = (FD_OneDimArrayInt32)Marshal.PtrToStructure(
|
||||
batch_boxes[i].data + j * Marshal.SizeOf(boxes[0]),
|
||||
typeof(FD_OneDimArrayInt32));
|
||||
int[] box_j = new int[boxes[j].size];
|
||||
Marshal.Copy(boxes[j].data, box_j, 0, box_j.Length);
|
||||
result.boxes.Add(box_j);
|
||||
}
|
||||
results_out.Add(result);
|
||||
}
|
||||
FD_C_DestroyThreeDimArrayInt32(ref fd_det_results_list);
|
||||
Marshal.FreeHGlobal(imgs_in.data);
|
||||
return results_out;
|
||||
}
|
||||
|
||||
public bool Initialized() {
|
||||
return FD_C_DBDetectorWrapperInitialized(fd_dbdetector_model_wrapper);
|
||||
}
|
||||
|
||||
public IntPtr GetWrapperPtr(){
|
||||
return fd_dbdetector_model_wrapper;
|
||||
}
|
||||
|
||||
// below are underlying C api
|
||||
private IntPtr fd_dbdetector_model_wrapper;
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_CreateDBDetectorWrapper")]
|
||||
private static extern IntPtr FD_C_CreateDBDetectorWrapper(
|
||||
string model_file, string params_file,
|
||||
IntPtr fd_runtime_option_wrapper, ModelFormat model_format);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyDBDetectorWrapper")]
|
||||
private static extern void
|
||||
FD_C_DestroyDBDetectorWrapper(IntPtr fd_dbdetector_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DBDetectorWrapperPredict")]
|
||||
private static extern bool
|
||||
FD_C_DBDetectorWrapperPredict(IntPtr fd_dbdetector_model_wrapper,
|
||||
IntPtr img,
|
||||
ref FD_TwoDimArrayInt32 boxes_result);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DBDetectorWrapperInitialized")]
|
||||
private static extern bool
|
||||
FD_C_DBDetectorWrapperInitialized(IntPtr fd_dbdetector_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DBDetectorWrapperBatchPredict")]
|
||||
private static extern bool
|
||||
FD_C_DBDetectorWrapperBatchPredict(IntPtr fd_dbdetector_model_wrapper,
|
||||
FD_OneDimMat imgs,
|
||||
ref FD_ThreeDimArrayInt32 det_results);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyOneDimArrayInt32")]
|
||||
private static extern void
|
||||
FD_C_DestroyOneDimArrayInt32(ref FD_OneDimArrayInt32 fd_onedim_int32);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyTwoDimArrayInt32")]
|
||||
private static extern void
|
||||
FD_C_DestroyTwoDimArrayInt32(ref FD_TwoDimArrayInt32 fd_twodim_int32);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyThreeDimArrayInt32")]
|
||||
private static extern void
|
||||
FD_C_DestroyThreeDimArrayInt32(ref FD_ThreeDimArrayInt32 fd_threedim_int32);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace pipeline {
|
||||
|
||||
// PPOCRv2
|
||||
|
||||
public class PPOCRv2 {
|
||||
|
||||
public PPOCRv2(DBDetector ppocrv2, Classifier classifier,
|
||||
Recognizer recognizer) {
|
||||
fd_ppocrv2_wrapper = FD_C_CreatePPOCRv2Wrapper(
|
||||
ppocrv2.GetWrapperPtr(),
|
||||
classifier.GetWrapperPtr(),
|
||||
recognizer.GetWrapperPtr());
|
||||
}
|
||||
|
||||
~PPOCRv2() {
|
||||
FD_C_DestroyPPOCRv2Wrapper(fd_ppocrv2_wrapper);
|
||||
}
|
||||
|
||||
|
||||
public string ModelName() {
|
||||
return "PPOCRv2";
|
||||
}
|
||||
|
||||
public OCRResult Predict(Mat img) {
|
||||
FD_OCRResult fd_ocr_result = new FD_OCRResult();
|
||||
if(! FD_C_PPOCRv2WrapperPredict(
|
||||
fd_ppocrv2_wrapper, img.CvPtr,
|
||||
ref fd_ocr_result))
|
||||
{
|
||||
return null;
|
||||
} // predict
|
||||
OCRResult ocr_detector_result = ConvertResult.ConvertCResultToOCRResult(fd_ocr_result);
|
||||
FD_C_DestroyOCRResult(ref fd_ocr_result);
|
||||
return ocr_detector_result;
|
||||
}
|
||||
|
||||
public List<OCRResult> BatchPredict(List<Mat> imgs){
|
||||
FD_OneDimMat imgs_in = new FD_OneDimMat();
|
||||
imgs_in.size = (nuint)imgs.Count;
|
||||
// Copy data to unmanaged memory
|
||||
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
mat_ptrs[i] = imgs[i].CvPtr;
|
||||
}
|
||||
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
|
||||
imgs_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
|
||||
mat_ptrs.Length);
|
||||
FD_OneDimOCRResult fd_ocr_result_array = new FD_OneDimOCRResult();
|
||||
if (!FD_C_PPOCRv2WrapperBatchPredict(fd_ppocrv2_wrapper, imgs_in, ref fd_ocr_result_array)){
|
||||
return null;
|
||||
}
|
||||
List<OCRResult> results_out = new List<OCRResult>();
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
FD_OCRResult fd_ocr_result = (FD_OCRResult)Marshal.PtrToStructure(
|
||||
fd_ocr_result_array.data + i * Marshal.SizeOf(new FD_OCRResult()),
|
||||
typeof(FD_OCRResult));
|
||||
results_out.Add(ConvertResult.ConvertCResultToOCRResult(fd_ocr_result));
|
||||
FD_C_DestroyOCRResult(ref fd_ocr_result);
|
||||
}
|
||||
Marshal.FreeHGlobal(imgs_in.data);
|
||||
return results_out;
|
||||
}
|
||||
|
||||
public bool Initialized() {
|
||||
return FD_C_PPOCRv2WrapperInitialized(fd_ppocrv2_wrapper);
|
||||
}
|
||||
|
||||
// below are underlying C api
|
||||
private IntPtr fd_ppocrv2_wrapper;
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_CreatePPOCRv2Wrapper")]
|
||||
private static extern IntPtr FD_C_CreatePPOCRv2Wrapper(
|
||||
IntPtr det_model, IntPtr cls_model,
|
||||
IntPtr rec_model);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyPPOCRv2Wrapper")]
|
||||
private static extern void
|
||||
FD_C_DestroyPPOCRv2Wrapper(IntPtr fd_ppocrv2_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_PPOCRv2WrapperPredict")]
|
||||
private static extern bool
|
||||
FD_C_PPOCRv2WrapperPredict(IntPtr fd_ppocrv2_model_wrapper,
|
||||
IntPtr img,
|
||||
ref FD_OCRResult result);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_PPOCRv2WrapperInitialized")]
|
||||
private static extern bool
|
||||
FD_C_PPOCRv2WrapperInitialized(IntPtr fd_ppocrv2_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_PPOCRv2WrapperBatchPredict")]
|
||||
private static extern bool
|
||||
FD_C_PPOCRv2WrapperBatchPredict(IntPtr fd_ppocrv2_model_wrapper,
|
||||
FD_OneDimMat imgs,
|
||||
ref FD_OneDimOCRResult batch_result);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyOCRResult")]
|
||||
private static extern void
|
||||
FD_C_DestroyOCRResult(ref FD_OCRResult fd_ocr_result);
|
||||
|
||||
}
|
||||
|
||||
// PPOCRv3
|
||||
|
||||
public class PPOCRv3 {
|
||||
|
||||
public PPOCRv3(DBDetector ppocrv3, Classifier classifier,
|
||||
Recognizer recognizer) {
|
||||
fd_ppocrv3_wrapper = FD_C_CreatePPOCRv3Wrapper(
|
||||
ppocrv3.GetWrapperPtr(),
|
||||
classifier.GetWrapperPtr(),
|
||||
recognizer.GetWrapperPtr());
|
||||
}
|
||||
|
||||
~PPOCRv3() {
|
||||
FD_C_DestroyPPOCRv3Wrapper(fd_ppocrv3_wrapper);
|
||||
}
|
||||
|
||||
|
||||
public string ModelName() {
|
||||
return "PPOCRv3";
|
||||
}
|
||||
|
||||
public OCRResult Predict(Mat img) {
|
||||
FD_OCRResult fd_ocr_result = new FD_OCRResult();
|
||||
if(! FD_C_PPOCRv3WrapperPredict(
|
||||
fd_ppocrv3_wrapper, img.CvPtr,
|
||||
ref fd_ocr_result))
|
||||
{
|
||||
return null;
|
||||
} // predict
|
||||
OCRResult ocr_detector_result = ConvertResult.ConvertCResultToOCRResult(fd_ocr_result);
|
||||
FD_C_DestroyOCRResult(ref fd_ocr_result);
|
||||
return ocr_detector_result;
|
||||
}
|
||||
|
||||
public List<OCRResult> BatchPredict(List<Mat> imgs){
|
||||
FD_OneDimMat imgs_in = new FD_OneDimMat();
|
||||
imgs_in.size = (nuint)imgs.Count;
|
||||
// Copy data to unmanaged memory
|
||||
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
mat_ptrs[i] = imgs[i].CvPtr;
|
||||
}
|
||||
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
|
||||
imgs_in.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
|
||||
mat_ptrs.Length);
|
||||
FD_OneDimOCRResult fd_ocr_result_array = new FD_OneDimOCRResult();
|
||||
if (!FD_C_PPOCRv3WrapperBatchPredict(fd_ppocrv3_wrapper, imgs_in, ref fd_ocr_result_array)){
|
||||
return null;
|
||||
}
|
||||
List<OCRResult> results_out = new List<OCRResult>();
|
||||
for(int i=0;i < (int)imgs.Count; i++){
|
||||
FD_OCRResult fd_ocr_result = (FD_OCRResult)Marshal.PtrToStructure(
|
||||
fd_ocr_result_array.data + i * Marshal.SizeOf(new FD_OCRResult()),
|
||||
typeof(FD_OCRResult));
|
||||
results_out.Add(ConvertResult.ConvertCResultToOCRResult(fd_ocr_result));
|
||||
FD_C_DestroyOCRResult(ref fd_ocr_result);
|
||||
}
|
||||
Marshal.FreeHGlobal(imgs_in.data);
|
||||
return results_out;
|
||||
}
|
||||
|
||||
public bool Initialized() {
|
||||
return FD_C_PPOCRv3WrapperInitialized(fd_ppocrv3_wrapper);
|
||||
}
|
||||
|
||||
// below are underlying C api
|
||||
private IntPtr fd_ppocrv3_wrapper;
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_CreatePPOCRv3Wrapper")]
|
||||
private static extern IntPtr FD_C_CreatePPOCRv3Wrapper(
|
||||
IntPtr det_model, IntPtr cls_model,
|
||||
IntPtr rec_model);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyPPOCRv3Wrapper")]
|
||||
private static extern void
|
||||
FD_C_DestroyPPOCRv3Wrapper(IntPtr fd_ppocrv3_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_PPOCRv3WrapperPredict")]
|
||||
private static extern bool
|
||||
FD_C_PPOCRv3WrapperPredict(IntPtr fd_ppocrv3_model_wrapper,
|
||||
IntPtr img,
|
||||
ref FD_OCRResult result);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_PPOCRv3WrapperInitialized")]
|
||||
private static extern bool
|
||||
FD_C_PPOCRv3WrapperInitialized(IntPtr fd_ppocrv3_model_wrapper);
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_PPOCRv3WrapperBatchPredict")]
|
||||
private static extern bool
|
||||
FD_C_PPOCRv3WrapperBatchPredict(IntPtr fd_ppocrv3_model_wrapper,
|
||||
FD_OneDimMat imgs,
|
||||
ref FD_OneDimOCRResult batch_result);
|
||||
|
||||
[DllImport("fastdeploy.dll",
|
||||
EntryPoint = "FD_C_DestroyOCRResult")]
|
||||
private static extern void
|
||||
FD_C_DestroyOCRResult(ref FD_OCRResult fd_ocr_result);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -130,6 +130,101 @@ public class DetectionResult {
|
||||
|
||||
}
|
||||
|
||||
public class OCRResult {
|
||||
public List<int[]> boxes;
|
||||
public List<string> text;
|
||||
public List<float> rec_scores;
|
||||
public List<float> cls_scores;
|
||||
public List<int> cls_labels;
|
||||
public ResultType type;
|
||||
|
||||
public OCRResult() {
|
||||
this.boxes = new List<int[]>();
|
||||
this.text = new List<string>();
|
||||
this.rec_scores = new List<float>();
|
||||
this.cls_scores = new List<float>();
|
||||
this.cls_labels = new List<int>();
|
||||
this.type = ResultType.OCR;
|
||||
}
|
||||
public string ToString() {
|
||||
string no_result = "";
|
||||
if (boxes.Count > 0) {
|
||||
string information = "";
|
||||
for (int n = 0; n < boxes.Count; n++) {
|
||||
information = information + "det boxes: [";
|
||||
for (int i = 0; i < 4; i++) {
|
||||
information = information + "[" + boxes[n][i * 2].ToString() + "," +
|
||||
boxes[n][i * 2 + 1].ToString() + "]";
|
||||
|
||||
if (i != 3) {
|
||||
information = information + ",";
|
||||
}
|
||||
}
|
||||
information = information + "]";
|
||||
|
||||
if (rec_scores.Count > 0) {
|
||||
information = information + "rec text: " + text[n] + " rec score:" +
|
||||
rec_scores[n].ToString() + " ";
|
||||
}
|
||||
if (cls_labels.Count > 0) {
|
||||
information = information + "cls label: " + cls_labels[n].ToString() +
|
||||
" cls score: " + cls_scores[n].ToString();
|
||||
}
|
||||
information = information + "\n";
|
||||
}
|
||||
return information;
|
||||
|
||||
} else if (boxes.Count == 0 && rec_scores.Count > 0 &&
|
||||
cls_scores.Count > 0) {
|
||||
string information="";
|
||||
for (int i = 0; i < rec_scores.Count; i++) {
|
||||
information = information + "rec text: " + text[i] + " rec score:" +
|
||||
rec_scores[i].ToString() + " ";
|
||||
information = information + "cls label: " + cls_labels[i].ToString() +
|
||||
" cls score: " + cls_scores[i].ToString();
|
||||
information = information + "\n";
|
||||
}
|
||||
return information;
|
||||
} else if (boxes.Count == 0 && rec_scores.Count == 0 &&
|
||||
cls_scores.Count > 0) {
|
||||
string information="";
|
||||
for (int i = 0; i < cls_scores.Count; i++) {
|
||||
information = information + "cls label: " + cls_labels[i].ToString() +
|
||||
" cls score: " + cls_scores[i].ToString();
|
||||
information = information + "\n";
|
||||
}
|
||||
return information;
|
||||
} else if (boxes.Count == 0 && rec_scores.Count > 0 &&
|
||||
cls_scores.Count == 0) {
|
||||
string information="";
|
||||
for (int i = 0; i < rec_scores.Count; i++) {
|
||||
information = information + "rec text: " + text[i] + " rec score:" +
|
||||
rec_scores[i].ToString() + " ";
|
||||
information = information + "\n";
|
||||
}
|
||||
return information;
|
||||
}
|
||||
|
||||
no_result = no_result + "No Results!";
|
||||
return no_result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class OCRClassifierResult{
|
||||
public int cls_label;
|
||||
public float cls_score;
|
||||
}
|
||||
|
||||
public class OCRDBDetectorResult{
|
||||
public List<int[]> boxes;
|
||||
}
|
||||
|
||||
public class OCRRecognizerResult{
|
||||
public string text;
|
||||
public float rec_score;
|
||||
}
|
||||
|
||||
public class SegmentationResult{
|
||||
public List<byte> label_map;
|
||||
public List<float> score_map;
|
||||
@@ -144,7 +239,6 @@ public class SegmentationResult{
|
||||
this.type = ResultType.SEGMENTATION;
|
||||
}
|
||||
|
||||
|
||||
public string ToString() {
|
||||
string information;
|
||||
information = "SegmentationResult Image masks 10 rows x 10 cols: \n";
|
||||
@@ -364,6 +458,115 @@ public class ConvertResult {
|
||||
return detection_result;
|
||||
}
|
||||
|
||||
// OCRResult
|
||||
public static FD_OCRResult
|
||||
ConvertOCRResultToCResult(OCRResult ocr_result) {
|
||||
FD_OCRResult fd_ocr_result = new FD_OCRResult();
|
||||
|
||||
// copy boxes
|
||||
int boxes_coordinate_dim = 8;
|
||||
int size;
|
||||
fd_ocr_result.boxes.size = (uint)ocr_result.boxes.Count;
|
||||
FD_OneDimArrayInt32[] boxes =
|
||||
new FD_OneDimArrayInt32[fd_ocr_result.boxes.size];
|
||||
// Copy each box
|
||||
for (int i = 0; i < (int)fd_ocr_result.boxes.size; i++) {
|
||||
boxes[i].size = (uint)ocr_result.boxes[i].Length;
|
||||
int[] boxes_i = new int[boxes_coordinate_dim];
|
||||
ocr_result.boxes[i].CopyTo(boxes_i, 0);
|
||||
size = Marshal.SizeOf(boxes_i[0]) * boxes_i.Length;
|
||||
boxes[i].data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(boxes_i, 0, boxes[i].data, boxes_i.Length);
|
||||
}
|
||||
// Copy data to unmanaged memory
|
||||
size = Marshal.SizeOf(boxes[0]) * boxes.Length;
|
||||
fd_ocr_result.boxes.data = Marshal.AllocHGlobal(size);
|
||||
for (int i = 0; i < boxes.Length; i++) {
|
||||
Marshal.StructureToPtr(
|
||||
boxes[i],
|
||||
fd_ocr_result.boxes.data + i * Marshal.SizeOf(boxes[0]), true);
|
||||
}
|
||||
|
||||
// copy text
|
||||
fd_ocr_result.text = ConvertStringArrayToCOneDimArrayCstr(ocr_result.text.ToArray());
|
||||
|
||||
// copy rec_scores
|
||||
fd_ocr_result.rec_scores.size = (uint)ocr_result.rec_scores.Count;
|
||||
float[] rec_scores = new float[fd_ocr_result.rec_scores.size];
|
||||
// Copy data from Link to Array
|
||||
ocr_result.rec_scores.CopyTo(rec_scores);
|
||||
// Copy data to unmanaged memory
|
||||
size = Marshal.SizeOf(rec_scores[0]) * rec_scores.Length;
|
||||
fd_ocr_result.rec_scores.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(rec_scores, 0, fd_ocr_result.rec_scores.data, rec_scores.Length);
|
||||
|
||||
// copy cls_scores
|
||||
fd_ocr_result.cls_scores.size = (uint)ocr_result.cls_scores.Count;
|
||||
float[] cls_scores = new float[fd_ocr_result.cls_scores.size];
|
||||
// Copy data from Link to Array
|
||||
ocr_result.cls_scores.CopyTo(cls_scores);
|
||||
// Copy data to unmanaged memory
|
||||
size = Marshal.SizeOf(cls_scores[0]) * cls_scores.Length;
|
||||
fd_ocr_result.cls_scores.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(cls_scores, 0, fd_ocr_result.cls_scores.data, cls_scores.Length);
|
||||
|
||||
// copy cls_labels
|
||||
fd_ocr_result.cls_labels.size = (uint)ocr_result.cls_labels.Count;
|
||||
int[] cls_labels = new int[fd_ocr_result.cls_labels.size];
|
||||
// Copy data from Link to Array
|
||||
ocr_result.cls_labels.CopyTo(cls_labels);
|
||||
// Copy data to unmanaged memory
|
||||
size = Marshal.SizeOf(cls_labels[0]) * cls_labels.Length;
|
||||
fd_ocr_result.cls_labels.data = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(cls_labels, 0, fd_ocr_result.cls_labels.data, cls_labels.Length);
|
||||
|
||||
fd_ocr_result.type = (FD_ResultType)ocr_result.type;
|
||||
return fd_ocr_result;
|
||||
}
|
||||
|
||||
public static OCRResult
|
||||
ConvertCResultToOCRResult(FD_OCRResult fd_ocr_result) {
|
||||
OCRResult ocr_result = new OCRResult();
|
||||
|
||||
// copy boxes
|
||||
ocr_result.boxes = new List<int[]>();
|
||||
FD_OneDimArrayInt32[] boxes =
|
||||
new FD_OneDimArrayInt32[fd_ocr_result.boxes.size];
|
||||
for (int i = 0; i < (int)fd_ocr_result.boxes.size; i++) {
|
||||
boxes[i] = (FD_OneDimArrayInt32)Marshal.PtrToStructure(
|
||||
fd_ocr_result.boxes.data + i * Marshal.SizeOf(boxes[0]),
|
||||
typeof(FD_OneDimArrayInt32));
|
||||
int[] box_i = new int[boxes[i].size];
|
||||
Marshal.Copy(boxes[i].data, box_i, 0, box_i.Length);
|
||||
ocr_result.boxes.Add(box_i);
|
||||
}
|
||||
|
||||
// copy text
|
||||
string[] texts = ConvertCOneDimArrayCstrToStringArray(fd_ocr_result.text);
|
||||
ocr_result.text = new List<string>(texts);
|
||||
|
||||
// copy rec_scores
|
||||
float[] rec_scores = new float[fd_ocr_result.rec_scores.size];
|
||||
Marshal.Copy(fd_ocr_result.rec_scores.data, rec_scores, 0,
|
||||
rec_scores.Length);
|
||||
ocr_result.rec_scores = new List<float>(rec_scores);
|
||||
|
||||
// copy cls_scores
|
||||
float[] cls_scores = new float[fd_ocr_result.cls_scores.size];
|
||||
Marshal.Copy(fd_ocr_result.cls_scores.data, cls_scores, 0,
|
||||
cls_scores.Length);
|
||||
ocr_result.cls_scores = new List<float>(cls_scores);
|
||||
|
||||
// copy cls_labels
|
||||
int[] cls_labels = new int[fd_ocr_result.cls_labels.size];
|
||||
Marshal.Copy(fd_ocr_result.cls_labels.data, cls_labels, 0,
|
||||
cls_labels.Length);
|
||||
ocr_result.cls_labels = new List<int>(cls_labels);
|
||||
|
||||
ocr_result.type = (ResultType)fd_ocr_result.type;
|
||||
return ocr_result;
|
||||
}
|
||||
|
||||
public static SegmentationResult
|
||||
ConvertCResultToSegmentationResult(FD_SegmentationResult fd_segmentation_result){
|
||||
SegmentationResult segmentation_result = new SegmentationResult();
|
||||
|
@@ -50,6 +50,14 @@ public class Visualize {
|
||||
return new Mat(result_ptr);
|
||||
}
|
||||
|
||||
public static Mat VisOcr(Mat im, OCRResult ocr_result){
|
||||
FD_OCRResult fd_ocr_result =
|
||||
ConvertResult.ConvertOCRResultToCResult(ocr_result);
|
||||
IntPtr result_ptr =
|
||||
FD_C_VisOcr(im.CvPtr, ref fd_ocr_result);
|
||||
return new Mat(result_ptr);
|
||||
}
|
||||
|
||||
public static Mat VisSegmentation(Mat im,
|
||||
SegmentationResult segmentation_result,
|
||||
float weight = 0.5f){
|
||||
@@ -74,6 +82,10 @@ public class Visualize {
|
||||
ref FD_OneDimArrayCstr labels,
|
||||
float score_threshold, int line_size, float font_size);
|
||||
|
||||
[DllImport("fastdeploy.dll", EntryPoint = "FD_C_VisOcr")]
|
||||
private static extern IntPtr
|
||||
FD_C_VisOcr(IntPtr im, ref FD_OCRResult fd_ocr_result);
|
||||
|
||||
[DllImport("fastdeploy.dll", EntryPoint = "FD_C_VisSegmentation")]
|
||||
private static extern IntPtr
|
||||
FD_C_VisSegmentation(IntPtr im, ref FD_SegmentationResult fd_segmentation_result, float weight);
|
||||
|
Reference in New Issue
Block a user