|
| 1 | +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +// #cgo CFLAGS: -I./fastdeploy_capi |
| 18 | +// #cgo LDFLAGS: -L./fastdeploy-linux-x64-0.0.0/lib -lfastdeploy |
| 19 | +// #include <fastdeploy_capi/vision.h> |
| 20 | +// #include <stdio.h> |
| 21 | +// #include <stdbool.h> |
| 22 | +// #include <stdlib.h> |
| 23 | +/* |
| 24 | +#include <stdio.h> |
| 25 | +#ifdef WIN32 |
| 26 | +const char sep = '\\'; |
| 27 | +#else |
| 28 | +const char sep = '/'; |
| 29 | +#endif |
| 30 | +
|
| 31 | +char* GetModelFilePath(char* model_dir, char* model_file, int max_size){ |
| 32 | + snprintf(model_file, max_size, "%s%c%s", model_dir, sep, "model.pdmodel"); |
| 33 | + return model_file; |
| 34 | +} |
| 35 | +
|
| 36 | +char* GetParametersFilePath(char* model_dir, char* params_file, int max_size){ |
| 37 | + snprintf(params_file, max_size, "%s%c%s", model_dir, sep, "model.pdiparams"); |
| 38 | + return params_file; |
| 39 | +} |
| 40 | +
|
| 41 | +char* GetConfigFilePath(char* model_dir, char* config_file, int max_size){ |
| 42 | + snprintf(config_file, max_size, "%s%c%s", model_dir, sep, "infer_cfg.yml"); |
| 43 | + return config_file; |
| 44 | +} |
| 45 | +*/ |
| 46 | +import "C" |
| 47 | +import ( |
| 48 | +"flag" |
| 49 | +"fmt" |
| 50 | +"unsafe" |
| 51 | +) |
| 52 | + |
| 53 | +func FDBooleanToGo(b C.FD_C_Bool) bool { |
| 54 | +var cFalse C.FD_C_Bool |
| 55 | +if b != cFalse { |
| 56 | +return true |
| 57 | +} |
| 58 | +return false |
| 59 | +} |
| 60 | + |
| 61 | +func CpuInfer(modelDir *C.char, imageFile *C.char) { |
| 62 | + |
| 63 | +var modelFile = (*C.char)(C.malloc(C.size_t(100))) |
| 64 | +var paramsFile = (*C.char)(C.malloc(C.size_t(100))) |
| 65 | +var configFile = (*C.char)(C.malloc(C.size_t(100))) |
| 66 | +var maxSize = 99 |
| 67 | + |
| 68 | +modelFile = C.GetModelFilePath(modelDir, modelFile, C.int(maxSize)) |
| 69 | +paramsFile = C.GetParametersFilePath(modelDir, paramsFile, C.int(maxSize)) |
| 70 | +configFile = C.GetConfigFilePath(modelDir, configFile, C.int(maxSize)) |
| 71 | + |
| 72 | +var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() |
| 73 | +C.FD_C_RuntimeOptionWrapperUseCpu(option) |
| 74 | + |
| 75 | +var model *C.FD_C_PPYOLOEWrapper = C.FD_C_CreatePPYOLOEWrapper( |
| 76 | +modelFile, paramsFile, configFile, option, C.FD_C_ModelFormat_PADDLE) |
| 77 | + |
| 78 | +if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperInitialized(model)) { |
| 79 | +fmt.Printf("Failed to initialize.\n") |
| 80 | +C.FD_C_DestroyRuntimeOptionWrapper(option) |
| 81 | +C.FD_C_DestroyPPYOLOEWrapper(model) |
| 82 | +return |
| 83 | +} |
| 84 | + |
| 85 | +var image C.FD_C_Mat = C.FD_C_Imread(imageFile) |
| 86 | + |
| 87 | +var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() |
| 88 | + |
| 89 | +if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperPredict(model, image, result)) { |
| 90 | +fmt.Printf("Failed to predict.\n") |
| 91 | +C.FD_C_DestroyRuntimeOptionWrapper(option) |
| 92 | +C.FD_C_DestroyPPYOLOEWrapper(model) |
| 93 | +C.FD_C_DestroyMat(image) |
| 94 | +C.free(unsafe.Pointer(result)) |
| 95 | +return |
| 96 | +} |
| 97 | + |
| 98 | +var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) |
| 99 | + |
| 100 | +C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) |
| 101 | +fmt.Printf("Visualized result saved in ./vis_result.jpg\n") |
| 102 | + |
| 103 | +C.FD_C_DestroyRuntimeOptionWrapper(option) |
| 104 | +C.FD_C_DestroyPPYOLOEWrapper(model) |
| 105 | +C.FD_C_DestroyDetectionResult(result) |
| 106 | +C.FD_C_DestroyMat(image) |
| 107 | +C.FD_C_DestroyMat(visImage) |
| 108 | +} |
| 109 | + |
| 110 | +func GpuInfer(modelDir *C.char, imageFile *C.char) { |
| 111 | + |
| 112 | +var modelFile = (*C.char)(C.malloc(C.size_t(100))) |
| 113 | +var paramsFile = (*C.char)(C.malloc(C.size_t(100))) |
| 114 | +var configFile = (*C.char)(C.malloc(C.size_t(100))) |
| 115 | +var maxSize = 99 |
| 116 | + |
| 117 | +modelFile = C.GetModelFilePath(modelDir, modelFile, C.int(maxSize)) |
| 118 | +paramsFile = C.GetParametersFilePath(modelDir, paramsFile, C.int(maxSize)) |
| 119 | +configFile = C.GetConfigFilePath(modelDir, configFile, C.int(maxSize)) |
| 120 | + |
| 121 | +var option *C.FD_C_RuntimeOptionWrapper = C.FD_C_CreateRuntimeOptionWrapper() |
| 122 | +C.FD_C_RuntimeOptionWrapperUseGpu(option, 0) |
| 123 | + |
| 124 | +var model *C.FD_C_PPYOLOEWrapper = C.FD_C_CreatePPYOLOEWrapper( |
| 125 | +modelFile, paramsFile, configFile, option, C.FD_C_ModelFormat_PADDLE) |
| 126 | + |
| 127 | +if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperInitialized(model)) { |
| 128 | +fmt.Printf("Failed to initialize.\n") |
| 129 | +C.FD_C_DestroyRuntimeOptionWrapper(option) |
| 130 | +C.FD_C_DestroyPPYOLOEWrapper(model) |
| 131 | +return |
| 132 | +} |
| 133 | + |
| 134 | +var image C.FD_C_Mat = C.FD_C_Imread(imageFile) |
| 135 | + |
| 136 | +var result *C.FD_C_DetectionResult = C.FD_C_CreateDetectionResult() |
| 137 | + |
| 138 | +if !FDBooleanToGo(C.FD_C_PPYOLOEWrapperPredict(model, image, result)) { |
| 139 | +fmt.Printf("Failed to predict.\n") |
| 140 | +C.FD_C_DestroyRuntimeOptionWrapper(option) |
| 141 | +C.FD_C_DestroyPPYOLOEWrapper(model) |
| 142 | +C.FD_C_DestroyMat(image) |
| 143 | +C.free(unsafe.Pointer(result)) |
| 144 | +return |
| 145 | +} |
| 146 | + |
| 147 | +var visImage C.FD_C_Mat = C.FD_C_VisDetection(image, result, 0.5, 1, 0.5) |
| 148 | + |
| 149 | +C.FD_C_Imwrite(C.CString("vis_result.jpg"), visImage) |
| 150 | +fmt.Printf("Visualized result saved in ./vis_result.jpg\n") |
| 151 | + |
| 152 | +C.FD_C_DestroyRuntimeOptionWrapper(option) |
| 153 | +C.FD_C_DestroyPPYOLOEWrapper(model) |
| 154 | +C.FD_C_DestroyDetectionResult(result) |
| 155 | +C.FD_C_DestroyMat(image) |
| 156 | +C.FD_C_DestroyMat(visImage) |
| 157 | +} |
| 158 | + |
| 159 | +var ( |
| 160 | +modelDir string |
| 161 | +imageFile string |
| 162 | +deviceType int |
| 163 | +) |
| 164 | + |
| 165 | +func init() { |
| 166 | +flag.StringVar(&modelDir, "model", "", "paddle detection model to use") |
| 167 | +flag.StringVar(&imageFile, "image", "", "image to predict") |
| 168 | +flag.IntVar(&deviceType, "device", 0, "The data type of run_option is int, 0: run with cpu; 1: run with gpu") |
| 169 | +} |
| 170 | + |
| 171 | +func main() { |
| 172 | +flag.Parse() |
| 173 | + |
| 174 | +if modelDir != "" && imageFile != "" { |
| 175 | +if deviceType == 0 { |
| 176 | +CpuInfer(C.CString(modelDir), C.CString(imageFile)) |
| 177 | +} else if deviceType == 1 { |
| 178 | +GpuInfer(C.CString(modelDir), C.CString(imageFile)) |
| 179 | +} |
| 180 | +} else { |
| 181 | +fmt.Printf("Usage: ./infer -model path/to/model_dir -image path/to/image -device run_option \n") |
| 182 | +fmt.Printf("e.g ./infer -model ./ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device 0 \n") |
| 183 | +} |
| 184 | + |
| 185 | +} |
0 commit comments