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

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

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

Documentation copyright © 2025 TOKYO ELECTRON DEVICE LIMITED https://www.teldevice.co.jp/
Generated on Thu Jan 23 2025 16:11:31 for WIL-PDL Reference ( C++ ) 1.0.5 by Doxygen 1.9.5.