MCPcopy Create free account
hub / github.com/MITK/MITK / PostInferRequest

Method PostInferRequest

Modules/Segmentation/src/Interactions/mitkMonaiLabelTool.cpp:474–556  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

472}
473
474void mitk::MonaiLabelTool::PostInferRequest(const std::string &hostName,
475 const int &port,
476 const std::string &filePath,
477 const std::string &outFile,
478 const mitk::BaseGeometry *baseGeometry)
479{
480 std::string &modelName = m_RequestParameters->model.name; // Get this from args as well.
481 std::string postPath = "/infer/"; // make this separate class of constants
482 postPath.append(modelName);
483 std::ifstream input(filePath, std::ios::binary);
484 if (!input)
485 {
486 MITK_WARN << "could not read file to POST";
487 }
488 std::stringstream buffer_lf_img;
489 buffer_lf_img << input.rdbuf();
490 input.close();
491 httplib::UploadFormDataItems items;
492 if (m_RequestParameters->model.IsInteractive())
493 {
494 std::string foreground = this->ConvertPointsAsListString(baseGeometry, m_PointSetPositive);
495 std::string background = this->ConvertPointsAsListString(baseGeometry, m_PointSetNegative);
496 bool resetState = (m_PointSetPositive->GetSize() == 1 && m_PointSetNegative->GetSize() == 0);
497 std::stringstream paramString;
498 paramString << "{"
499 << "\"foreground\":" << foreground
500 << ",\"background\":" << background
501 << ",\"reset_state\":" << (resetState ? "true" : "false")
502 << "}";
503 MITK_DEBUG << paramString.str();
504 items.push_back({"params", paramString.str(), "", ""});
505 }
506 else // Auto models
507 {
508 items.push_back({"params", "{\"restore_label_idx\": true}", "", ""});
509 }
510 items.push_back({"file", buffer_lf_img.str(), "post_from_mitk.nii.gz", "application/octet-stream"});
511 httplib::SSLClient cli(hostName, port);
512 cli.set_read_timeout(m_Timeout);
513 cli.enable_server_certificate_verification(false);
514 if (auto response = cli.Post(postPath, items))
515 {
516 if (response->status == 200)
517 {
518 // Find boundary
519 std::string boundaryString = ::GetBoundaryString(response);
520 MITK_DEBUG << "boundary hash: " << boundaryString;
521
522 // Parse metadata JSON
523 std::string resBody = response->body;
524 std::vector<std::string> multiPartResponse = ::GetPartsBetweenBoundary(resBody, boundaryString);
525
526 std::string metaData = multiPartResponse[0];
527 std::string contentTypeJson = "Content-Type: application/json";
528 size_t ctPos = metaData.find(contentTypeJson) + contentTypeJson.length();
529 std::string metaDataPart = metaData.substr(ctPos);
530 MITK_DEBUG << metaDataPart;
531 auto jsonObj = nlohmann::json::parse(metaDataPart);

Callers 1

DoUpdatePreviewMethod · 0.95

Calls 11

GetBoundaryStringFunction · 0.85
GetPartsBetweenBoundaryFunction · 0.85
GetResponseImageStringFunction · 0.85
closeMethod · 0.80
IsInteractiveMethod · 0.80
PostMethod · 0.80
flushMethod · 0.80
GetSizeMethod · 0.45
findMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected