[C#] Supplement model and result api for c sharp (#1322)

* add c sharp api for fastdeploy

* update accroding to c apis

* add cmakelist for c sharp api

* add cmakelists for c sharp

* fix cmakelists

* fix cmakelists

* add c sharp api for fastdeploy

* add ppyoloe demo

* add ppyoloe demo

* modify demo namespace code

* add readme

* fix format

* format code

* fix doc

* add model api

* add batch_predict and string result for c sharp

* add ppdet models

* update api

* fix
This commit is contained in:
chenjian
2023-02-17 11:03:52 +08:00
committed by GitHub
parent db9739a76e
commit ea548ab3db
5 changed files with 2216 additions and 31 deletions

View File

@@ -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<ClassifyResult> BatchPredict(List<Mat> imgs){
FD_OneDimMat imgs_in = new FD_OneDimMat();
imgs_in.size = (nuint)imgs.Count;
// Copy data to unmanaged memory
IntPtr[] mat_ptrs = new IntPtr[imgs_in.size];
for(int i=0;i < (int)imgs.Count; i++){
mat_ptrs[i] = imgs[i].CvPtr;
}
int size = Marshal.SizeOf(new IntPtr()) * (int)imgs_in.size;
imgs_in.data = Marshal.AllocHGlobal(size);
Marshal.Copy(mat_ptrs, 0, imgs_in.data,
mat_ptrs.Length);
FD_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<ClassifyResult> results_out = new List<ClassifyResult>();
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);
}
}