diff --git a/csharp/fastdeploy/types_internal_c.cs b/csharp/fastdeploy/types_internal_c.cs index d1274e28b..4402afc70 100644 --- a/csharp/fastdeploy/types_internal_c.cs +++ b/csharp/fastdeploy/types_internal_c.cs @@ -97,6 +97,13 @@ public struct FD_ClassifyResult { public FD_ResultType type; } +[StructLayout(LayoutKind.Sequential)] +public struct FD_OneDimClassifyResult { + public nuint size; + public IntPtr data; // FD_ClassifyResult[] +} + + [StructLayout(LayoutKind.Sequential)] public struct FD_Mask { public FD_OneDimArrayUint8 data; @@ -121,5 +128,18 @@ public struct FD_DetectionResult { public FD_ResultType type; } + +[StructLayout(LayoutKind.Sequential)] +public struct FD_OneDimDetectionResult { + public nuint size; + public IntPtr data; // FD_DetectionResult[] +} + +[StructLayout(LayoutKind.Sequential)] +public struct FD_OneDimMat { + public nuint size; + public IntPtr data; // Mat[] +} + } } diff --git a/csharp/fastdeploy/vision/classification/ppcls/model.cs b/csharp/fastdeploy/vision/classification/ppcls/model.cs index 4217c77a6..e130d8ecf 100644 --- a/csharp/fastdeploy/vision/classification/ppcls/model.cs +++ b/csharp/fastdeploy/vision/classification/ppcls/model.cs @@ -23,7 +23,7 @@ namespace fastdeploy { namespace vision { namespace classification { -class PaddleClasModel { +public class PaddleClasModel { public PaddleClasModel(string model_file, string params_file, string config_file, RuntimeOption custom_option = null, @@ -40,25 +40,54 @@ class PaddleClasModel { FD_C_DestroyPaddleClasModelWrapper(fd_paddleclas_model_wrapper); } + + public string ModelName() { + return "PaddleClas/Model"; + } + public ClassifyResult Predict(Mat img) { - IntPtr fd_classify_result_wrapper_ptr = FD_C_CreateClassifyResultWrapper(); - FD_C_PaddleClasModelWrapperPredict( + FD_ClassifyResult fd_classify_result = new FD_ClassifyResult(); + if(! FD_C_PaddleClasModelWrapperPredict( fd_paddleclas_model_wrapper, img.CvPtr, - fd_classify_result_wrapper_ptr); // predict - IntPtr fd_classify_result_ptr = FD_C_ClassifyResultWrapperGetData( - fd_classify_result_wrapper_ptr); // get result from wrapper - FD_ClassifyResult fd_classify_result = - (FD_ClassifyResult)Marshal.PtrToStructure(fd_classify_result_ptr, - typeof(FD_ClassifyResult)); + ref fd_classify_result)) + { + return null; + } // predict ClassifyResult classify_result = ConvertResult.ConvertCResultToClassifyResult(fd_classify_result); - FD_C_DestroyClassifyResultWrapper( - fd_classify_result_wrapper_ptr); // free fd_classify_result_wrapper_ptr - FD_C_DestroyClassifyResult( - fd_classify_result_ptr); // free fd_classify_result_ptr return 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_OneDimClassifyResult fd_classify_result_array = new FD_OneDimClassifyResult(); + if (!FD_C_PaddleClasModelWrapperBatchPredict(fd_paddleclas_model_wrapper, ref imgs_in, ref fd_classify_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_ClassifyResult fd_classify_result = (FD_ClassifyResult)Marshal.PtrToStructure( + fd_classify_result_array.data + i * Marshal.SizeOf(new FD_ClassifyResult()), + typeof(FD_ClassifyResult)); + results_out.Add(ConvertResult.ConvertCResultToClassifyResult(fd_classify_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PaddleClasModelWrapperInitialized(fd_paddleclas_model_wrapper); + } + // below are underlying C api private IntPtr fd_paddleclas_model_wrapper; [DllImport("fastdeploy.dll", @@ -75,7 +104,7 @@ class PaddleClasModel { private static extern bool FD_C_PaddleClasModelWrapperPredict(IntPtr fd_paddleclas_model_wrapper, IntPtr img, - IntPtr fd_classify_result_wrapper); + ref FD_ClassifyResult fd_classify_result); [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreateClassifyResultWrapper")] private static extern IntPtr FD_C_CreateClassifyResultWrapper(); [DllImport("fastdeploy.dll", @@ -93,6 +122,18 @@ class PaddleClasModel { EntryPoint = "FD_C_CreateClassifyResultWrapperFromData")] private static extern IntPtr FD_C_CreateClassifyResultWrapperFromData(IntPtr fd_classify_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleClasModelWrapperInitialized")] + private static extern bool + FD_C_PaddleClasModelWrapperInitialized(IntPtr fd_paddleclas_model_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleClasModelWrapperBatchPredict")] + private static extern bool + FD_C_PaddleClasModelWrapperBatchPredict(IntPtr fd_paddleclas_model_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimClassifyResult results); + } } diff --git a/csharp/fastdeploy/vision/detection/ppdet/model.cs b/csharp/fastdeploy/vision/detection/ppdet/model.cs index e675746ad..a6f5c0953 100644 --- a/csharp/fastdeploy/vision/detection/ppdet/model.cs +++ b/csharp/fastdeploy/vision/detection/ppdet/model.cs @@ -23,6 +23,7 @@ namespace fastdeploy { namespace vision { namespace detection { +// PPYOLOE public class PPYOLOE { public PPYOLOE(string model_file, string params_file, string config_file, @@ -39,24 +40,48 @@ public class PPYOLOE { ~PPYOLOE() { FD_C_DestroyPPYOLOEWrapper(fd_ppyoloe_wrapper); } public DetectionResult Predict(Mat img) { - IntPtr fd_detection_result_wrapper_ptr = - FD_C_CreateDetectionResultWrapper(); - FD_C_PPYOLOEWrapperPredict(fd_ppyoloe_wrapper, img.CvPtr, - fd_detection_result_wrapper_ptr); // predict - IntPtr fd_detection_result_ptr = FD_C_DetectionResultWrapperGetData( - fd_detection_result_wrapper_ptr); // get result from wrapper - FD_DetectionResult fd_detection_result = - (FD_DetectionResult)Marshal.PtrToStructure(fd_detection_result_ptr, - typeof(FD_DetectionResult)); + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PPYOLOEWrapperPredict(fd_ppyoloe_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + DetectionResult detection_result = ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); - FD_C_DestroyDetectionResultWrapper( - fd_detection_result_wrapper_ptr); // free fd_detection_result_wrapper_ptr - FD_C_DestroyDetectionResult( - fd_detection_result_ptr); // free fd_detection_result_ptr return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PPYOLOEWrapperBatchPredict(fd_ppyoloe_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PPYOLOEWrapperInitialized(fd_ppyoloe_wrapper); + } + // below are underlying C api private IntPtr fd_ppyoloe_wrapper; [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPPYOLOEWrapper")] @@ -69,7 +94,7 @@ public class PPYOLOE { [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PPYOLOEWrapperPredict")] private static extern bool FD_C_PPYOLOEWrapperPredict(IntPtr fd_ppyoloe_wrapper, IntPtr img, - IntPtr fd_detection_result_wrapper); + ref FD_DetectionResult fd_detection_result); [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreateDetectionResultWrapper")] private static extern IntPtr FD_C_CreateDetectionResultWrapper(); @@ -88,7 +113,1995 @@ public class PPYOLOE { EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] private static extern IntPtr FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PPYOLOEWrapperInitialized")] + private static extern bool + FD_C_PPYOLOEWrapperInitialized(IntPtr fd_c_ppyoloe_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PPYOLOEWrapperBatchPredict")] + private static extern bool + FD_C_PPYOLOEWrapperBatchPredict(IntPtr fd_c_ppyoloe_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); } + +// PicoDet +public class PicoDet { + + public PicoDet(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_picodet_wrapper = + FD_C_CreatesPicoDetWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PicoDet() { FD_C_DestroyPicoDetWrapper(fd_picodet_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PicoDetWrapperPredict(fd_picodet_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PicoDetWrapperBatchPredict(fd_picodet_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PicoDetWrapperInitialized(fd_picodet_wrapper); + } + + // below are underlying C api + private IntPtr fd_picodet_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPicoDetWrapper")] + private static extern IntPtr FD_C_CreatesPicoDetWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPicoDetWrapper")] + private static extern void + FD_C_DestroyPicoDetWrapper(IntPtr fd_picodet_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PicoDetWrapperPredict")] + private static extern bool + FD_C_PicoDetWrapperPredict(IntPtr fd_picodet_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PicoDetWrapperInitialized")] + private static extern bool + FD_C_PicoDetWrapperInitialized(IntPtr fd_c_picodet_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PicoDetWrapperBatchPredict")] + private static extern bool + FD_C_PicoDetWrapperBatchPredict(IntPtr fd_c_picodet_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + + +// PPYOLO + +public class PPYOLO { + + public PPYOLO(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_ppyolo_wrapper = + FD_C_CreatesPPYOLOWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PPYOLO() { FD_C_DestroyPPYOLOWrapper(fd_ppyolo_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PPYOLOWrapperPredict(fd_ppyolo_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PPYOLOWrapperBatchPredict(fd_ppyolo_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PPYOLOWrapperInitialized(fd_ppyolo_wrapper); + } + + // below are underlying C api + private IntPtr fd_ppyolo_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPPYOLOWrapper")] + private static extern IntPtr FD_C_CreatesPPYOLOWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPPYOLOWrapper")] + private static extern void + FD_C_DestroyPPYOLOWrapper(IntPtr fd_ppyolo_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PPYOLOWrapperPredict")] + private static extern bool + FD_C_PPYOLOWrapperPredict(IntPtr fd_ppyolo_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PPYOLOWrapperInitialized")] + private static extern bool + FD_C_PPYOLOWrapperInitialized(IntPtr fd_c_ppyolo_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PPYOLOWrapperBatchPredict")] + private static extern bool + FD_C_PPYOLOWrapperBatchPredict(IntPtr fd_c_ppyolo_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// YOLOv3 + +public class YOLOv3 { + + public YOLOv3(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_yolov3_wrapper = + FD_C_CreatesYOLOv3Wrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~YOLOv3() { FD_C_DestroyYOLOv3Wrapper(fd_yolov3_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_YOLOv3WrapperPredict(fd_yolov3_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_YOLOv3WrapperBatchPredict(fd_yolov3_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_YOLOv3WrapperInitialized(fd_yolov3_wrapper); + } + + // below are underlying C api + private IntPtr fd_yolov3_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesYOLOv3Wrapper")] + private static extern IntPtr FD_C_CreatesYOLOv3Wrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyYOLOv3Wrapper")] + private static extern void + FD_C_DestroyYOLOv3Wrapper(IntPtr fd_yolov3_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_YOLOv3WrapperPredict")] + private static extern bool + FD_C_YOLOv3WrapperPredict(IntPtr fd_yolov3_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_YOLOv3WrapperInitialized")] + private static extern bool + FD_C_YOLOv3WrapperInitialized(IntPtr fd_c_yolov3_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_YOLOv3WrapperBatchPredict")] + private static extern bool + FD_C_YOLOv3WrapperBatchPredict(IntPtr fd_c_yolov3_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// PaddleYOLOX + +public class PaddleYOLOX { + + public PaddleYOLOX(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_paddleyolox_wrapper = + FD_C_CreatesPaddleYOLOXWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PaddleYOLOX() { FD_C_DestroyPaddleYOLOXWrapper(fd_paddleyolox_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PaddleYOLOXWrapperPredict(fd_paddleyolox_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PaddleYOLOXWrapperBatchPredict(fd_paddleyolox_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PaddleYOLOXWrapperInitialized(fd_paddleyolox_wrapper); + } + + // below are underlying C api + private IntPtr fd_paddleyolox_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPaddleYOLOXWrapper")] + private static extern IntPtr FD_C_CreatesPaddleYOLOXWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPaddleYOLOXWrapper")] + private static extern void + FD_C_DestroyPaddleYOLOXWrapper(IntPtr fd_paddleyolox_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PaddleYOLOXWrapperPredict")] + private static extern bool + FD_C_PaddleYOLOXWrapperPredict(IntPtr fd_paddleyolox_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOXWrapperInitialized")] + private static extern bool + FD_C_PaddleYOLOXWrapperInitialized(IntPtr fd_c_paddleyolox_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOXWrapperBatchPredict")] + private static extern bool + FD_C_PaddleYOLOXWrapperBatchPredict(IntPtr fd_c_paddleyolox_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// FasterRCNN + +public class FasterRCNN { + + public FasterRCNN(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_fasterrcnn_wrapper = + FD_C_CreatesFasterRCNNWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~FasterRCNN() { FD_C_DestroyFasterRCNNWrapper(fd_fasterrcnn_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_FasterRCNNWrapperPredict(fd_fasterrcnn_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_FasterRCNNWrapperBatchPredict(fd_fasterrcnn_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_FasterRCNNWrapperInitialized(fd_fasterrcnn_wrapper); + } + + // below are underlying C api + private IntPtr fd_fasterrcnn_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesFasterRCNNWrapper")] + private static extern IntPtr FD_C_CreatesFasterRCNNWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyFasterRCNNWrapper")] + private static extern void + FD_C_DestroyFasterRCNNWrapper(IntPtr fd_fasterrcnn_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_FasterRCNNWrapperPredict")] + private static extern bool + FD_C_FasterRCNNWrapperPredict(IntPtr fd_fasterrcnn_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_FasterRCNNWrapperInitialized")] + private static extern bool + FD_C_FasterRCNNWrapperInitialized(IntPtr fd_c_fasterrcnn_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_FasterRCNNWrapperBatchPredict")] + private static extern bool + FD_C_FasterRCNNWrapperBatchPredict(IntPtr fd_c_fasterrcnn_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// MaskRCNN + +public class MaskRCNN { + + public MaskRCNN(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_maskrcnn_wrapper = + FD_C_CreatesMaskRCNNWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~MaskRCNN() { FD_C_DestroyMaskRCNNWrapper(fd_maskrcnn_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_MaskRCNNWrapperPredict(fd_maskrcnn_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_MaskRCNNWrapperBatchPredict(fd_maskrcnn_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_MaskRCNNWrapperInitialized(fd_maskrcnn_wrapper); + } + + // below are underlying C api + private IntPtr fd_maskrcnn_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesMaskRCNNWrapper")] + private static extern IntPtr FD_C_CreatesMaskRCNNWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyMaskRCNNWrapper")] + private static extern void + FD_C_DestroyMaskRCNNWrapper(IntPtr fd_maskrcnn_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_MaskRCNNWrapperPredict")] + private static extern bool + FD_C_MaskRCNNWrapperPredict(IntPtr fd_maskrcnn_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_MaskRCNNWrapperInitialized")] + private static extern bool + FD_C_MaskRCNNWrapperInitialized(IntPtr fd_c_maskrcnn_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_MaskRCNNWrapperBatchPredict")] + private static extern bool + FD_C_MaskRCNNWrapperBatchPredict(IntPtr fd_c_maskrcnn_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// SSD + +public class SSD { + + public SSD(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_ssd_wrapper = + FD_C_CreatesSSDWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~SSD() { FD_C_DestroySSDWrapper(fd_ssd_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_SSDWrapperPredict(fd_ssd_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_SSDWrapperBatchPredict(fd_ssd_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_SSDWrapperInitialized(fd_ssd_wrapper); + } + + // below are underlying C api + private IntPtr fd_ssd_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesSSDWrapper")] + private static extern IntPtr FD_C_CreatesSSDWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroySSDWrapper")] + private static extern void + FD_C_DestroySSDWrapper(IntPtr fd_ssd_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_SSDWrapperPredict")] + private static extern bool + FD_C_SSDWrapperPredict(IntPtr fd_ssd_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_SSDWrapperInitialized")] + private static extern bool + FD_C_SSDWrapperInitialized(IntPtr fd_c_ssd_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_SSDWrapperBatchPredict")] + private static extern bool + FD_C_SSDWrapperBatchPredict(IntPtr fd_c_ssd_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// PaddleYOLOv5 + +public class PaddleYOLOv5 { + + public PaddleYOLOv5(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_paddleyolov5_wrapper = + FD_C_CreatesPaddleYOLOv5Wrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PaddleYOLOv5() { FD_C_DestroyPaddleYOLOv5Wrapper(fd_paddleyolov5_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PaddleYOLOv5WrapperPredict(fd_paddleyolov5_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PaddleYOLOv5WrapperBatchPredict(fd_paddleyolov5_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PaddleYOLOv5WrapperInitialized(fd_paddleyolov5_wrapper); + } + + // below are underlying C api + private IntPtr fd_paddleyolov5_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPaddleYOLOv5Wrapper")] + private static extern IntPtr FD_C_CreatesPaddleYOLOv5Wrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPaddleYOLOv5Wrapper")] + private static extern void + FD_C_DestroyPaddleYOLOv5Wrapper(IntPtr fd_paddleyolov5_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PaddleYOLOv5WrapperPredict")] + private static extern bool + FD_C_PaddleYOLOv5WrapperPredict(IntPtr fd_paddleyolov5_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv5WrapperInitialized")] + private static extern bool + FD_C_PaddleYOLOv5WrapperInitialized(IntPtr fd_c_paddleyolov5_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv5WrapperBatchPredict")] + private static extern bool + FD_C_PaddleYOLOv5WrapperBatchPredict(IntPtr fd_c_paddleyolov5_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// PaddleYOLOv6 + +public class PaddleYOLOv6 { + + public PaddleYOLOv6(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_paddleyolov6_wrapper = + FD_C_CreatesPaddleYOLOv6Wrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PaddleYOLOv6() { FD_C_DestroyPaddleYOLOv6Wrapper(fd_paddleyolov6_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PaddleYOLOv6WrapperPredict(fd_paddleyolov6_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PaddleYOLOv6WrapperBatchPredict(fd_paddleyolov6_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PaddleYOLOv6WrapperInitialized(fd_paddleyolov6_wrapper); + } + + // below are underlying C api + private IntPtr fd_paddleyolov6_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPaddleYOLOv6Wrapper")] + private static extern IntPtr FD_C_CreatesPaddleYOLOv6Wrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPaddleYOLOv6Wrapper")] + private static extern void + FD_C_DestroyPaddleYOLOv6Wrapper(IntPtr fd_paddleyolov6_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PaddleYOLOv6WrapperPredict")] + private static extern bool + FD_C_PaddleYOLOv6WrapperPredict(IntPtr fd_paddleyolov6_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv6WrapperInitialized")] + private static extern bool + FD_C_PaddleYOLOv6WrapperInitialized(IntPtr fd_c_paddleyolov6_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv6WrapperBatchPredict")] + private static extern bool + FD_C_PaddleYOLOv6WrapperBatchPredict(IntPtr fd_c_paddleyolov6_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// PaddleYOLOv7 + +public class PaddleYOLOv7 { + + public PaddleYOLOv7(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_paddleyolov7_wrapper = + FD_C_CreatesPaddleYOLOv7Wrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PaddleYOLOv7() { FD_C_DestroyPaddleYOLOv7Wrapper(fd_paddleyolov7_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PaddleYOLOv7WrapperPredict(fd_paddleyolov7_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PaddleYOLOv7WrapperBatchPredict(fd_paddleyolov7_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PaddleYOLOv7WrapperInitialized(fd_paddleyolov7_wrapper); + } + + // below are underlying C api + private IntPtr fd_paddleyolov7_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPaddleYOLOv7Wrapper")] + private static extern IntPtr FD_C_CreatesPaddleYOLOv7Wrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPaddleYOLOv7Wrapper")] + private static extern void + FD_C_DestroyPaddleYOLOv7Wrapper(IntPtr fd_paddleyolov7_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PaddleYOLOv7WrapperPredict")] + private static extern bool + FD_C_PaddleYOLOv7WrapperPredict(IntPtr fd_paddleyolov7_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv7WrapperInitialized")] + private static extern bool + FD_C_PaddleYOLOv7WrapperInitialized(IntPtr fd_c_paddleyolov7_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv7WrapperBatchPredict")] + private static extern bool + FD_C_PaddleYOLOv7WrapperBatchPredict(IntPtr fd_c_paddleyolov7_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// PaddleYOLOv8 + +public class PaddleYOLOv8 { + + public PaddleYOLOv8(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_paddleyolov8_wrapper = + FD_C_CreatesPaddleYOLOv8Wrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PaddleYOLOv8() { FD_C_DestroyPaddleYOLOv8Wrapper(fd_paddleyolov8_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PaddleYOLOv8WrapperPredict(fd_paddleyolov8_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PaddleYOLOv8WrapperBatchPredict(fd_paddleyolov8_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PaddleYOLOv8WrapperInitialized(fd_paddleyolov8_wrapper); + } + + // below are underlying C api + private IntPtr fd_paddleyolov8_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPaddleYOLOv8Wrapper")] + private static extern IntPtr FD_C_CreatesPaddleYOLOv8Wrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPaddleYOLOv8Wrapper")] + private static extern void + FD_C_DestroyPaddleYOLOv8Wrapper(IntPtr fd_paddleyolov8_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PaddleYOLOv8WrapperPredict")] + private static extern bool + FD_C_PaddleYOLOv8WrapperPredict(IntPtr fd_paddleyolov8_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv8WrapperInitialized")] + private static extern bool + FD_C_PaddleYOLOv8WrapperInitialized(IntPtr fd_c_paddleyolov8_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PaddleYOLOv8WrapperBatchPredict")] + private static extern bool + FD_C_PaddleYOLOv8WrapperBatchPredict(IntPtr fd_c_paddleyolov8_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// RTMDet + +public class RTMDet { + + public RTMDet(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_rtmdet_wrapper = + FD_C_CreatesRTMDetWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~RTMDet() { FD_C_DestroyRTMDetWrapper(fd_rtmdet_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_RTMDetWrapperPredict(fd_rtmdet_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_RTMDetWrapperBatchPredict(fd_rtmdet_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_RTMDetWrapperInitialized(fd_rtmdet_wrapper); + } + + // below are underlying C api + private IntPtr fd_rtmdet_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesRTMDetWrapper")] + private static extern IntPtr FD_C_CreatesRTMDetWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyRTMDetWrapper")] + private static extern void + FD_C_DestroyRTMDetWrapper(IntPtr fd_rtmdet_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_RTMDetWrapperPredict")] + private static extern bool + FD_C_RTMDetWrapperPredict(IntPtr fd_rtmdet_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_RTMDetWrapperInitialized")] + private static extern bool + FD_C_RTMDetWrapperInitialized(IntPtr fd_c_rtmdet_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_RTMDetWrapperBatchPredict")] + private static extern bool + FD_C_RTMDetWrapperBatchPredict(IntPtr fd_c_rtmdet_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// CascadeRCNN + +public class CascadeRCNN { + + public CascadeRCNN(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_cascadercnn_wrapper = + FD_C_CreatesCascadeRCNNWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~CascadeRCNN() { FD_C_DestroyCascadeRCNNWrapper(fd_cascadercnn_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_CascadeRCNNWrapperPredict(fd_cascadercnn_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_CascadeRCNNWrapperBatchPredict(fd_cascadercnn_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_CascadeRCNNWrapperInitialized(fd_cascadercnn_wrapper); + } + + // below are underlying C api + private IntPtr fd_cascadercnn_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesCascadeRCNNWrapper")] + private static extern IntPtr FD_C_CreatesCascadeRCNNWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyCascadeRCNNWrapper")] + private static extern void + FD_C_DestroyCascadeRCNNWrapper(IntPtr fd_cascadercnn_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CascadeRCNNWrapperPredict")] + private static extern bool + FD_C_CascadeRCNNWrapperPredict(IntPtr fd_cascadercnn_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CascadeRCNNWrapperInitialized")] + private static extern bool + FD_C_CascadeRCNNWrapperInitialized(IntPtr fd_c_cascadercnn_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CascadeRCNNWrapperBatchPredict")] + private static extern bool + FD_C_CascadeRCNNWrapperBatchPredict(IntPtr fd_c_cascadercnn_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// PSSDet + +public class PSSDet { + + public PSSDet(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_pssdet_wrapper = + FD_C_CreatesPSSDetWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~PSSDet() { FD_C_DestroyPSSDetWrapper(fd_pssdet_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_PSSDetWrapperPredict(fd_pssdet_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_PSSDetWrapperBatchPredict(fd_pssdet_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_PSSDetWrapperInitialized(fd_pssdet_wrapper); + } + + // below are underlying C api + private IntPtr fd_pssdet_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesPSSDetWrapper")] + private static extern IntPtr FD_C_CreatesPSSDetWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyPSSDetWrapper")] + private static extern void + FD_C_DestroyPSSDetWrapper(IntPtr fd_pssdet_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_PSSDetWrapperPredict")] + private static extern bool + FD_C_PSSDetWrapperPredict(IntPtr fd_pssdet_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PSSDetWrapperInitialized")] + private static extern bool + FD_C_PSSDetWrapperInitialized(IntPtr fd_c_pssdet_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_PSSDetWrapperBatchPredict")] + private static extern bool + FD_C_PSSDetWrapperBatchPredict(IntPtr fd_c_pssdet_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// RetinaNet + +public class RetinaNet { + + public RetinaNet(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_retinanet_wrapper = + FD_C_CreatesRetinaNetWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~RetinaNet() { FD_C_DestroyRetinaNetWrapper(fd_retinanet_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_RetinaNetWrapperPredict(fd_retinanet_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_RetinaNetWrapperBatchPredict(fd_retinanet_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_RetinaNetWrapperInitialized(fd_retinanet_wrapper); + } + + // below are underlying C api + private IntPtr fd_retinanet_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesRetinaNetWrapper")] + private static extern IntPtr FD_C_CreatesRetinaNetWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyRetinaNetWrapper")] + private static extern void + FD_C_DestroyRetinaNetWrapper(IntPtr fd_retinanet_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_RetinaNetWrapperPredict")] + private static extern bool + FD_C_RetinaNetWrapperPredict(IntPtr fd_retinanet_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_RetinaNetWrapperInitialized")] + private static extern bool + FD_C_RetinaNetWrapperInitialized(IntPtr fd_c_retinanet_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_RetinaNetWrapperBatchPredict")] + private static extern bool + FD_C_RetinaNetWrapperBatchPredict(IntPtr fd_c_retinanet_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// FCOS + +public class FCOS { + + public FCOS(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_fcos_wrapper = + FD_C_CreatesFCOSWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~FCOS() { FD_C_DestroyFCOSWrapper(fd_fcos_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_FCOSWrapperPredict(fd_fcos_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_FCOSWrapperBatchPredict(fd_fcos_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_FCOSWrapperInitialized(fd_fcos_wrapper); + } + + // below are underlying C api + private IntPtr fd_fcos_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesFCOSWrapper")] + private static extern IntPtr FD_C_CreatesFCOSWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyFCOSWrapper")] + private static extern void + FD_C_DestroyFCOSWrapper(IntPtr fd_fcos_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_FCOSWrapperPredict")] + private static extern bool + FD_C_FCOSWrapperPredict(IntPtr fd_fcos_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_FCOSWrapperInitialized")] + private static extern bool + FD_C_FCOSWrapperInitialized(IntPtr fd_c_fcos_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_FCOSWrapperBatchPredict")] + private static extern bool + FD_C_FCOSWrapperBatchPredict(IntPtr fd_c_fcos_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// TTFNet + +public class TTFNet { + + public TTFNet(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_ttfnet_wrapper = + FD_C_CreatesTTFNetWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~TTFNet() { FD_C_DestroyTTFNetWrapper(fd_ttfnet_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_TTFNetWrapperPredict(fd_ttfnet_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_TTFNetWrapperBatchPredict(fd_ttfnet_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_TTFNetWrapperInitialized(fd_ttfnet_wrapper); + } + + // below are underlying C api + private IntPtr fd_ttfnet_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesTTFNetWrapper")] + private static extern IntPtr FD_C_CreatesTTFNetWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyTTFNetWrapper")] + private static extern void + FD_C_DestroyTTFNetWrapper(IntPtr fd_ttfnet_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_TTFNetWrapperPredict")] + private static extern bool + FD_C_TTFNetWrapperPredict(IntPtr fd_ttfnet_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_TTFNetWrapperInitialized")] + private static extern bool + FD_C_TTFNetWrapperInitialized(IntPtr fd_c_ttfnet_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_TTFNetWrapperBatchPredict")] + private static extern bool + FD_C_TTFNetWrapperBatchPredict(IntPtr fd_c_ttfnet_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// TOOD + +public class TOOD { + + public TOOD(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_tood_wrapper = + FD_C_CreatesTOODWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~TOOD() { FD_C_DestroyTOODWrapper(fd_tood_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_TOODWrapperPredict(fd_tood_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_TOODWrapperBatchPredict(fd_tood_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_TOODWrapperInitialized(fd_tood_wrapper); + } + + // below are underlying C api + private IntPtr fd_tood_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesTOODWrapper")] + private static extern IntPtr FD_C_CreatesTOODWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyTOODWrapper")] + private static extern void + FD_C_DestroyTOODWrapper(IntPtr fd_tood_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_TOODWrapperPredict")] + private static extern bool + FD_C_TOODWrapperPredict(IntPtr fd_tood_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_TOODWrapperInitialized")] + private static extern bool + FD_C_TOODWrapperInitialized(IntPtr fd_c_tood_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_TOODWrapperBatchPredict")] + private static extern bool + FD_C_TOODWrapperBatchPredict(IntPtr fd_c_tood_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + +// GFL + +public class GFL { + + public GFL(string model_file, string params_file, string config_file, + RuntimeOption custom_option = null, + ModelFormat model_format = ModelFormat.PADDLE) { + if (custom_option == null) { + custom_option = new RuntimeOption(); + } + fd_gfl_wrapper = + FD_C_CreatesGFLWrapper(model_file, params_file, config_file, + custom_option.GetWrapperPtr(), model_format); + } + + ~GFL() { FD_C_DestroyGFLWrapper(fd_gfl_wrapper); } + + public DetectionResult Predict(Mat img) { + FD_DetectionResult fd_detection_result = new FD_DetectionResult(); + if(! FD_C_GFLWrapperPredict(fd_gfl_wrapper, img.CvPtr, + ref fd_detection_result)) + { + return null; + } // predict + + DetectionResult detection_result = + ConvertResult.ConvertCResultToDetectionResult(fd_detection_result); + return detection_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_OneDimDetectionResult fd_detection_result_array = new FD_OneDimDetectionResult(); + if(!FD_C_GFLWrapperBatchPredict(fd_gfl_wrapper, ref imgs_in, ref fd_detection_result_array)){ + return null; + } + List results_out = new List(); + for(int i=0;i < (int)imgs.Count; i++){ + FD_DetectionResult fd_detection_result = (FD_DetectionResult)Marshal.PtrToStructure( + fd_detection_result_array.data + i * Marshal.SizeOf(new FD_DetectionResult()), + typeof(FD_DetectionResult)); + results_out.Add(ConvertResult.ConvertCResultToDetectionResult(fd_detection_result)); + } + return results_out; + } + + public bool Initialized() { + return FD_C_GFLWrapperInitialized(fd_gfl_wrapper); + } + + // below are underlying C api + private IntPtr fd_gfl_wrapper; + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_CreatesGFLWrapper")] + private static extern IntPtr FD_C_CreatesGFLWrapper( + string model_file, string params_file, string config_file, + IntPtr fd_runtime_option_wrapper, ModelFormat model_format); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyGFLWrapper")] + private static extern void + FD_C_DestroyGFLWrapper(IntPtr fd_gfl_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_GFLWrapperPredict")] + private static extern bool + FD_C_GFLWrapperPredict(IntPtr fd_gfl_wrapper, IntPtr img, + ref FD_DetectionResult fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapper")] + private static extern IntPtr FD_C_CreateDetectionResultWrapper(); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DestroyDetectionResultWrapper")] + private static extern void + FD_C_DestroyDetectionResultWrapper(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyDetectionResult")] + private static extern void + FD_C_DestroyDetectionResult(IntPtr fd_detection_result); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_DetectionResultWrapperGetData")] + private static extern IntPtr + FD_C_DetectionResultWrapperGetData(IntPtr fd_detection_result_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_CreateDetectionResultWrapperFromData")] + private static extern IntPtr + FD_C_CreateDetectionResultWrapperFromData(IntPtr fd_detection_result); + + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_GFLWrapperInitialized")] + private static extern bool + FD_C_GFLWrapperInitialized(IntPtr fd_c_gfl_wrapper); + [DllImport("fastdeploy.dll", + EntryPoint = "FD_C_GFLWrapperBatchPredict")] + private static extern bool + FD_C_GFLWrapperBatchPredict(IntPtr fd_c_gfl_wrapper, + ref FD_OneDimMat imgs, + ref FD_OneDimDetectionResult results); +} + } } } \ No newline at end of file diff --git a/csharp/fastdeploy/vision/result.cs b/csharp/fastdeploy/vision/result.cs index 5b137bc86..e302674a2 100644 --- a/csharp/fastdeploy/vision/result.cs +++ b/csharp/fastdeploy/vision/result.cs @@ -37,7 +37,7 @@ public enum ResultType { HEADPOSE } -public struct Mask { +public class Mask { public List data; public List shape; public ResultType type; @@ -46,9 +46,24 @@ public struct Mask { this.shape = new List(); this.type = ResultType.MASK; } + + public override string ToString() { + string information = "Mask(" ; + int ndim = this.shape.Count; + for (int i = 0; i < ndim; i++) { + if (i < ndim - 1) { + information += this.shape[i].ToString() + ","; + } else { + information += this.shape[i].ToString(); + } + } + information += ")\n"; + return information; + } + } -public struct ClassifyResult { +public class ClassifyResult { public List label_ids; public List scores; public ResultType type; @@ -57,9 +72,24 @@ public struct ClassifyResult { this.scores = new List(); this.type = ResultType.CLASSIFY; } + + public string ToString() { + string information; + information = "ClassifyResult(\nlabel_ids: "; + for (int i = 0; i < label_ids.Count; i++) { + information = information + label_ids[i].ToString() + ", "; + } + information += "\nscores: "; + for (int i = 0; i < scores.Count; i++) { + information = information + scores[i].ToString() + ", "; + } + information += "\n)"; + return information; + + } } -public struct DetectionResult { +public class DetectionResult { public List boxes; public List scores; public List label_ids; @@ -74,6 +104,30 @@ public struct DetectionResult { this.contain_masks = false; this.type = ResultType.DETECTION; } + + + public string ToString() { + string information; + if (!contain_masks) { + information = "DetectionResult: [xmin, ymin, xmax, ymax, score, label_id]\n"; + } else { + information = + "DetectionResult: [xmin, ymin, xmax, ymax, score, label_id, mask_shape]\n"; + } + for (int i = 0; i < boxes.Count; i++) { + information = information + boxes[i][0].ToString() + "," + + boxes[i][1].ToString() + ", " + boxes[i][2].ToString() + + ", " + boxes[i][3].ToString() + ", " + + scores[i].ToString() + ", " + label_ids[i].ToString(); + if (!contain_masks) { + information += "\n"; + } else { + information += ", " + masks[i].ToString(); + } + } + return information; + } + } public class ConvertResult { @@ -265,6 +319,39 @@ public class ConvertResult { detection_result.type = (ResultType)fd_detection_result.type; return detection_result; } + + + public static FD_OneDimArrayCstr + ConvertStringArrayToCOneDimArrayCstr(string[] strs){ + FD_OneDimArrayCstr fd_one_dim_cstr = new FD_OneDimArrayCstr(); + fd_one_dim_cstr.size = (nuint)strs.Length; + + // Copy data to unmanaged memory + FD_Cstr[] c_strs = new FD_Cstr[strs.Length]; + int size = Marshal.SizeOf(c_strs[0]) * c_strs.Length; + fd_one_dim_cstr.data = Marshal.AllocHGlobal(size); + for (int i = 0; i < strs.Length; i++) { + c_strs[i].size = (nuint)strs[i].Length; + c_strs[i].data = strs[i]; + Marshal.StructureToPtr( + c_strs[i], + fd_one_dim_cstr.data + i * Marshal.SizeOf(c_strs[0]), true); + } + return fd_one_dim_cstr; + } + + public static string[] + ConvertCOneDimArrayCstrToStringArray(FD_OneDimArrayCstr c_strs){ + string[] strs = new string[c_strs.size]; + for(int i=0; i<(int)c_strs.size; i++){ + FD_Cstr cstr = (FD_Cstr)Marshal.PtrToStructure( + c_strs.data + i * Marshal.SizeOf(new FD_Cstr()), + typeof(FD_Cstr)); + strs[i] = cstr.data; + } + return strs; + } + } } diff --git a/csharp/fastdeploy/vision/visualize.cs b/csharp/fastdeploy/vision/visualize.cs index 6ed5f168a..a2c83b3af 100644 --- a/csharp/fastdeploy/vision/visualize.cs +++ b/csharp/fastdeploy/vision/visualize.cs @@ -35,10 +35,34 @@ public class Visualize { return new Mat(result_ptr); } + + public static Mat VisDetection(Mat im, DetectionResult detection_result, + string[] labels, + float score_threshold = 0.0f, + int line_size = 1, float font_size = 0.5f) { + FD_DetectionResult fd_detection_result = + ConvertResult.ConvertDetectionResultToCResult(detection_result); + FD_OneDimArrayCstr labels_in = ConvertResult.ConvertStringArrayToCOneDimArrayCstr(labels); + IntPtr result_ptr = + FD_C_VisDetectionWithLabel(im.CvPtr, ref fd_detection_result, + ref labels_in, score_threshold, + line_size, font_size); + return new Mat(result_ptr); + } + + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_VisDetection")] private static extern IntPtr FD_C_VisDetection(IntPtr im, ref FD_DetectionResult fd_detection_result, float score_threshold, int line_size, float font_size); + + + [DllImport("fastdeploy.dll", EntryPoint = "FD_C_VisDetectionWithLabel")] + private static extern IntPtr + FD_C_VisDetectionWithLabel(IntPtr im, ref FD_DetectionResult fd_detection_result, + ref FD_OneDimArrayCstr labels, + float score_threshold, int line_size, float font_size); + } }