-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathObjectDetectorTOP.cpp
More file actions
405 lines (345 loc) · 10.4 KB
/
ObjectDetectorTOP.cpp
File metadata and controls
405 lines (345 loc) · 10.4 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
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
405
/* Shared Use License: This file is owned by Derivative Inc. (Derivative)
* and can only be used, and/or modified for use, in conjunction with
* Derivative's TouchDesigner software, and only if you are a licensee who has
* accepted Derivative's TouchDesigner license or assignment agreement
* (which also govern the use of this file). You may share or redistribute
* a modified version of this file provided the following conditions are met:
*
* 1. The shared file or redistribution must retain the information set out
* above and this list of conditions.
* 2. Derivative's name (Derivative Inc.) or its trademarks may not be used
* to endorse or promote products derived from this file without specific
* prior written permission from Derivative.
*/
#include "ObjectDetectorTOP.h"
#include "Parameters.h"
#include <cassert>
#include <string>
#include <sstream>
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
enum class
InfoChopChan
{
Tracked,
Confidence,
Tx,
Ty,
W,
H,
Size
};
// These functions are basic C function, which the DLL loader can find
// much easier than finding a C++ Class.
// The DLLEXPORT prefix is needed so the compile exports these functions from the .dll
// you are creating
extern "C"
{
DLLEXPORT
void
FillTOPPluginInfo(TD::TOP_PluginInfo *info)
{
// This must always be set to this constant
info->apiVersion = TD::TOPCPlusPlusAPIVersion;
// Change this to change the executeMode behavior of this plugin.
info->executeMode = TD::TOP_ExecuteMode::CPUMem;
// For more information on OP_CustomOPInfo see CPlusPlus_Common.h
TD::OP_CustomOPInfo& customInfo = info->customOPInfo;
// Unique name of the node which starts with an upper case letter, followed by lower case letters or numbers
customInfo.opType->setString("Objectdetector");
// English readable name
customInfo.opLabel->setString("Object Detector");
// Information of the author of the node
customInfo.authorName->setString("Author Name");
customInfo.authorEmail->setString("email@email.ca");
// This TOP takes one input
customInfo.minInputs = 1;
customInfo.maxInputs = 1;
}
DLLEXPORT
TD::TOP_CPlusPlusBase*
CreateTOPInstance(const TD::OP_NodeInfo* info, TD::TOP_Context* context)
{
// Return a new instance of your class every time this is called.
// It will be called once per TOP that is using the .dll
return new ObjectDetectorTOP(info, context);
}
DLLEXPORT
void
DestroyTOPInstance(TD::TOP_CPlusPlusBase* instance, TD::TOP_Context *context)
{
// Delete the instance here, this will be called when
// Touch is shutting down, when the TOP using that instance is deleted, or
// if the TOP loads a different DLL
delete (ObjectDetectorTOP*)instance;
}
};
ObjectDetectorTOP::ObjectDetectorTOP(const TD::OP_NodeInfo* info, TD::TOP_Context* context ) :
myFrame{ new cv::Mat() }, myClassifier{ new cv::CascadeClassifier() },
myObjects{}, myLevelWeights{}, myRejectLevels{}, myPath{}, myScale{},
myMinNeighbors{}, myLimitSize{}, myMinSize{}, myMaxSize{}, myDrawBoundingBox{},
myLimitObjs{}, myMaxObjs{},
myContext(context),
myExecuteCount(0),
myPrevDownRes(nullptr)
{
}
ObjectDetectorTOP::~ObjectDetectorTOP()
{
delete myFrame;
delete myClassifier;
}
void
ObjectDetectorTOP::getGeneralInfo(TD::TOP_GeneralInfo* ginfo, const TD::OP_Inputs*, void*)
{
ginfo->cookEveryFrameIfAsked = false;
}
void
ObjectDetectorTOP::execute(TD::TOP_Output* output,
const TD::OP_Inputs* inputs,
void* reserved1)
{
myExecuteCount++;
using namespace cv;
inputToMat(inputs);
if (myFrame->empty())
return;
if (!myPrevDownRes)
return;
handleParameters(inputs);
TD::TOP_UploadInfo info;
info.textureDesc = myPrevDownRes->textureDesc;
info.colorBufferIndex = 0;
resize(*myFrame, *myFrame, cv::Size(info.textureDesc.width, info.textureDesc.height));
Mat frameGray;
cvtColor(*myFrame, frameGray, COLOR_BGRA2GRAY);
try
{
myClassifier->load(myPath);
myClassifier->detectMultiScale(frameGray, myObjects, myRejectLevels, myLevelWeights, myScale, myMinNeighbors, 0, myMinSize, myMaxSize, true);
}
catch (...)
{
// If something went wrong just empty detected objects
myObjects.clear();
}
if (myLimitObjs && myObjects.size() > myMaxObjs)
myObjects.resize(myMaxObjs);
if (myDrawBoundingBox)
drawBoundingBoxes();
cvMatToOutput(*myFrame, output, info);
}
void
ObjectDetectorTOP::setupParameters(TD::OP_ParameterManager* manager, void*)
{
myParms.setup(manager);
}
int32_t
ObjectDetectorTOP::getNumInfoCHOPChans(void*)
{
if (myLimitObjs)
return static_cast<int32_t>(InfoChopChan::Size) * myMaxObjs + 1;
else
return static_cast<int32_t>(InfoChopChan::Size) * static_cast<int32_t>(myObjects.size()) + 1;
}
void
ObjectDetectorTOP::getInfoCHOPChan(int32_t index, TD::OP_InfoCHOPChan* chop, void*)
{
if (index == 0)
{
chop->name->setString("objects_tracked");
chop->value = static_cast<float>(myObjects.size());
return;
}
--index; // Reduce one since the first channel is fixed
int obj = index / static_cast<int>(InfoChopChan::Size) + 1;
int prop = index % static_cast<int>(InfoChopChan::Size);
bool tracked = myObjects.size() >= obj;
if (!tracked)
chop->value = 0.0f;
std::ostringstream oss;
oss << "obj" << obj << ":";
switch (static_cast<InfoChopChan>(prop))
{
case InfoChopChan::Tracked:
{
oss << "tracked";
if (tracked)
chop->value = 1.0f;
break;
}
case InfoChopChan::Confidence:
{
oss << "levelweight";
if (tracked)
chop->value = static_cast<float>(myLevelWeights.at(obj - 1));
break;
}
case InfoChopChan::Tx:
{
oss << "tx";
if (tracked)
chop->value = static_cast<float>(myObjects.at(obj - 1).x);
break;
}
case InfoChopChan::Ty:
{
oss << "ty";
if (tracked)
chop->value = static_cast<float>(myObjects.at(obj - 1).y);
break;
}
case InfoChopChan::W:
{
oss << "w";
if (tracked)
chop->value = static_cast<float>(myObjects.at(obj - 1).width);
break;
}
case InfoChopChan::H:
{
oss << "h";
if (tracked)
chop->value = static_cast<float>(myObjects.at(obj - 1).height);
break;
}
case InfoChopChan::Size:
{
break;
}
}
const std::string& tmp = oss.str();
chop->name->setString(tmp.c_str());
}
bool
ObjectDetectorTOP::getInfoDATSize(TD::OP_InfoDATSize* info, void*)
{
info->byColumn = false;
info->cols = static_cast<int>(InfoChopChan::Size) + 1;
info->rows = myLimitObjs ? myMaxObjs + 1 : static_cast<int32_t>(myObjects.size() + 1);
return true;
}
void
ObjectDetectorTOP::getInfoDATEntries(int32_t index, int32_t nEntries, TD::OP_InfoDATEntries* entries, void*)
{
if (index == 0)
{
entries->values[0]->setString("Object");
entries->values[1]->setString("Tracked");
entries->values[2]->setString("Level Weight");
entries->values[3]->setString("Tx");
entries->values[4]->setString("Ty");
entries->values[5]->setString("W");
entries->values[6]->setString("H");
}
else
{
std::ostringstream oss;
oss << "obj" << index;
const std::string& tmp = oss.str();
entries->values[0]->setString(tmp.c_str());
if (index > myObjects.size())
{
entries->values[1]->setString("0");
entries->values[2]->setString("0");
entries->values[3]->setString("0");
entries->values[4]->setString("0");
entries->values[5]->setString("0");
entries->values[6]->setString("0");
}
else
{
int obj = index - 1;
char buffer[64];
entries->values[1]->setString("1");\
sprintf_s(buffer, "%f", myLevelWeights.at(obj));
entries->values[2]->setString(buffer);
sprintf_s(buffer, "%d", myObjects.at(obj).x);
entries->values[3]->setString(buffer);
sprintf_s(buffer, "%d", myObjects.at(obj).y);
entries->values[4]->setString(buffer);
sprintf_s(buffer, "%d", myObjects.at(obj).width);
entries->values[5]->setString(buffer);
sprintf_s(buffer, "%d", myObjects.at(obj).height);
entries->values[6]->setString(buffer);
}
}
}
void
ObjectDetectorTOP::handleParameters(const TD::OP_Inputs* in)
{
myPath = myParms.evalClassifier(in);
myScale = myParms.evalScalefactor(in);
myMinNeighbors = myParms.evalMinneighbors(in);
myLimitSize = myParms.evalLimitobjectsize(in);
in->enablePar(MinobjectwidthName, myLimitSize);
in->enablePar(MinobjectheightName, myLimitSize);
in->enablePar(MaxobjectwidthName, myLimitSize);
in->enablePar(MaxobjectheightName, myLimitSize);
if (myLimitSize)
{
const TD::OP_TOPInput* top = in->getInputTOP(0);
int32_t totalH = myPrevDownRes->textureDesc.height;
int32_t totalW = myPrevDownRes->textureDesc.width;
double w, h;
w = myParms.evalMinobjectwidth(in);
h = myParms.evalMinobjectheight(in);
myMinSize = cv::Size(static_cast<int>(w * totalW), static_cast<int>(h * totalH));
w = myParms.evalMaxobjectwidth(in);
h = myParms.evalMaxobjectheight(in);
myMaxSize = cv::Size(static_cast<int>(w * totalW), static_cast<int>(h * totalH));
}
else
{
myMinSize = cv::Size();
myMaxSize = cv::Size();
}
myDrawBoundingBox = myParms.evalDrawboundingbox(in);
myLimitObjs = myParms.evalLimitobjectsdetected(in);
in->enablePar(MaximumobjectsName, myLimitObjs);
myMaxObjs = myLimitObjs ? myParms.evalMaximumobjects(in) : 0;
}
void
ObjectDetectorTOP::cvMatToOutput(const cv::Mat& M, TD::TOP_Output* out, TD::TOP_UploadInfo info) const
{
size_t height = info.textureDesc.height;
size_t width = info.textureDesc.width;
size_t imgsize = myPrevDownRes->size;
TD::OP_SmartRef<TD::TOP_Buffer> buf = myContext->createOutputBuffer(imgsize, TD::TOP_BufferFlags::None, nullptr);
cv::flip(M, M, 0);
uint8_t* data = static_cast<uint8_t*>(M.data);
memcpy(buf->data, data, imgsize);
out->uploadBuffer(&buf, info, nullptr);
}
void
ObjectDetectorTOP::inputToMat(const TD::OP_Inputs* in)
{
const TD::OP_TOPInput* top = in->getInputTOP(0);
if (!top)
return;
TD::OP_TOPInputDownloadOptions opts;
opts.verticalFlip = true;
opts.pixelFormat = TD::OP_PixelFormat::BGRA8Fixed;
TD::OP_SmartRef<TD::OP_TOPDownloadResult> downRes = top->downloadTexture(opts, nullptr);
// myPrevDownRes does the job of delaying reading the texture by one frame - replace with downRes to read texture instantly
if (myPrevDownRes)
{
int height = downRes->textureDesc.height;
int width = downRes->textureDesc.width;
*myFrame = cv::Mat(height, width, CV_8UC4);
uint8_t* data = (uint8_t*)myFrame->data;
memcpy(data, myPrevDownRes->getData(), 4 * height * width * sizeof(uint8_t));
}
myPrevDownRes = std::move(downRes);
}
void
ObjectDetectorTOP::drawBoundingBoxes() const
{
cv::Scalar color = cv::Scalar(255, 0, 0);
for (const cv::Rect& obj : myObjects)
{
rectangle(*myFrame, obj, color, 2);
}
}