Skip to content

Commit

Permalink
[C#] Add c# api for ppseg models (PaddlePaddle#1398)
Browse files Browse the repository at this point in the history
* add c# api for ppseg

* add example

* fix according to test

* update interface

* fix destroy funcs
  • Loading branch information
rainyfly authored Feb 27, 2023
1 parent 7e5e690 commit 48f776b
Show file tree
Hide file tree
Showing 9 changed files with 586 additions and 174 deletions.
4 changes: 0 additions & 4 deletions c_api/fastdeploy_capi/vision/result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ void FD_C_DestroyClassifyResult(
delete[] fd_c_classify_result->label_ids.data;
// delete scores
delete[] fd_c_classify_result->scores.data;
delete fd_c_classify_result;
}

void FD_C_ClassifyResultWrapperToCResult(
Expand Down Expand Up @@ -135,7 +134,6 @@ void FD_C_DestroyDetectionResult(
delete[] fd_c_detection_result->masks.data[i].data.data;
delete[] fd_c_detection_result->masks.data[i].shape.data;
}
delete fd_c_detection_result;
}

void FD_C_DetectionResultWrapperToCResult(
Expand Down Expand Up @@ -295,7 +293,6 @@ void FD_C_DestroyOCRResult(__fd_take FD_C_OCRResult* fd_c_ocr_result) {
delete[] fd_c_ocr_result->cls_scores.data;
// delete cls_labels
delete[] fd_c_ocr_result->cls_labels.data;
delete fd_c_ocr_result;
}

void FD_C_OCRResultWrapperToCResult(
Expand Down Expand Up @@ -430,7 +427,6 @@ void FD_C_DestroySegmentationResult(
delete[] fd_c_segmentation_result->score_map.data;
// delete shape
delete[] fd_c_segmentation_result->shape.data;
delete fd_c_segmentation_result;
}

void FD_C_SegmentationResultWrapperToCResult(
Expand Down
17 changes: 16 additions & 1 deletion csharp/fastdeploy/types_internal_c.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,28 @@ 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_SegmentationResult {
public FD_OneDimArrayUint8 label_map;
public FD_OneDimArrayFloat score_map;
public FD_OneDimArrayInt64 shape;
[MarshalAs(UnmanagedType.U1)]
public bool contain_score_map;
public FD_ResultType type;
}

[StructLayout(LayoutKind.Sequential)]
public struct FD_OneDimSegmentationResult {
public nuint size;
public IntPtr data; // FD_SegmentationResult[]
}

[StructLayout(LayoutKind.Sequential)]
public struct FD_OneDimMat {
public nuint size;
Expand Down
18 changes: 10 additions & 8 deletions csharp/fastdeploy/vision/classification/ppcls/model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public ClassifyResult Predict(Mat img) {
} // predict
ClassifyResult classify_result =
ConvertResult.ConvertCResultToClassifyResult(fd_classify_result);
FD_C_DestroyClassifyResult(ref fd_classify_result);
return classify_result;
}

Expand All @@ -71,7 +72,7 @@ public List<ClassifyResult> BatchPredict(List<Mat> imgs){
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)){
if (!FD_C_PaddleClasModelWrapperBatchPredict(fd_paddleclas_model_wrapper, imgs_in, ref fd_classify_result_array)){
return null;
}
List<ClassifyResult> results_out = new List<ClassifyResult>();
Expand All @@ -80,6 +81,7 @@ public List<ClassifyResult> BatchPredict(List<Mat> imgs){
fd_classify_result_array.data + i * Marshal.SizeOf(new FD_ClassifyResult()),
typeof(FD_ClassifyResult));
results_out.Add(ConvertResult.ConvertCResultToClassifyResult(fd_classify_result));
FD_C_DestroyClassifyResult(ref fd_classify_result);
}
return results_out;
}
Expand Down Expand Up @@ -113,15 +115,15 @@ private static extern void
FD_C_DestroyClassifyResultWrapper(IntPtr fd_classify_result_wrapper);
[DllImport("fastdeploy.dll", EntryPoint = "FD_C_DestroyClassifyResult")]
private static extern void
FD_C_DestroyClassifyResult(IntPtr fd_classify_result);
FD_C_DestroyClassifyResult(ref FD_ClassifyResult fd_classify_result);
[DllImport("fastdeploy.dll",
EntryPoint = "FD_C_ClassifyResultWrapperGetData")]
private static extern IntPtr
FD_C_ClassifyResultWrapperGetData(IntPtr fd_classify_result_wrapper);
EntryPoint = "FD_C_ClassifyResultWrapperToCResult")]
private static extern void
FD_C_ClassifyResultWrapperToCResult(IntPtr fd_classify_result_wrapper, ref FD_ClassifyResult fd_classify_result);
[DllImport("fastdeploy.dll",
EntryPoint = "FD_C_CreateClassifyResultWrapperFromData")]
EntryPoint = "FD_C_CreateClassifyResultWrapperFromCResult")]
private static extern IntPtr
FD_C_CreateClassifyResultWrapperFromData(IntPtr fd_classify_result);
FD_C_CreateClassifyResultWrapperFromCResult(ref FD_ClassifyResult fd_classify_result);

[DllImport("fastdeploy.dll",
EntryPoint = "FD_C_PaddleClasModelWrapperInitialized")]
Expand All @@ -131,7 +133,7 @@ private static extern bool
EntryPoint = "FD_C_PaddleClasModelWrapperBatchPredict")]
private static extern bool
FD_C_PaddleClasModelWrapperBatchPredict(IntPtr fd_paddleclas_model_wrapper,
ref FD_OneDimMat imgs,
FD_OneDimMat imgs,
ref FD_OneDimClassifyResult results);

}
Expand Down
Loading

0 comments on commit 48f776b

Please sign in to comment.