diff --git a/c_api/fastdeploy_capi/core/fd_type.cc b/c_api/fastdeploy_capi/core/fd_type.cc index 9c0e7c93c..c3a742778 100644 --- a/c_api/fastdeploy_capi/core/fd_type.cc +++ b/c_api/fastdeploy_capi/core/fd_type.cc @@ -22,6 +22,36 @@ extern "C" { #endif +// FD_C_OneDimArrayUint8 +DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(OneDimArrayUint8) +// FD_C_OneDimArrayInt8 +DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(OneDimArrayInt8) +// FD_C_OneDimArrayInt32 +DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(OneDimArrayInt32) +// FD_C_OneDimArraySize +DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(OneDimArraySize) +// FD_C_OneDimArrayInt64 +DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(OneDimArrayInt64) +// FD_C_OneDimArrayFloat +DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(OneDimArrayFloat) +// FD_C_Cstr +DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(Cstr) +// FD_C_OneDimArrayCstr +DECLARE_AND_IMPLEMENT_FD_TYPE_TWODIMARRAY(OneDimArrayCstr, Cstr) +// FD_C_TwoDimArraySize +DECLARE_AND_IMPLEMENT_FD_TYPE_TWODIMARRAY(TwoDimArraySize, OneDimArraySize) +// FD_C_TwoDimArrayInt8 +DECLARE_AND_IMPLEMENT_FD_TYPE_TWODIMARRAY(TwoDimArrayInt8, OneDimArrayInt8) +// FD_C_TwoDimArrayInt32 +DECLARE_AND_IMPLEMENT_FD_TYPE_TWODIMARRAY(TwoDimArrayInt32, OneDimArrayInt32) +// FD_C_ThreeDimArrayInt32 +DECLARE_AND_IMPLEMENT_FD_TYPE_THREEDIMARRAY(ThreeDimArrayInt32, + TwoDimArrayInt32) +// FD_C_TwoDimArrayFloat +DECLARE_AND_IMPLEMENT_FD_TYPE_TWODIMARRAY(TwoDimArrayFloat, OneDimArrayFloat) +// FD_C_OneDimMat +DECLARE_AND_IMPLEMENT_FD_TYPE_TWODIMARRAY(OneDimMat, Mat) + FD_C_Mat FD_C_Imread(const char* imgpath) { cv::Mat image = cv::imread(imgpath); return new cv::Mat(image); diff --git a/c_api/fastdeploy_capi/core/fd_type.h b/c_api/fastdeploy_capi/core/fd_type.h index cee401447..640ace890 100644 --- a/c_api/fastdeploy_capi/core/fd_type.h +++ b/c_api/fastdeploy_capi/core/fd_type.h @@ -96,6 +96,57 @@ typedef struct FD_C_OneDimMat { extern "C" { #endif +#define DECLARE_DESTROY_FD_TYPE_FUNCTION(typename) FASTDEPLOY_CAPI_EXPORT extern void FD_C_Destroy##typename (__fd_take FD_C_##typename *) +#define DECLARE_AND_IMPLEMENT_FD_TYPE_ONEDIMARRAY(typename) void FD_C_Destroy##typename (__fd_take FD_C_##typename * ptr) \ + { \ + delete[] ptr->data; \ + } + +#define DECLARE_AND_IMPLEMENT_FD_TYPE_TWODIMARRAY(typename, one_dim_type) void FD_C_Destroy##typename (__fd_take FD_C_##typename * ptr) \ + { \ + for(int i=0; i< ptr->size; i++) { \ + FD_C_Destroy##one_dim_type(ptr->data + i); \ + } \ + delete[] ptr->data; \ + } + +#define DECLARE_AND_IMPLEMENT_FD_TYPE_THREEDIMARRAY(typename, two_dim_type) void FD_C_Destroy##typename (__fd_take FD_C_##typename * ptr) \ + { \ + for(int i=0; i< ptr->size; i++) { \ + FD_C_Destroy##two_dim_type(ptr->data + i); \ + } \ + delete[] ptr->data; \ + } + +// FD_C_OneDimArrayUint8 +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayUint8); +// FD_C_OneDimArrayInt8 +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayInt8); +// FD_C_OneDimArrayInt32 +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayInt32); +// FD_C_OneDimArraySize +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArraySize); +// FD_C_OneDimArrayInt64 +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayInt64); +// FD_C_OneDimArrayFloat +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayFloat); +// FD_C_Cstr +DECLARE_DESTROY_FD_TYPE_FUNCTION(Cstr); +// FD_C_OneDimArrayCstr +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayCstr); +// FD_C_TwoDimArraySize +DECLARE_DESTROY_FD_TYPE_FUNCTION(TwoDimArraySize); +// FD_C_TwoDimArrayInt8 +DECLARE_DESTROY_FD_TYPE_FUNCTION(TwoDimArrayInt8); +// FD_C_TwoDimArrayInt32 +DECLARE_DESTROY_FD_TYPE_FUNCTION(TwoDimArrayInt32); +// FD_C_ThreeDimArrayInt32 +DECLARE_DESTROY_FD_TYPE_FUNCTION(ThreeDimArrayInt32); +// FD_C_TwoDimArrayFloat +DECLARE_DESTROY_FD_TYPE_FUNCTION(TwoDimArrayFloat); +// FD_C_OneDimMat +DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimMat); + FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat FD_C_Imread(const char* imgpath); diff --git a/csharp/fastdeploy/types_internal_c.cs b/csharp/fastdeploy/types_internal_c.cs index a5d7645e8..a94f13e4b 100644 --- a/csharp/fastdeploy/types_internal_c.cs +++ b/csharp/fastdeploy/types_internal_c.cs @@ -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 { } } -} +} \ No newline at end of file diff --git a/csharp/fastdeploy/vision/ocr/model.cs b/csharp/fastdeploy/vision/ocr/model.cs new file mode 100644 index 000000000..e8a6804d0 --- /dev/null +++ b/csharp/fastdeploy/vision/ocr/model.cs @@ -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 BatchPredict(List 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 results_out = new List(); + + 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 BatchPredict(List imgs, int start_index, int end_index, List 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 results_out = new List(); + + 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 BatchPredict(List 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 results_out = new List(); + + 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 BatchPredict(List 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 results_out = new List(); + + 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(); + 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 BatchPredict(List 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 results_out = new List(); + 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(); + 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 BatchPredict(List 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 results_out = new List(); + 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 BatchPredict(List 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 results_out = new List(); + 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); + +} + +} + +} \ No newline at end of file diff --git a/csharp/fastdeploy/vision/result.cs b/csharp/fastdeploy/vision/result.cs index 815824072..8184ecb77 100644 --- a/csharp/fastdeploy/vision/result.cs +++ b/csharp/fastdeploy/vision/result.cs @@ -130,6 +130,101 @@ public class DetectionResult { } +public class OCRResult { + public List boxes; + public List text; + public List rec_scores; + public List cls_scores; + public List cls_labels; + public ResultType type; + + public OCRResult() { + this.boxes = new List(); + this.text = new List(); + this.rec_scores = new List(); + this.cls_scores = new List(); + this.cls_labels = new List(); + 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 boxes; +} + +public class OCRRecognizerResult{ + public string text; + public float rec_score; +} + public class SegmentationResult{ public List label_map; public List 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(); + 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(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(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(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(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(); diff --git a/csharp/fastdeploy/vision/visualize.cs b/csharp/fastdeploy/vision/visualize.cs index 5418a866b..9bd56e660 100644 --- a/csharp/fastdeploy/vision/visualize.cs +++ b/csharp/fastdeploy/vision/visualize.cs @@ -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); diff --git a/examples/vision/ocr/PP-OCRv2/csharp/CMakeLists.txt b/examples/vision/ocr/PP-OCRv2/csharp/CMakeLists.txt new file mode 100644 index 000000000..7ae8e2aba --- /dev/null +++ b/examples/vision/ocr/PP-OCRv2/csharp/CMakeLists.txt @@ -0,0 +1,22 @@ +PROJECT(infer_demo CSharp) +CMAKE_MINIMUM_REQUIRED (VERSION 3.10) + +# Set the C# language version (defaults to 3.0 if not set). +set(CMAKE_CSharp_FLAGS "/langversion:10") +set(CMAKE_DOTNET_TARGET_FRAMEWORK "net6.0") +set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk") + +# 指定下载解压后的fastdeploy库路径 +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeployCSharp.cmake) + + +add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cs) + +set_property(TARGET infer_demo PROPERTY VS_DOTNET_REFERENCES + ${FASTDEPLOY_DOTNET_REFERENCES} +) + +set_property(TARGET infer_demo + PROPERTY VS_PACKAGE_REFERENCES ${FASTDEPLOY_PACKAGE_REFERENCES}) diff --git a/examples/vision/ocr/PP-OCRv2/csharp/infer.cs b/examples/vision/ocr/PP-OCRv2/csharp/infer.cs new file mode 100644 index 000000000..cc54a5551 --- /dev/null +++ b/examples/vision/ocr/PP-OCRv2/csharp/infer.cs @@ -0,0 +1,79 @@ +// 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 OpenCvSharp; +using fastdeploy; + +namespace Test +{ + public class TestPPOCRv2 + { + public static void Main(string[] args) + { + if (args.Length < 6) { + Console.WriteLine( + "Usage: infer_demo path/to/det_model path/to/cls_model " + + "path/to/rec_model path/to/rec_label_file path/to/image " + + "run_option, " + + "e.g ./infer_demo ./ch_PP-OCRv2_det_infer " + + "./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv2_rec_infer " + + "./ppocr_keys_v1.txt ./12.jpg 0" + ); + Console.WriteLine( "The data type of run_option is int, 0: run with cpu; 1: run with gpu"); + return; + } + string det_model_dir = args[0]; + string cls_model_dir = args[1]; + string rec_model_dir = args[2]; + string rec_label_file = args[3]; + string image_path = args[4]; + RuntimeOption runtimeoption = new RuntimeOption(); + int device_option = Int32.Parse(args[5]); + if(device_option==0){ + runtimeoption.UseCpu(); + }else{ + runtimeoption.UseGpu(); + } + string sep = "\\"; + string det_model_file = det_model_dir + sep + "inference.pdmodel"; + string det_params_file = det_model_dir + sep + "inference.pdiparams"; + + string cls_model_file = cls_model_dir + sep + "inference.pdmodel"; + string cls_params_file = cls_model_dir + sep + "inference.pdiparams"; + + string rec_model_file = rec_model_dir + sep + "inference.pdmodel"; + string rec_params_file = rec_model_dir + sep + "inference.pdiparams"; + + fastdeploy.vision.ocr.DBDetector dbdetector = new fastdeploy.vision.ocr.DBDetector(det_model_file, det_params_file, runtimeoption, ModelFormat.PADDLE); + fastdeploy.vision.ocr.Classifier classifier = new fastdeploy.vision.ocr.Classifier(cls_model_file, cls_params_file, runtimeoption, ModelFormat.PADDLE); + fastdeploy.vision.ocr.Recognizer recognizer = new fastdeploy.vision.ocr.Recognizer(rec_model_file, rec_params_file, rec_label_file, runtimeoption, ModelFormat.PADDLE); + fastdeploy.pipeline.PPOCRv2 model = new fastdeploy.pipeline.PPOCRv2(dbdetector, classifier, recognizer); + if(!model.Initialized()){ + Console.WriteLine("Failed to initialize.\n"); + } + Mat image = Cv2.ImRead(image_path); + fastdeploy.vision.OCRResult res = model.Predict(image); + Console.WriteLine(res.ToString()); + Mat res_img = fastdeploy.vision.Visualize.VisOcr(image, res); + Cv2.ImShow("result.png", res_img); + Cv2.ImWrite("result.png", res_img); + Cv2.WaitKey(0); + + } + + } +} \ No newline at end of file diff --git a/examples/vision/ocr/PP-OCRv3/csharp/CMakeLists.txt b/examples/vision/ocr/PP-OCRv3/csharp/CMakeLists.txt new file mode 100644 index 000000000..7ae8e2aba --- /dev/null +++ b/examples/vision/ocr/PP-OCRv3/csharp/CMakeLists.txt @@ -0,0 +1,22 @@ +PROJECT(infer_demo CSharp) +CMAKE_MINIMUM_REQUIRED (VERSION 3.10) + +# Set the C# language version (defaults to 3.0 if not set). +set(CMAKE_CSharp_FLAGS "/langversion:10") +set(CMAKE_DOTNET_TARGET_FRAMEWORK "net6.0") +set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk") + +# 指定下载解压后的fastdeploy库路径 +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeployCSharp.cmake) + + +add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cs) + +set_property(TARGET infer_demo PROPERTY VS_DOTNET_REFERENCES + ${FASTDEPLOY_DOTNET_REFERENCES} +) + +set_property(TARGET infer_demo + PROPERTY VS_PACKAGE_REFERENCES ${FASTDEPLOY_PACKAGE_REFERENCES}) diff --git a/examples/vision/ocr/PP-OCRv3/csharp/infer.cs b/examples/vision/ocr/PP-OCRv3/csharp/infer.cs new file mode 100644 index 000000000..962500e08 --- /dev/null +++ b/examples/vision/ocr/PP-OCRv3/csharp/infer.cs @@ -0,0 +1,79 @@ +// 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 OpenCvSharp; +using fastdeploy; + +namespace Test +{ + public class TestPPOCRv3 + { + public static void Main(string[] args) + { + if (args.Length < 6) { + Console.WriteLine( + "Usage: infer_demo path/to/det_model path/to/cls_model " + + "path/to/rec_model path/to/rec_label_file path/to/image " + + "run_option, " + + "e.g ./infer_demo ./ch_PP-OCRv2_det_infer " + + "./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv2_rec_infer " + + "./ppocr_keys_v1.txt ./12.jpg 0" + ); + Console.WriteLine( "The data type of run_option is int, 0: run with cpu; 1: run with gpu"); + return; + } + string det_model_dir = args[0]; + string cls_model_dir = args[1]; + string rec_model_dir = args[2]; + string rec_label_file = args[3]; + string image_path = args[4]; + RuntimeOption runtimeoption = new RuntimeOption(); + int device_option = Int32.Parse(args[5]); + if(device_option==0){ + runtimeoption.UseCpu(); + }else{ + runtimeoption.UseGpu(); + } + string sep = "\\"; + string det_model_file = det_model_dir + sep + "inference.pdmodel"; + string det_params_file = det_model_dir + sep + "inference.pdiparams"; + + string cls_model_file = cls_model_dir + sep + "inference.pdmodel"; + string cls_params_file = cls_model_dir + sep + "inference.pdiparams"; + + string rec_model_file = rec_model_dir + sep + "inference.pdmodel"; + string rec_params_file = rec_model_dir + sep + "inference.pdiparams"; + + fastdeploy.vision.ocr.DBDetector dbdetector = new fastdeploy.vision.ocr.DBDetector(det_model_file, det_params_file, runtimeoption, ModelFormat.PADDLE); + fastdeploy.vision.ocr.Classifier classifier = new fastdeploy.vision.ocr.Classifier(cls_model_file, cls_params_file, runtimeoption, ModelFormat.PADDLE); + fastdeploy.vision.ocr.Recognizer recognizer = new fastdeploy.vision.ocr.Recognizer(rec_model_file, rec_params_file, rec_label_file, runtimeoption, ModelFormat.PADDLE); + fastdeploy.pipeline.PPOCRv3 model = new fastdeploy.pipeline.PPOCRv3(dbdetector, classifier, recognizer); + if(!model.Initialized()){ + Console.WriteLine("Failed to initialize.\n"); + } + Mat image = Cv2.ImRead(image_path); + fastdeploy.vision.OCRResult res = model.Predict(image); + Console.WriteLine(res.ToString()); + Mat res_img = fastdeploy.vision.Visualize.VisOcr(image, res); + Cv2.ImShow("result.png", res_img); + Cv2.ImWrite("result.png", res_img); + Cv2.WaitKey(0); + + } + + } +} \ No newline at end of file