-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnnxInstance.cpp
More file actions
203 lines (150 loc) · 4.68 KB
/
OnnxInstance.cpp
File metadata and controls
203 lines (150 loc) · 4.68 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
200
201
202
203
#include "OnnxInstance.h"
#include "BgBlur.h"
/**
* Onnx
*/
OnnxInstance* Onnx::get(obs_source_t* source)
{
auto it = m_instances.find(source);
if (it == m_instances.end())
return nullptr;
return it->second.second.get();
}
void Onnx::registerIncrementSource(obs_source_t* source)
{
auto it = m_instances.find(source);
if (it == m_instances.end())
m_instances[source] = std::make_pair(1, std::make_unique<OnnxInstance>());
else
it->second.first++;
}
void Onnx::unregisterDeIncrementSource(obs_source_t* source)
{
auto it = m_instances.find(source);
if (it == m_instances.end())
return;
it->second.first--;
if (it->second.first <= 0)
m_instances.erase(it);
}
/**
* OnnxInstance
*/
OnnxInstance::OnnxInstance()
{
}
OnnxInstance::~OnnxInstance()
{
}
void OnnxInstance::init(const std::wstring &onnxModelPath)
{
//::MessageBox(0, onnxModelPath.c_str(), onnxModelPath.c_str(), 0);
m_model = std::make_unique<OnnxModel>(onnxModelPath.c_str());
if (!m_model->isGood())
{
blog(LOG_ERROR, "OnnxModel failed to init properly.");
m_model = nullptr;
}
}
bool OnnxInstance::update(obs_source_t* source, gs_texrender_t* texrender, gs_stagesurf_t* stagesurface, const std::vector<OnnxModel::Category>& cats)
{
// More than 1 filter may feed off of this. We don't want to duplicate the workload.
if (m_lastUpdate == obs_get_video_frame_time())
return true;
if (m_model == nullptr)
return false;
cv::Mat fullBGRA;
if (!getRGBAFromStageSurface(texrender, stagesurface, source, m_maskWidth, m_maskHeight, fullBGRA))
return false;
bool boolQueryOnnx = true;
// 10fps is fine because we use temporal smoothing
if (boolQueryOnnx && ::clock() - m_lastModelRun < 50)
boolQueryOnnx = false;
cv::Mat mask;
if (boolQueryOnnx)
{
m_model->runImage(fullBGRA, cv::COLOR_BGRA2RGB, m_lastOnnxOutput);
m_lastModelRun = ::clock();
}
for (auto& cat : cats)
{
auto ref = m_lastOnnxOutput[cat].clone();
if (ref.empty())
return false;
if (m_temporalSmoothFactor <= 0 || m_lastSmallMask[cat].empty() || m_lastSmallMask[cat].size() != ref.size() || m_lastSmallMask[cat].type() != ref.type())
{
m_lastSmallMask[cat] = ref.clone();
}
else if (m_temporalSmoothFactor > 0)
{
const double f = std::clamp(m_temporalSmoothFactor, 0.0f, 1.0f);
cv::addWeighted(m_lastSmallMask[cat], f, ref, 1.0 - f, 0.0, m_lastSmallMask[cat]);
}
mask = m_lastSmallMask[cat].clone();
// edit as needed
static float smoothContour = 1.0f;
if (smoothContour > 0.0)
{
int k = (int)(3 + 11);
if ((k & 1) == 0)
++k;
cv::stackBlur(mask, mask, cv::Size(k, k));
}
// Resize mask back to input image size
cv::resize(mask, mask, fullBGRA.size());
// If we smoothed, re-binarize
if (smoothContour > 0.0)
mask = mask > 128;
m_lastFullMask[cat] = mask;
m_lastFullBGRA[cat] = fullBGRA.clone();
}
m_lastUpdate = obs_get_video_frame_time();
return true;
}
/*static*/
bool OnnxInstance::getRGBAFromStageSurface(gs_texrender_t*& texrender, gs_stagesurf_t*& stagesurface, obs_source_t* source, uint32_t &width, uint32_t &height, cv::Mat &outputBGRA)
{
// Captures a live video frame from a source, renders it to a texture, transfers it onto
// a staging surface, maps it into CPU-accessible memory, then it wraps the pixel buffer into an OpenCV cv::Mat (BGRA format)
if (!obs_source_enabled(source))
return false;
obs_source_t* target = obs_filter_get_target(source);
if (!target)
return false;
width = obs_source_get_base_width(target);
height = obs_source_get_base_height(target);
if (width == 0 || height == 0)
return false;
gs_texrender_reset(texrender);
if (!gs_texrender_begin(texrender, width, height))
return false;
struct vec4 background;
vec4_zero(&background);
gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
gs_ortho(0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height), -100.0f, 100.0f);
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
obs_source_video_render(target);
gs_blend_state_pop();
gs_texrender_end(texrender);
if (stagesurface)
{
uint32_t stagesurf_width = gs_stagesurface_get_width(stagesurface);
uint32_t stagesurf_height = gs_stagesurface_get_height(stagesurface);
if (stagesurf_width != width || stagesurf_height != height)
{
gs_stagesurface_destroy(stagesurface);
stagesurface = nullptr;
}
}
if (!stagesurface)
stagesurface = gs_stagesurface_create(width, height, GS_BGRA);
gs_stage_texture(stagesurface, gs_texrender_get_texture(texrender));
uint8_t* video_data;
uint32_t linesize;
if (!gs_stagesurface_map(stagesurface, &video_data, &linesize))
return false;
outputBGRA = cv::Mat(height, width, CV_8UC4, video_data, linesize);
gs_stagesurface_unmap(stagesurface);
return true;
}