WIL-PDL Reference ( C++ ) 1.0.6-gpu
サンプルコード ( 物体検出 )

物体検出のサンプルコードです

  • 最も単純なサンプルコード: L.5- smp_objdet_simple()
  • ベンチマークを計測するサンプルコード: L.78: smp_objdet_benchmark()
  • FIE の関数を前処理として行うサンプルコード: L.160: smp_objdet_with_fie()
1#pragma once
2#include "samples.h"
3#include "oal_aloc.h"
4
5int smp_objdet_simple(const char* model_name, const char* img_name)
6{
7 INT ret = F_ERR_NONE;
8 FHANDLE hsrc = NULL;
9 H_MODEL hmodel = NULL;
10 MODEL_CATEGORY model_category;
11 MODEL_BACKEND model_backend;
12 F_OD_RESULT* detections = NULL;
13 size_t num_detections;
14 INT ch, w, h;
15
16 // FIEライブラリの使用前に必ずコールする必要があります。
17 fnFIE_setup();
18
19 // 画像読込
20 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
21 printf_s("img: %d\r\n", ret);
22
23 //------- ここから -------//
24
25 // ライセンスチェック
26 ret = fnPDL_check_license();
27 printf_s("lic : %d\r\n", ret);
28
29 // ハンドルの生成
30 hmodel = fnPDL_create_handle();
31 if (NULL == hmodel) {
32 ret = F_ERR_NOMEMORY;
33 printf_s("failed to create handle\r\n");
34 goto finally;
35 }
36 else { printf_s("handle created\r\n"); }
37
38 // モデル読込
39 ret = fnPDL_load_model(model_name, hmodel);
40 printf_s("load: %d\r\n", ret);
41
42 // モデルの種別の確認
43 ret = fnPDL_get_model_category(hmodel, &model_category);
44 printf_s("par : %d category=%d\r\n", ret, model_category);
45 if (OBJECT_DETECTION != model_category) {
46 ret = F_ERR_INVALID_OBJECT;
47 printf_s("unmatch model\r\n");
48 goto finally;
49 }
50
51 // モデルの推論バックエンドの確認
52 ret = fnPDL_get_model_backend(hmodel, &model_backend);
53 printf_s("ret : %d backend=%d\r\n", ret, model_backend);
54
55 // モデルの期待する画像パラメータの確認
56 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
57 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
58
59 // 推論実行
60 ret = fnPDL_predict_object_detection(hmodel, hsrc, &detections, &num_detections);
61 printf_s("pred: %d\r\n", ret);
62 if (NULL != detections && 0 < num_detections) {
63 INT i_det;
64 printf_s(" # of detections = %zd\r\n", num_detections);
65 for (i_det = 0; i_det < num_detections; i_det++) {
66 F_OD_RESULT detection = detections[i_det];
67 DBOX_T box = detection.box;
68 printf_s(" label: %d, score %.2f, box: (st=(%.1f,%.1f), ed=(%.1f,%.1f))\r\n",
69 detection.class_id, detection.score, box.st.x, box.st.y, box.ed.x, box.ed.y);
70 }
71 }
72 printf_s("\r\n");
73
74finally:
75 // ハンドルの破棄
76 fnPDL_dispose(hmodel);
77
78 //------- ここまで -------//
79
80 // 終了処理
81 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
82 if (NULL != detections) { fnOAL_free(detections); }
83 fnFIE_teardown();
84
85 return ret;
86}
87
88int smp_objdet_benchmark(const char* model_name, const char* img_name, const int bench_iter)
89{
90 // benchmark 用
91 std::chrono::system_clock::time_point start, end;
92
93 INT ret = F_ERR_NONE;
94 FHANDLE hsrc = NULL;
95 H_MODEL hmodel = NULL;
96 MODEL_CATEGORY model_category;
97 F_OD_RESULT* detections = NULL;
98 size_t num_detections;
99
100 // FIEライブラリの使用前に必ずコールする必要があります。
101 fnFIE_setup();
102
103 // 画像読込
104 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
105 printf_s("img: %d\r\n", ret);
106 if (F_ERR_NONE != ret) { goto finally; }
107
108 // ライセンスチェック
109 ret = fnPDL_check_license();
110 printf_s("lic : %d\r\n", ret);
111
112 // ハンドルの生成
113 hmodel = fnPDL_create_handle();
114 if (NULL == hmodel) {
115 ret = F_ERR_NOMEMORY;
116 printf_s("failed to create handle\r\n");
117 goto finally;
118 }
119 else { printf_s("handle created\r\n"); }
120
121 // モデル読込
122 ret = fnPDL_load_model(model_name, hmodel);
123 printf_s("load: %d\r\n", ret);
124
125 // モデルの種別の確認
126 ret = fnPDL_get_model_category(hmodel, &model_category);
127 printf_s("par : %d category=%d\r\n", ret, model_category);
128 if (OBJECT_DETECTION != model_category) {
129 ret = F_ERR_INVALID_OBJECT;
130 printf_s("unmatch model\r\n");
131 goto finally;
132 }
133
134 printf_s("--- start ---\r\n");
135 printf_s("ret, elapsed[msec], num_detections\r\n");
136 // 本体ループ
137 for (int i = 0; i < bench_iter; i++) {
138 double elapsed;
139
140 // 開始時刻
141 start = std::chrono::system_clock::now();
142
143 // 推論実行
144 ret = fnPDL_predict_object_detection(hmodel, hsrc, &detections, &num_detections);
145
146 // 完了時刻
147 end = std::chrono::system_clock::now();
148
149 if (F_ERR_NONE != ret) {
150 printf_s("pred. err: %d\r\n", ret);
151 goto finally;
152 }
153
154 // 推論時間 [msec]
155 elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / (double)1000;
156
157 // コンソールに出力
158 printf_s("%04d, %.3e, %zd\r\n", i, elapsed, num_detections);
159
160 // 結果メモリを解放
161 fnOAL_free(detections);
162 detections = NULL;
163 }
164 printf_s("--- finish ---\r\n");
165 printf_s("\r\n");
166
167finally:
168 // ハンドルの破棄
169 fnPDL_dispose(hmodel);
170
171 // 終了処理
172 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
173 if (NULL != detections) { fnOAL_free(detections); }
174 fnFIE_teardown();
175
176 return ret;
177}
178
179int smp_objdet_with_fie(const char* model_name, const char* img_name)
180{
181 INT ret = F_ERR_NONE;
182 FHANDLE hsrc = NULL;
183 H_MODEL hmodel = NULL;
184 MODEL_CATEGORY model_category;
185 F_OD_RESULT* detections = NULL;
186 size_t num_detections;
187
188 FHANDLE hfiltered = NULL;
189 INT ch, w, h, type;
190 INT_PTR step;
191
192 // FIEライブラリの使用前に必ずコールする必要があります。
193 fnFIE_setup();
194
195 // 画像読込
196 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
197 printf_s("img: %d\r\n", ret);
198
199 //------- ここから -------//
200
201 // ライセンスチェック
202 ret = fnPDL_check_license();
203 printf_s("lic : %d\r\n", ret);
204
205 // ハンドルの生成
206 hmodel = fnPDL_create_handle();
207 if (NULL == hmodel) {
208 ret = F_ERR_NOMEMORY;
209 printf_s("failed to create handle\r\n");
210 goto finally;
211 }
212 else { printf_s("handle created\r\n"); }
213
214 // モデル読込
215 ret = fnPDL_load_model(model_name, hmodel);
216 printf_s("load: %d\r\n", ret);
217 if (F_ERR_NONE != ret) { goto finally; }
218
219 // モデルの種別の確認
220 ret = fnPDL_get_model_category(hmodel, &model_category);
221 printf_s("par : %d category=%d\r\n", ret, model_category);
222 if (OBJECT_DETECTION != model_category) {
223 ret = F_ERR_INVALID_OBJECT;
224 printf_s("unmatch model\r\n");
225 goto finally;
226 }
227
229 // 読み込んだ画像をそのまま推論する
231
232 // 推論実行
233 ret = fnPDL_predict_object_detection(hmodel, hsrc, &detections, &num_detections);
234 printf_s("pred(original): %d\r\n", ret);
235 if (F_ERR_NONE != ret) { goto finally; }
236 if (NULL != detections && 0 < num_detections) {
237 INT i_det;
238 printf_s(" # of detections = %zd\r\n", num_detections);
239 for (i_det = 0; i_det < num_detections; i_det++) {
240 F_OD_RESULT detection = detections[i_det];
241 DBOX_T box = detection.box;
242 printf_s(" label: %d, score %.2f, box: (st=(%.1f,%.1f), ed=(%.1f,%.1f))\r\n",
243 detection.class_id, detection.score, box.st.x, box.st.y, box.ed.x, box.ed.y);
244 }
245 }
246 fnOAL_free(detections);
247 detections = NULL;
248
250 // フィルタをかけた画像を推論する
252
253 // ここでは単純な平均化フィルタ
254 ret = fnFIE_img_get_params(hsrc, &ch, &type, &step, &w, &h);
255 if (F_ERR_NONE != ret) { goto finally; }
256 hfiltered = fnFIE_img_root_alloc(type, ch, w, h);
257 if (NULL == hfiltered) { ret = F_ERR_NOMEMORY; goto finally; }
258 ret = fnFIE_average(hsrc, hfiltered, 0, 0);
259 if (F_ERR_NONE != ret) { goto finally; }
260
261 // 推論実行
262 ret = fnPDL_predict_object_detection(hmodel, hfiltered, &detections, &num_detections);
263 printf_s("pred(average): %d\r\n", ret);
264 if (F_ERR_NONE != ret) { goto finally; }
265 if (NULL != detections && 0 < num_detections) {
266 INT i_det;
267 printf_s(" # of detections = %zd\r\n", num_detections);
268 for (i_det = 0; i_det < num_detections; i_det++) {
269 F_OD_RESULT detection = detections[i_det];
270 DBOX_T box = detection.box;
271 printf_s(" label: %d, score %.2f, box: (st=(%.1f,%.1f), ed=(%.1f,%.1f))\r\n",
272 detection.class_id, detection.score, box.st.x, box.st.y, box.ed.x, box.ed.y);
273 }
274 }
275 printf_s("\r\n");
276
277finally:
278 // ハンドルの破棄
279 fnPDL_dispose(hmodel);
280
281 //------- ここまで -------//
282
283 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
284 if (NULL != hfiltered) { fnFIE_free_object(hfiltered); }
285 if (NULL != detections) { fnOAL_free(detections); }
286
287 // 終了処理
288 fnFIE_teardown();
289
290 return ret;
291}
292
INT FVALGAPI fnPDL_get_input_image_size(const H_MODEL hmodel, INT *channels, INT *width, INT *height)
モデルパラメータの取得
Definition: prediction_cpp.cpp:1989
INT FVALGAPI fnPDL_get_model_backend(const H_MODEL hmodel, MODEL_BACKEND *model_backend)
モデルのバックエンドの取得
Definition: prediction_cpp.cpp:1968
MODEL_CATEGORY
モデルの種別
Definition: fv_pdl.h:46
INT fnPDL_check_license()
ライセンス確認
Definition: check_licence.cpp:160
MODEL_BACKEND
推論バックエンド
Definition: fv_pdl.h:30
INT FVALGAPI fnPDL_get_model_category(const H_MODEL hmodel, MODEL_CATEGORY *model_category)
モデルの種別の取得
Definition: prediction_cpp.cpp:1937
VOID FVALGAPI fnPDL_dispose(H_MODEL hmodel)
モデルハンドルの解放
Definition: prediction_cpp.cpp:2556
INT FVALGAPI fnPDL_predict_object_detection(const H_MODEL hmodel, const FHANDLE hsrc, F_OD_RESULT **detections, size_t *num_detections)
推論の実行 ( 物体検出 )
Definition: prediction_cpp.cpp:2394
INT FVALGAPI fnPDL_load_model(const CHAR *filename, H_MODEL hmodel)
モデルの読み込み
Definition: prediction_cpp.cpp:1810
VOID * H_MODEL
モデルハンドル
Definition: fv_pdl.h:18
H_MODEL *FVALGAPI fnPDL_create_handle()
モデルハンドルの生成
Definition: prediction_cpp.cpp:1755
@ OBJECT_DETECTION
Definition: fv_pdl.h:62
物体検出結果
Definition: fv_pdl.h:92
INT class_id
Definition: fv_pdl.h:98
DBOX_T box
Definition: fv_pdl.h:94
FLOAT score
Definition: fv_pdl.h:96

Documentation copyright © 2026 TOKYO ELECTRON DEVICE LIMITED https://www.teldevice.co.jp/
Generated on Fri Jun 19 2026 14:08:06 for WIL-PDL Reference ( C++ ) 1.0.6-gpu by Doxygen 1.9.5.