Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (149 loc) · 5.49 KB
/
Copy pathmain.cpp
File metadata and controls
199 lines (149 loc) · 5.49 KB
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
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <onnxruntime_cxx_api.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
const string modelPath = "models/modnet_webcam.onnx";
const int cameraIndex = argc > 1 ? stoi(argv[1]) : 0;
constexpr int inputWidth = 512;
constexpr int inputHeight = 512;
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "MODNet");
Ort::SessionOptions sessionOptions;
sessionOptions.SetIntraOpNumThreads(4);
sessionOptions.SetGraphOptimizationLevel(
GraphOptimizationLevel::ORT_ENABLE_ALL
);
Ort::Session session(env, modelPath.c_str(), sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
auto inputNameAllocated = session.GetInputNameAllocated(0, allocator);
auto outputNameAllocated = session.GetOutputNameAllocated(0, allocator);
string inputName = inputNameAllocated.get();
string outputName = outputNameAllocated.get();
cout << "Model input: " << inputName << endl;
cout << "Model output: " << outputName << endl;
vector<int64_t> inputShape = { // Expected MODNet input dimensions: NCHW.
1, 3, inputHeight, inputWidth
};
vector<float> inputTensorValues(
3 * inputHeight * inputWidth
);
VideoCapture camera(cameraIndex, CAP_ANY);
if (!camera.isOpened()) {
cerr << "Could not open camera " << cameraIndex << endl;
return 1;
}
camera.set(CAP_PROP_FRAME_WIDTH, 640);
camera.set(CAP_PROP_FRAME_HEIGHT, 480);
auto memoryInfo = Ort::MemoryInfo::CreateCpu(
OrtArenaAllocator, OrtMemTypeDefault
);
const char* inputNames[] = { inputName.c_str() };
const char* outputNames[] = { outputName.c_str() };
Mat frame;
while (camera.read(frame)) {
if (frame.empty())
continue;
Mat resized, rgb, floatImage;
resize(frame, resized, Size(inputWidth, inputHeight));
cvtColor(resized, rgb, COLOR_BGR2RGB);
rgb.convertTo(floatImage, CV_32FC3);
vector<Mat> channels;
split(floatImage, channels);
for (int c = 0; c < 3; ++c) {
float* destination =
inputTensorValues.data() +
c * inputWidth * inputHeight;
for (int y = 0; y < inputHeight; ++y) {
const float* source = channels[c].ptr<float>(y);
for (int x = 0; x < inputWidth; ++x) {
destination[y * inputWidth + x] =
(source[x] - 127.5f) / 127.5f;
}
}
}
auto inputTensor = Ort::Value::CreateTensor<float>(
memoryInfo,
inputTensorValues.data(),
inputTensorValues.size(),
inputShape.data(),
inputShape.size()
);
auto outputs = session.Run(
Ort::RunOptions{nullptr},
inputNames,
&inputTensor,
1,
outputNames,
1
);
if (!outputs[0].IsTensor()) {
cerr << "Model output is not a tensor." << endl;
break;
}
auto outputInfo = outputs[0].GetTensorTypeAndShapeInfo();
auto outputShape = outputInfo.GetShape();
if (outputShape.size() != 4 ||
outputShape[0] != 1 ||
outputShape[1] != 1) {
cerr << "Unexpected output shape: ";
for (auto d : outputShape)
cerr << d << " ";
cerr << endl;
break;
}
const int outH = static_cast<int>(outputShape[2]);
const int outW = static_cast<int>(outputShape[3]);
const float* outputData =
outputs[0].GetTensorData<float>();
Mat alphaSmall(outH, outW, CV_32F,
const_cast<float*>(outputData));
Mat alpha;
resize(alphaSmall, alpha, frame.size());
cv::max(alpha, 0.0, alpha);
cv::min(alpha, 1.0, alpha);
Mat mask;
alpha.convertTo(mask, CV_8U, 255.0);
Mat background(frame.size(), frame.type(),
Scalar(0, 180, 0));
Mat alpha3;
vector<Mat> alphaChannels(3, alpha);
merge(alphaChannels, alpha3);
Mat foregroundFloat, backgroundFloat;
frame.convertTo(foregroundFloat, CV_32FC3);
background.convertTo(backgroundFloat, CV_32FC3);
Mat inverseAlpha = Scalar::all(1.0) - alpha3;
Mat resultFloat =
foregroundFloat.mul(alpha3) +
backgroundFloat.mul(inverseAlpha);
Mat result;
resultFloat.convertTo(result, CV_8UC3);
Mat maskBgr;
cvtColor(mask, maskBgr, COLOR_GRAY2BGR);
Mat display;
hconcat(vector<Mat>{frame, result, maskBgr}, display);
putText(display, "Original", Point(10, 25),
FONT_HERSHEY_SIMPLEX, 0.7,
Scalar(0, 255, 255), 2);
putText(display, "Green background", Point(frame.cols + 10, 25),
FONT_HERSHEY_SIMPLEX, 0.7,
Scalar(255, 255, 255), 2);
putText(display, "Alpha mask", Point(2 * frame.cols + 10, 25),
FONT_HERSHEY_SIMPLEX, 0.7,
Scalar(0, 255, 255), 2);
imshow("MODNet - ONNX Runtime", display);
int key = waitKey(1);
if (key == 'q' || key == 'Q' || key == 27)
break;
}
camera.release();
destroyAllWindows();
return 0;
}