FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
onnx_runtime_cpp/examples/YoloXApp.cpp at develop · cardboardcode/onnx_runtime_cpp · GitHub
cardboardcode
/
onnx_runtime_cpp
Public
forked from
xmba15/onnx_runtime_cpp
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
onnx_runtime_cpp
/
examples
/
YoloXApp.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
110 lines (85 loc) · 3.73 KB
Breadcrumbs
onnx_runtime_cpp
/
examples
/
YoloXApp.cpp
Copy path
File metadata and controls
110 lines (85 loc) · 3.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
*
* @file YoloXApp.cpp
*
* @author btran
*
*/
#
include
<
ort_utility/ort_utility.hpp
>
#
include
"
Utility.hpp
"
#
include
"
YoloX.hpp
"
static
const
std::vector<std::string>
MSCOCO_WITHOUT_BG_CLASSES
(Ort::
MSCOCO_CLASSES
.begin() + 1,
Ort::MSCOCO_CLASSES.end());
static
constexpr
int64_t
NUM_CLASSES
=
80
;
static
const
std::vector<std::array<
int
,
3
>>
COLOR_CHART
= Ort::generateColorCharts(
NUM_CLASSES
);
static
constexpr
float
CONFIDENCE_THRESHOLD
=
0.1
;
static
const
std::vector<cv::Scalar>
COLORS
= toCvScalarColors(
COLOR_CHART
);
namespace
{
cv::Mat
processOneFrame
(
const
Ort::YoloX& osh,
const
cv::Mat& inputImg,
float
* dst,
const
float
confThresh);
}
//
namespace
int
main
(
int
argc,
char
* argv[])
{
if
(argc !=
3
) {
std::cerr <<
"
Usage: [apps] [path/to/onnx/yolox] [path/to/image]
"
<< std::endl;
return
EXIT_FAILURE
;
}
const
std::string
ONNX_MODEL_PATH
= argv[
1
];
const
std::string
IMAGE_PATH
= argv[
2
];
cv::Mat img =
cv::imread
(
IMAGE_PATH
);
if
(img.
empty
()) {
std::cerr <<
"
Failed to read input image
"
<< std::endl;
return
EXIT_FAILURE
;
}
Ort::YoloX
osh
(
NUM_CLASSES
,
ONNX_MODEL_PATH
,
0
,
std::vector<std::vector<
int64_t
>>{{
1
, Ort::YoloX::
IMG_CHANNEL
, Ort::YoloX::
IMG_H
, Ort::YoloX::
IMG_W
}});
osh.
initClassNames
(
MSCOCO_WITHOUT_BG_CLASSES
);
std::vector<
float
>
dst
(Ort::YoloX::
IMG_CHANNEL
* Ort::YoloX::
IMG_H
* Ort::YoloX::
IMG_W
);
auto
result =
processOneFrame
(osh, img, dst.
data
(),
CONFIDENCE_THRESHOLD
);
cv::imwrite
(
"
result.jpg
"
, result);
std::cout <<
"
Written to [result.jpg]
"
<< std::endl;
return
EXIT_SUCCESS
;
}
namespace
{
cv::Mat
processOneFrame
(
const
Ort::YoloX& osh,
const
cv::Mat& inputImg,
float
* dst,
const
float
confThresh)
{
int
origW = inputImg.
cols
, origH = inputImg.
rows
;
std::vector<
float
> originImageSize{
static_cast
<
float
>(origH),
static_cast
<
float
>(origW)};
cv::Mat scaledImg;
cv::resize
(inputImg, scaledImg,
cv::Size
(Ort::YoloX::
IMG_W
, Ort::YoloX::
IMG_H
),
0
,
0
, cv::
INTER_CUBIC
);
osh.
preprocess
(dst, scaledImg.
data
, Ort::YoloX::
IMG_W
, Ort::YoloX::
IMG_H
,
3
);
auto
inferenceOutput =
osh
({dst});
std::vector<Ort::YoloX::Object> objects = osh.
decodeOutputs
(inferenceOutput[
0
].
first
, confThresh);
std::vector<std::array<
float
,
4
>> bboxes;
std::vector<
float
> scores;
std::vector<
uint64_t
> classIndices;
float
scaleW =
1
. * origW / Ort::YoloX::
IMG_W
;
float
scaleH =
1
. * origH / Ort::YoloX::
IMG_H
;
for
(
const
auto
& object : objects) {
float
xmin = object.
pos
.
x
* scaleW;
float
ymin = object.
pos
.
y
* scaleH;
float
xmax = (object.
pos
.
x
+ object.
pos
.
width
) * scaleW;
float
ymax = (object.
pos
.
y
+ object.
pos
.
height
) * scaleH;
xmin = std::max<
float
>(xmin,
0
);
ymin = std::max<
float
>(ymin,
0
);
xmax = std::min<
float
>(xmax, inputImg.
cols
-
1
);
ymax = std::min<
float
>(ymax, inputImg.
rows
-
1
);
bboxes.
emplace_back
(std::array<
float
,
4
>{xmin, ymin, xmax, ymax});
scores.
emplace_back
(object.
prob
);
classIndices.
emplace_back
(object.
label
);
}
auto
afterNmsIndices =
Ort::nms
(bboxes, scores, confThresh);
std::vector<std::array<
float
,
4
>> afterNmsBboxes;
std::vector<
uint64_t
> afterNmsClassIndices;
afterNmsBboxes.
reserve
(afterNmsIndices.
size
());
afterNmsClassIndices.
reserve
(afterNmsIndices.
size
());
for
(
const
auto
idx : afterNmsIndices) {
afterNmsBboxes.
emplace_back
(bboxes[idx]);
afterNmsClassIndices.
emplace_back
(classIndices[idx]);
}
return
afterNmsBboxes.
empty
()
? inputImg
:
visualizeOneImage
(inputImg, afterNmsBboxes, afterNmsClassIndices,
COLORS
, osh.
classNames
());
}
}
//
namespace
Back
|
FazBrowse Home
|
New Git URL