forked from TheFoundryVisionmongers/nuke-ML-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLClient.cpp
764 lines (674 loc) · 25.4 KB
/
MLClient.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// Copyright (c) 2018 Foundry.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*************************************************************************
#include <cstring>
#include "MLClient.h"
const char* const MLClient::kClassName = "MLClient";
const char* const MLClient::kHelpString =
"Connects to a Python server for Machine Learning inference.";
const char* const MLClient::kDefaultHostName = "172.17.0.2";
const int MLClient::kDefaultPortNumber = 55555;
const DD::Image::ChannelSet MLClient::kDefaultChannels = DD::Image::Mask_RGB;
const int MLClient::kDefaultNumberOfChannels = MLClient::kDefaultChannels.size();
using namespace DD::Image;
/*! This is a function that creates an instance of the operator, and is
needed for the Iop::Description to work.
*/
static Iop* MLClientCreate(Node* node)
{
return new MLClient(node);
}
/*! The Iop::Description is how NUKE knows what the name of the operator is,
how to create one, and the menu item to show the user. The menu item may be
0 if you do not want the operator to be visible.
*/
const Iop::Description MLClient::description(MLClient::kClassName, 0, MLClientCreate);
//! Constructor. Initialize user controls to their default values.
MLClient::MLClient(Node* node)
: DD::Image::PlanarIop(node)
, _host(MLClient::kDefaultHostName)
, _hostIsValid(true)
, _port(MLClient::kDefaultPortNumber)
, _portIsValid(true)
, _chosenModel(0)
, _modelSelected(false)
, _showDynamic(false)
, _numNewKnobs(0)
, _modelManager(this)
{ }
MLClient::~MLClient() {}
//! The maximum number of input connections the operator can have.
int MLClient::maximum_inputs() const
{
if (haveValidModelInfo() && _modelSelected) {
return _numInputs[_chosenModel];
}
else {
return 1;
}
}
//! The minimum number of input connections the operator can have.
int MLClient::minimum_inputs() const
{
if (haveValidModelInfo() && _modelSelected) {
return _numInputs[_chosenModel];
}
else {
return 1;
}
}
/*! Return the text Nuke should draw on the arrow head for input \a input
in the DAG window. This should be a very short string, one letter
ideally. Return null or an empty string to not label the arrow.
*/
const char* MLClient::input_label(int input, char* buffer) const
{
if (!haveValidModelInfo() || !_modelSelected) {
return "";
}
else {
if ((input < _inputNames[_chosenModel].size()) && (_chosenModel < _inputNames.size())) {
return _inputNames[_chosenModel][input].c_str();
}
else {
return "";
}
}
}
bool MLClient::useStripes() const
{
return false;
}
bool MLClient::renderFullPlanes() const
{
return true;
}
MLClientModelManager& MLClient::getModelManager()
{
return dynamic_cast<MLClient*>(firstOp())->_modelManager;
}
int MLClient::getNumNewKnobs()
{
return dynamic_cast<MLClient*>(firstOp())->_numNewKnobs;
}
void MLClient::setNumNewKnobs(int i)
{
dynamic_cast<MLClient*>(firstOp())->_numNewKnobs = i;
}
void MLClient::_validate(bool forReal)
{
// Try connect to the server, erroring if it can't connect.
std::string connectErrorMsg;
if (!haveValidModelInfo() && !refreshModelsAndKnobsFromServer(connectErrorMsg)) {
error(connectErrorMsg.c_str());
}
// The only other thing needed to do in validate is copy the image info.
copy_info();
}
void MLClient::getRequests(const Box& box, const ChannelSet& channels, int count, RequestOutput &reqData) const
{
// request all input input as we are going to search the whole input area
for (int i = 0, endI = getInputs().size(); i < endI; i++) {
const ChannelSet readChannels = input(i)->info().channels();
input(i)->request(readChannels, count);
}
}
void MLClient::renderStripe(ImagePlane& imagePlane)
{
// Before doing any rendering, check if we've aborted.
// Note that it's perfectly fine to abort here, it usually
// means the user has scrubbed quickly around on the timeline
// or switched between Viewers.
if (aborted() || cancelled()) {
// The following print is commented out as it happens too frequently
// MLClientComms::Vprint("Aborted before processing images.");
return;
}
// Check that it's connected and set up correctly
if (haveValidModelInfo() && _modelSelected) {
// Set up our error string
std::string errorMsg;
// Set up our incoming response message structure.
mlserver::RespondWrapper responseWrapper;
// Wrap up our image data to be sent, send it, and
// retrieve the response.
if(!processImage(_host, _port, responseWrapper, errorMsg)) {
// Test if the failure was due to Nuke aborting
if (aborted() || cancelled()) {
// errorMsg should be filled with where / when the
// processImage() call was aborted.
MLClientComms::Vprint(errorMsg);
return;
}
// Display the error in Nuke if it was some systematic issue.
error(errorMsg.c_str());
return;
}
// If control reached here then responseWrapper contains a valid
// response, so let's try to extract an image from it and
// place it into our imagePlane.
if (!renderOutputBuffer(responseWrapper, imagePlane, errorMsg)) {
MLClientComms::Vprint(errorMsg);
error(errorMsg.c_str());
return;
}
// If control reached here, it's all good, return.
return;
}
// Check again if we hit abort during processing
if (aborted() || cancelled()) {
MLClientComms::Vprint("Aborted without processing image.");
return;
}
// If we reached here by default let's pull an image from input0() so
// that it's at least passing something through.
input0().fetchPlane(imagePlane);
}
bool MLClient::refreshModelsAndKnobsFromServer(std::string& errorMsg)
{
// Before trying to connect, ensure ports and hostname are valid.
if (!_portIsValid) {
errorMsg = "Port is invalid.";
return false;
}
if(!_hostIsValid) {
errorMsg = "Hostname is invalid.";
return false;
}
// Actually try to connect, and pull model info
mlserver::RespondWrapper responseWrapper;
{
// Local scope our comms object so that the connection is torn
// down after we have our data.
MLClientComms comms(_host, _port);
if (!comms.isConnected()) {
errorMsg = "Could not connect to server. Please check your host / port numbers.";
return false;
}
// Try pull the model info into the responseWrapper
if(!comms.sendInfoRequestAndReadInfoResponse(responseWrapper, errorMsg)) {
// If it failed, the error is set, return.
return false;
}
}
// Parse message and fill in menu items for enumeration knob
_serverModels.clear();
_numInputs.clear();
_inputNames.clear();
std::vector<std::string> modelNames;
int numModels = responseWrapper.r1().num_models();
std::stringstream ss;
ss << "Server can serve " << std::to_string(numModels) << " models" << std::endl;
ss << "-----------------------------------------------";
MLClientComms::Vprint(ss.str());
for (int i = 0; i < numModels; i++) {
mlserver::Model m;
m = responseWrapper.r1().models(i);
modelNames.push_back(m.label());
_serverModels.push_back(m);
_numInputs.push_back(m.inputs_size());
std::vector<std::string> names;
for (int j = 0; j < m.inputs_size(); j++) {
mlserver::ImagePrototype p;
p = m.inputs(j);
names.push_back(p.name());
}
_inputNames.push_back(names);
}
// Sanity check that some models were returned
if (_serverModels.size() == 0) {
errorMsg = "Server returned no models.";
return false;
}
// Change enumeration knob choices
Enumeration_KnobI* pSelectModelEnum = _selectedModelknob->enumerationKnob();
pSelectModelEnum->menu(modelNames);
if (_chosenModel >= (int)numModels) {
_selectedModelknob->set_value(0);
_chosenModel = 0;
}
// We try to select the model saved in the serial serialiseKnob if any.
bool restoreModel = false;
MLClientModelKnob* modelKnob = nullptr;
DD::Image::Knob* k = knob("serialiseKnob");
if(k != nullptr) {
modelKnob = dynamic_cast<MLClientModelKnob*>(k);
if(modelKnob != nullptr) {
std::string modelLabel = modelKnob->getModel();
if(modelLabel != "") {
const std::vector<std::string>& models = pSelectModelEnum->menu();
int i = 0;
for(auto& m : models) {
if(m == modelLabel) {
_chosenModel = i;
_selectedModelknob->set_value(_chosenModel);
restoreModel = true;
break;
}
i++;
}
}
}
}
// Set member variables to indicate our connections and model set-up succeeded.
_modelSelected = true;
_showDynamic = true;
// Update the dynamic knobs
const mlserver::Model m = _serverModels[_chosenModel];
if (this == this->firstOp()) {
getModelManager().parseOptions(m);
}
setNumNewKnobs(replace_knobs(knob("models"), getNumNewKnobs(), addDynamicKnobs, this->firstOp()));
// If we have restored a model, we also need to restore its parameters
// now that its knobs have been created,
if(restoreModel && (modelKnob != nullptr)) {
for(const std::pair<std::string, std::string>& keyVal: modelKnob->getParameters()) {
restoreKnobValue(keyVal.first, keyVal.second);
}
}
// Return true if control made it here, success.
return true;
}
void MLClient::restoreKnobValue(const std::string& knobName, const std::string& value)
{
// We look for the corresponding knob
DD::Image::Knob* paramKnob = knob(knobName.c_str());
if(paramKnob != nullptr) {
// Is this an animation curve?
if(value.substr(0, 6) == "{curve") {
// This is a curve, we remove the { }
std::string curveString = value.substr(1, value.find("}") - 1);
paramKnob->set_animation(curveString.c_str(), 0);
}
else if(value.substr(0, 1) == "{") {
// That's an expression
std::string expressionString = value.substr(1, value.find("}") - 1);
// If the expression is within double quote, we need to extract it
if(expressionString.substr(0, 1) == "\"") {
expressionString.erase(0, 1);
expressionString = expressionString.substr(0, expressionString.find("\""));
} else {
// The expression might be followed by keys that we ignore here.
if(expressionString.find(" ") != std::string::npos) {
expressionString = expressionString.substr(0, expressionString.find(" "));
}
}
paramKnob->set_expression(expressionString.c_str(), 0);
}
else {
// That's one value
paramKnob->set_text(value.c_str());
}
}
}
//! Return whether we successfully managed to pull model
//! info from the server at some time in the past, and the selected model is
//! valid.
bool MLClient::haveValidModelInfo() const
{
return _serverModels.size() > 0 && _serverModels.size() > _chosenModel;
}
bool MLClient::processImage(const std::string& hostStr, int port,
mlserver::RespondWrapper& responseWrapper, std::string& errorMsg)
{
// Check if Nuke has aborted, making it impossible to pull images.
if (aborted()) {
errorMsg = "Process aborted at beginning of processing images.";
return false;
}
try {
// Sanity check that some models exist and a valid one is selected.
if (!haveValidModelInfo()) {
errorMsg = "No models exist to send to server.";
return false;
}
// Checking again after connection is made, just in case.
if (aborted()) {
errorMsg = "Process aborted after connection.";
return false;
}
// Validate ourself before proceeding, this ensures if this is being invoked by a button press
// then it's set up correctly.
if (!tryValidate(/*for_real*/true)) {
errorMsg = "Could not set-up node correctly.";
return false;
}
// And again after validate
if (aborted()) {
errorMsg = "Process aborted after validating self.";
return false;
}
// Set our format box, this is the dimension of the
// image that will be passed to the server.
const Box imageFormat = info().format();
// Create inference message
mlserver::RequestInference* requestInference = new mlserver::RequestInference;
mlserver::Model* m = new mlserver::Model(_serverModels[_chosenModel]);
getModelManager().updateOptions(*m);
requestInference->set_allocated_model(m);
// Parse image. TODO: Check for multiple inputs, different channel size
for (int i = 0; i < node_inputs(); i++) {
// Create an ImagePlane, and read each input into it.
// Get our input & sanity check
DD::Image::Iop* inputIop = dynamic_cast<DD::Image::Iop*>(input(i));
if (inputIop == NULL) {
errorMsg = "Input is empty or not connected.";
return false;
}
// Checking before validating inputs
if (aborted()) {
errorMsg = "Process aborted before validating inputs.";
return false;
}
// Try validate & request the input, this should be quick if the data
// has already been requested.
if (!inputIop->tryValidate(/*force*/true)) {
errorMsg = "Unable to validate input.";
return false;
}
// Set our input bounding box, this is what our inputs can give us.
Box imageBounds = inputIop->info();
// We're going to clip it to our format.
imageBounds.intersect(imageFormat);
const int fx = imageBounds.x();
const int fy = imageBounds.y();
const int fr = imageBounds.r();
const int ft = imageBounds.t();
// Request our default channels, for our own bounding box
inputIop->request(fx, fy, fr, ft, kDefaultChannels, 0);
// Let's assume everything went fine, and fetch our plane
ImagePlane plane(imageBounds, /*packed*/ true, kDefaultChannels, kDefaultNumberOfChannels);
inputIop->fetchPlane(plane);
// Sanity check that that the plane was filled successfully, and nothing
// was interrupted.
if (plane.usage() == 0) {
errorMsg = "No image data fetched from input.";
return false;
}
// Checking after fetching inputs
if (aborted()) {
errorMsg = "Process aborted after fetching inputs.";
return false;
}
// Set up our message
mlserver::Image* image = requestInference->add_images();
image->set_width(imageFormat.w());
image->set_height(imageFormat.h());
image->set_channels(kDefaultNumberOfChannels);
// Set up our temp contiguous buffer
size_t byteBufferSize = imageFormat.w() * imageFormat.h() * kDefaultNumberOfChannels * sizeof(float);
if (byteBufferSize == 0) {
errorMsg = "Image size is zero.";
return false;
}
// Create and zero our buffer
byte* byteBuffer = new byte[byteBufferSize];
std::memset(byteBuffer, 0, byteBufferSize);
// Copy the data from our image plane to the buffer. Ideally
// this should be done directly on the plane's data but it
// can't guarantee that it's contiguous, or packed in the
// expected way.
float* floatBuffer = (float*)byteBuffer;
for (int z = 0; z < kDefaultNumberOfChannels; z++) {
const int chanStride = z * imageFormat.w() * imageFormat.h();
for (int ry = fy; ry < ft; ry++) {
const int rowStride = ry * imageFormat.w();
ImageTileReadOnlyPtr tile = plane.readableAt(ry, z);
for (int rx = fx, currentPos = 0; rx < fr; rx++) {
size_t fullPos = chanStride + rowStride + currentPos++;
floatBuffer[fullPos] = tile[rx];
}
}
}
// Set the image data on our message, and release the temp buffer.
image->set_image(byteBuffer, byteBufferSize);
delete[] byteBuffer;
}
// Send the inference request, await and process the response.
{
// Local scope our comms object so that the connection is torn
// down after we have our data.
MLClientComms comms(_host, _port);
if (!comms.isConnected()) {
errorMsg = "Could not connect to server. Please check your host / port numbers.";
return false;
}
// Try pull the model info into the responseWrapper
if (!comms.sendInferenceRequestAndReadInferenceResponse(*requestInference, responseWrapper, errorMsg)) {
// If it failed, the error is set, return.
return false;
}
}
}
catch (...) {
errorMsg = "Error processing messages.";
MLClientComms::Vprint(errorMsg);
return false;
}
// Return true to indicate success
return true;
}
bool MLClient::renderOutputBuffer(mlserver::RespondWrapper& responseWrapper, DD::Image::ImagePlane& imagePlane, std::string& errorMsg)
{
// Sanity check, make sure the response actually contains an image.
if (!responseWrapper.has_r2() || responseWrapper.r2().num_images() == 0) {
errorMsg = "No image found in message response.";
return false;
}
// Validate ourself before proceeding, this ensures if this is being invoked by a button press
// then it's set up correctly. This will return immediately if it's already set up.
if (!tryValidate(/*for_real*/true)) {
errorMsg = "Could not set-up node correctly.";
return false;
}
// Get the resulting image data
const mlserver::Image &imageMessage = responseWrapper.r2().images(0);
// Verify that the image passed back to us is of the same format as the input
// format (note, the bounds of the imagePlane may be different, e.g. if there's
// a Crop on the input.)
const Box imageFormat = info().format();
if (imageMessage.width() != imageFormat.w() || imageMessage.height() != imageFormat.h()) {
errorMsg = "Received Image has dimensions different than expected";
return false;
}
// Set the dimensions of the imagePlane, note this can be different than the format.
// Clip it to the intersection of the image format.
Box imageBounds = imagePlane.bounds();
imageBounds.intersect(imageFormat);
const int fx = imageBounds.x();
const int fy = imageBounds.y();
const int fr = imageBounds.r();
const int ft = imageBounds.t();
// This is going to copy back the minimum intersection of channels between
// what's required to fill in imagePlane, and what's been returned
// in the response. This allows us to gracefully handle cases where the returned
// image has too few channels, or when the imagePlane has too many.
const size_t numChannelsToCopy = (imageMessage.channels() < imagePlane.channels().size()) ? imageMessage.channels() : imagePlane.channels().size();
// Copy the data
const char* imageByteDataPtr = imageMessage.image().c_str();
// Sanity check our image has the correct number of elements
const size_t numImageElements = imageMessage.image().size() / sizeof(float);
const size_t numImageElementsToCopy = numChannelsToCopy * (ft - fy) * (fr - fx);
if (numImageElements < numImageElementsToCopy) {
errorMsg = "Received Image has insuffient elements.";
return false;
}
// Allow the imagePlane to be writable
imagePlane.makeWritable();
float* imageFloatDataPtr = (float*)imageByteDataPtr;
for (int z = 0; z < numChannelsToCopy; z++) {
const int chanStride = z * imageFormat.w() * imageFormat.h();
for (int ry = fy; ry < ft; ry++) {
const int rowStride = ry * imageFormat.w();
for (int rx = fx, currentPos = 0; rx < fr; rx++) {
int fullPos = chanStride + rowStride + currentPos++;
imagePlane.writableAt(rx, ry, z) = imageFloatDataPtr[fullPos];
}
}
}
// If this is reached here, return true for success
return true;
}
void MLClient::addDynamicKnobs(void* p, Knob_Callback f)
{
if (((MLClient *)p)->getShowDynamic()) {
for (int i = 0; i < ((MLClient *)p)->getModelManager().getNumOfInts(); i++) {
std::string name = ((MLClient *)p)->getModelManager().getDynamicIntName(i);
std::string label = ((MLClient *)p)->getModelManager().getDynamicIntName(i);
Int_knob(f, ((MLClient *)p)->getModelManager().getDynamicIntValue(i), name.c_str(), label.c_str());
SetFlags(f, Knob::DO_NOT_WRITE);
Newline(f, " ");
}
for (int i = 0; i < ((MLClient *)p)->getModelManager().getNumOfFloats(); i++) {
std::string name = ((MLClient *)p)->getModelManager().getDynamicFloatName(i);
std::string label = ((MLClient *)p)->getModelManager().getDynamicFloatName(i);
Float_knob(f, ((MLClient *)p)->getModelManager().getDynamicFloatValue(i), name.c_str(), label.c_str());
ClearFlags(f, Knob::SLIDER);
SetFlags(f, Knob::DO_NOT_WRITE);
Newline(f, " ");
}
for (int i = 0; i < ((MLClient *)p)->getModelManager().getNumOfBools(); i++) {
std::string name = ((MLClient *)p)->getModelManager().getDynamicBoolName(i);
std::string label = ((MLClient *)p)->getModelManager().getDynamicBoolName(i);
Bool_knob(f, ((MLClient *)p)->getModelManager().getDynamicBoolValue(i), name.c_str(), label.c_str());
SetFlags(f, Knob::DO_NOT_WRITE);
Newline(f, " ");
}
for (int i = 0; i < ((MLClient *)p)->getModelManager().getNumOfStrings(); i++) {
std::string name = ((MLClient *)p)->getModelManager().getDynamicStringName(i);
std::string label = ((MLClient *)p)->getModelManager().getDynamicStringName(i);
String_knob(f, ((MLClient *)p)->getModelManager().getDynamicStringValue(i), name.c_str(), label.c_str());
SetFlags(f, Knob::DO_NOT_WRITE);
Newline(f, " ");
}
for (int i = 0; i < ((MLClient *)p)->getModelManager().getNumOfButtons(); i++) {
std::string name = ((MLClient *)p)->getModelManager().getDynamicButtonName(i);
std::string label = ((MLClient *)p)->getModelManager().getDynamicButtonName(i);
Button(f, name.c_str(), label.c_str());
Newline(f, " ");
}
}
}
void MLClient::knobs(Knob_Callback f)
{
String_knob(f, &_host, "host");
SetFlags(f, Knob::ALWAYS_SAVE);
Int_knob(f, &_port, "port");
SetFlags(f, Knob::ALWAYS_SAVE);
Button(f, "connect", "Connect");
Divider(f, " ");
static const char* static_choices[] = {
0};
Knob* knob = Enumeration_knob(f, &_chosenModel, static_choices, "models", "Models");
if (knob) {
_selectedModelknob = knob;
}
SetFlags(f, Knob::SAVE_MENU);
// We create a knob to save/load the current state of the dynamic knobs.
if(f.makeKnobs()) {
CustomKnob1(MLClientModelKnob, f, this, "serialiseKnob");
}
if (!f.makeKnobs()) {
MLClient::addDynamicKnobs(this->firstOp(), f);
}
}
int MLClient::knob_changed(Knob* knobChanged)
{
if (knobChanged->is("host")) {
if (!MLClientComms::ValidateHostName(_host)) {
error("Please insert a valid host ipv4 or ipv6 address.");
_hostIsValid = false;
}
else {
_hostIsValid = true;
}
return 1;
}
if (knobChanged->is("port")) {
if (_port > 65535 || _port < 0) {
error("Port out of range.");
_portIsValid = false;
}
else {
_portIsValid = true;
}
return 1;
}
if (knobChanged->is("connect")) {
std::string connectErrorMsg;
if (!refreshModelsAndKnobsFromServer(connectErrorMsg)) {
error(connectErrorMsg.c_str());
}
return 1;
}
if (knobChanged->is("models")) {
// Sanity check that some models exist
if (haveValidModelInfo()) {
const mlserver::Model m = _serverModels[_chosenModel];
getModelManager().parseOptions(m);
setNumNewKnobs(replace_knobs(knob("models"), getNumNewKnobs(), addDynamicKnobs, this->firstOp()));
}
return 1;
}
// Check if dynamic button is pressed
for (int i = 0; i < getModelManager().getNumOfButtons(); i++) {
if (knobChanged->is(getModelManager().getDynamicButtonName(i).c_str())) {
// Set current button to true (pressed) for model inference
getModelManager().setDynamicButtonValue(i, true);
// Set up our error string
std::string errorMsg;
// Set up our incoming response message structure.
mlserver::RespondWrapper responseWrapper;
// Wrap up our image data to be sent, send it, and
// retrieve the response.
if (!processImage(_host, _port, responseWrapper, errorMsg)) {
error(errorMsg.c_str());
}
// Get the resulting general data
if (responseWrapper.has_r2() && responseWrapper.r2().num_objects() > 0) {
const mlserver::FieldValuePairAttrib object = responseWrapper.r2().objects(0);
// Run script in Nuke if object called PythonScript is created
if (object.name() == "PythonScript") {
// Check object has string_attributes
if (object.values_size() != 0
&& object.values(0).string_attributes_size() != 0) {
mlserver::StringAttrib pythonScript = object.values(0).string_attributes(0);
// Run Python Script in Nuke
if (pythonScript.values_size() != 0) {
script_command(pythonScript.values(0).c_str(), true, false);
script_unlock();
}
}
}
}
// Set current button to false (unpressed)
getModelManager().setDynamicButtonValue(i, false);
return 1;
}
}
return 0;
}
//! Return the name of the class.
const char* MLClient::Class() const
{
return MLClient::kClassName;
}
const char* MLClient::node_help() const
{
return MLClient::kHelpString;
}
bool MLClient::getShowDynamic() const
{
return _showDynamic && haveValidModelInfo();
}