-
Notifications
You must be signed in to change notification settings - Fork 3
/
yolox.cpp
404 lines (349 loc) · 12.3 KB
/
yolox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*******************************************************************
*
* DESCRIPTION:
* AILIA yolox sample
* AUTHOR:
*
* DATE:2022/07/20
*
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <string>
#include <opencv2/opencv.hpp>
#undef UNICODE
#include "ailia.h"
#include "ailia_detector.h"
#include "utils.h"
#include "detector_utils.h"
#include "webcamera_utils.h"
// ======================
// Parameters
// ======================
#define WEIGHT_PATH "yolox_s.opt.onnx"
#define MODEL_PATH "yolox_s.opt.onnx.prototxt"
#define IMAGE_PATH "input.jpg"
#define SAVE_IMAGE_PATH "output.png"
#define MODEL_INPUT_WIDTH 640
#define MODEL_INPUT_HEIGHT 640
#define IMAGE_WIDTH 640 // for video mode
#define IMAGE_HEIGHT 640 // for video mode
#define THRESHOLD 0.4f
#define IOU 0.45f
#if defined(_WIN32) || defined(_WIN64)
#define PRINT_OUT(...) fprintf_s(stdout, __VA_ARGS__)
#define PRINT_ERR(...) fprintf_s(stderr, __VA_ARGS__)
#else
#define PRINT_OUT(...) fprintf(stdout, __VA_ARGS__)
#define PRINT_ERR(...) fprintf(stderr, __VA_ARGS__)
#endif
#define BENCHMARK_ITERS 5
static std::string weight(WEIGHT_PATH);
static std::string model(MODEL_PATH);
static std::string image_path(IMAGE_PATH);
static std::string video_path("0");
static std::string save_image_path(SAVE_IMAGE_PATH);
static const std::vector<const char*> COCO_CATEGORY = {
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train",
"truck", "boat", "traffic light", "fire hydrant", "stop sign",
"parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard",
"sports ball", "kite", "baseball bat", "baseball glove", "skateboard",
"surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork",
"knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange",
"broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair",
"couch", "potted plant", "bed", "dining table", "toilet", "tv",
"laptop", "mouse", "remote", "keyboard", "cell phone", "microwave",
"oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
"scissors", "teddy bear", "hair drier", "toothbrush"
};
static bool benchmark = false;
static bool video_mode = false;
static int args_env_id = -1;
// ======================
// Arguemnt Parser
// ======================
static void print_usage()
{
PRINT_OUT("usage: yolox [-h] [-i IMAGE] [-v VIDEO] [-s SAVE_IMAGE_PATH] [-b] [-e ENV_ID]\n");
return;
}
static void print_help()
{
PRINT_OUT("\n");
PRINT_OUT("yolox model\n");
PRINT_OUT("\n");
PRINT_OUT("optional arguments:\n");
PRINT_OUT(" -h, --help show this help message and exit\n");
PRINT_OUT(" -i IMAGE, --input IMAGE\n");
PRINT_OUT(" The input image path.\n");
PRINT_OUT(" -v VIDEO, --video VIDEO\n");
PRINT_OUT(" The input video path. If the VIDEO argument is set to\n");
PRINT_OUT(" 0, the webcam input will be used.\n");
PRINT_OUT(" -s SAVE_IMAGE_PATH, --savepath SAVE_IMAGE_PATH\n");
PRINT_OUT(" Save path for the output image.\n");
PRINT_OUT(" -b, --benchmark Running the inference on the same input 5 times to\n");
PRINT_OUT(" measure execution performance. (Cannot be used in\n");
PRINT_OUT(" video mode)\n");
PRINT_OUT(" -e ENV_ID, --env_id ENV_ID\n");
PRINT_OUT(" The backend environment id.\n");
return;
}
static void print_error(std::string arg)
{
PRINT_ERR("yolov3-tiny: error: unrecognized arguments: %s\n", arg.c_str());
return;
}
static int argument_parser(int argc, char **argv)
{
int status = 0;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (status == 0) {
if (arg == "-i" || arg == "--input") {
status = 1;
}
else if (arg == "-v" || arg == "--video") {
video_mode = true;
status = 2;
}
else if (arg == "-s" || arg == "--savepath") {
status = 3;
}
else if (arg == "-b" || arg == "--benchmark") {
benchmark = true;
}
else if (arg == "-h" || arg == "--help") {
print_usage();
print_help();
return -1;
}
else if (arg == "-e" || arg == "--env_id") {
status = 4;
}
else {
print_usage();
print_error(arg);
return -1;
}
}
else if (arg[0] != '-') {
switch (status) {
case 1:
image_path = arg;
break;
case 2:
video_path = arg;
break;
case 3:
save_image_path = arg;
break;
case 4:
args_env_id = atoi(arg.c_str());
break;
default:
print_usage();
print_error(arg);
return -1;
}
status = 0;
}
else {
print_usage();
print_error(arg);
return -1;
}
}
return AILIA_STATUS_SUCCESS;
}
// ======================
// Main functions
// ======================
static int recognize_from_image(AILIADetector* detector)
{
// prepare input data
cv::Mat img;
int status = load_image(img, image_path.c_str());
if (status != AILIA_STATUS_SUCCESS) {
return -1;
}
PRINT_OUT("input image shape: (%d, %d, %d)\n",
img.cols, img.rows, img.channels());
// inference
PRINT_OUT("Start inference...\n");
if (benchmark) {
PRINT_OUT("BENCHMARK mode\n");
for (int i = 0; i < BENCHMARK_ITERS; i++) {
clock_t start = clock();
status = ailiaDetectorCompute(detector, img.data,
img.cols*4, img.cols, img.rows,
AILIA_IMAGE_FORMAT_BGRA, THRESHOLD, IOU);
clock_t end = clock();
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaDetectorCompute failed %d\n", status);
return -1;
}
PRINT_OUT("\tailia processing time %ld ms\n", ((end-start)*1000)/CLOCKS_PER_SEC);
}
}
else {
status = ailiaDetectorCompute(detector, img.data,
img.cols*4, img.cols, img.rows,
AILIA_IMAGE_FORMAT_BGRA, THRESHOLD, IOU);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaDetectorCompute failed %d\n", status);
return -1;
}
}
status = plot_result(detector, img, COCO_CATEGORY);
if (status != AILIA_STATUS_SUCCESS) {
return -1;
}
cv::imwrite(save_image_path.c_str(), img);
PRINT_OUT("Program finished successfully.\n");
return AILIA_STATUS_SUCCESS;
}
static int recognize_from_video(AILIADetector* detector)
{
// inference
cv::VideoCapture capture;
if (video_path == "0") {
PRINT_OUT("[INFO] webcamera mode is activated\n");
capture = cv::VideoCapture(0);
if (!capture.isOpened()) {
PRINT_ERR("[ERROR] webcamera not found\n");
return -1;
}
}
else {
if (check_file_existance(video_path.c_str())) {
capture = cv::VideoCapture(video_path.c_str());
}
else {
PRINT_ERR("[ERROR] \"%s\" not found\n", video_path.c_str());
return -1;
}
}
while (1) {
cv::Mat frame;
capture >> frame;
if ((char)cv::waitKey(1) == 'q' || frame.empty()) {
break;
}
cv::Mat resized_img, img;
adjust_frame_size(frame, resized_img, IMAGE_WIDTH, IMAGE_HEIGHT);
cv::cvtColor(resized_img, img, cv::COLOR_BGR2BGRA);
int status = ailiaDetectorCompute(detector, img.data,
MODEL_INPUT_WIDTH*4, MODEL_INPUT_WIDTH, MODEL_INPUT_HEIGHT,
AILIA_IMAGE_FORMAT_BGRA, THRESHOLD, IOU);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaDetectorCompute failed %d\n", status);
return -1;
}
status = plot_result(detector, resized_img, COCO_CATEGORY, false);
if (status != AILIA_STATUS_SUCCESS) {
return -1;
}
cv::imshow("frame", resized_img);
}
capture.release();
cv::destroyAllWindows();
PRINT_OUT("Program finished successfully.\n");
return AILIA_STATUS_SUCCESS;
}
int main(int argc, char **argv)
{
int status = argument_parser(argc, argv);
if (status != AILIA_STATUS_SUCCESS) {
return -1;
}
// env list
unsigned int env_count;
status = ailiaGetEnvironmentCount(&env_count);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaGetEnvironmentCount Failed %d", status);
return -1;
}
int env_id = AILIA_ENVIRONMENT_ID_AUTO;
for (unsigned int i = 0; i < env_count; i++) {
AILIAEnvironment* env;
status = ailiaGetEnvironment(&env, i, AILIA_ENVIRONMENT_VERSION);
PRINT_OUT("env_id : %d type : %d name : %s\n", env->id, env->type, env->name);
if (args_env_id == env->id){
env_id = env->id;
}
if (args_env_id == -1 && env_id == AILIA_ENVIRONMENT_ID_AUTO){
if (env->type == AILIA_ENVIRONMENT_TYPE_GPU) {
env_id = env->id;
}
}
}
if (args_env_id == -1){
PRINT_OUT("you can select environment using -e option\n");
}
// net initialize
AILIANetwork *ailia;
status = ailiaCreate(&ailia, env_id, AILIA_MULTITHREAD_AUTO);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaCreate failed %d\n", status);
if (status == AILIA_STATUS_LICENSE_NOT_FOUND || status==AILIA_STATUS_LICENSE_EXPIRED){
PRINT_OUT("License file not found or expired : please place license file (AILIA.lic)\n");
}
return -1;
}
AILIAEnvironment *env_ptr = nullptr;
status = ailiaGetSelectedEnvironment(ailia, &env_ptr, AILIA_ENVIRONMENT_VERSION);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaGetSelectedEnvironment failed %d\n", status);
ailiaDestroy(ailia);
return -1;
}
// PRINT_OUT("env_id: %d\n", env_ptr->id);
PRINT_OUT("selected env name : %s\n", env_ptr->name);
status = ailiaOpenStreamFile(ailia, model.c_str());
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaOpenStreamFile failed %d\n", status);
PRINT_ERR("ailiaGetErrorDetail %s\n", ailiaGetErrorDetail(ailia));
ailiaDestroy(ailia);
return -1;
}
status = ailiaOpenWeightFile(ailia, weight.c_str());
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaOpenWeightFile failed %d\n", status);
ailiaDestroy(ailia);
return -1;
}
const unsigned int flags = AILIA_DETECTOR_FLAG_NORMAL;
AILIADetector *detector;
status = ailiaCreateDetector(&detector, ailia,
AILIA_NETWORK_IMAGE_FORMAT_BGR,
AILIA_NETWORK_IMAGE_CHANNEL_FIRST,
AILIA_NETWORK_IMAGE_RANGE_UNSIGNED_INT8,
AILIA_DETECTOR_ALGORITHM_YOLOX,
COCO_CATEGORY.size(), flags);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaCreateDetector failed %d\n", status);
ailiaDestroy(ailia);
return -1;
}
status = ailiaDetectorSetInputShape(detector, MODEL_INPUT_WIDTH, MODEL_INPUT_HEIGHT);
if (status != AILIA_STATUS_SUCCESS) {
PRINT_ERR("ailiaDetectorSetInputShape(w=%u, h=%u) failed %d\n",
MODEL_INPUT_WIDTH, MODEL_INPUT_HEIGHT, status);
ailiaDestroyDetector(detector);
ailiaDestroy(ailia);
return -1;
}
if (video_mode) {
status = recognize_from_video(detector);
}
else {
status = recognize_from_image(detector);
}
ailiaDestroyDetector(detector);
ailiaDestroy(ailia);
return status;
}