[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

@@ -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[]
}
}
}

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);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -37,7 +37,7 @@ public enum ResultType {
HEADPOSE
}
public struct Mask {
public class Mask {
public List<byte> data;
public List<long> shape;
public ResultType type;
@@ -46,9 +46,24 @@ public struct Mask {
this.shape = new List<long>();
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<int> label_ids;
public List<float> scores;
public ResultType type;
@@ -57,9 +72,24 @@ public struct ClassifyResult {
this.scores = new List<float>();
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<float[]> boxes;
public List<float> scores;
public List<int> 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;
}
}
}

View File

@@ -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);
}
}