Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Host-side DepthAI source code
- Install development environment dependencies:
- Linux:

sudo apt-get install -y git python-pip cmake cmake-gui libusb-1.0-0-dev
sudo apt-get install -y git python-pip cmake cmake-gui libusb-1.0-0-dev libcurl4-openssl-dev
- macOS:

brew install coreutils python3 cmake libusb wget opencv
brew install coreutils python3 cmake libusb wget opencv curl

- After cloning the repo, update the third-party libraries used:

Expand Down
185 changes: 185 additions & 0 deletions host/core/model_downloader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#include <stdio.h>
#include <curl/curl.h>
#include <string>
#include <iostream>
#include <cmath>
#include <sstream>
#include <fstream>

#define BLUE "\033[94m"
#define ENDC "\033[0m"
#define RED "\033[91m"

#define MAX_SHAVES (16)
#define MAX_CMX_SLICES (16)
#define MAX_NCES (2)

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string *)userp)->append((char *)contents, size * nmemb);
return size * nmemb;
}

static int progress_func(void *ptr, double TotalToDownload, double NowDownloaded,
double TotalToUpload, double NowUploaded)
{
int progress_bar_width = 50;
double download_progress = TotalToDownload ? (NowDownloaded / TotalToDownload) : 0;
int progress_indicator_width = download_progress * progress_bar_width;

int p_bar_i = 0;
printf("%3.0f%% ", download_progress * 100);
printf(RED "[" ENDC);
printf(BLUE);
for (; p_bar_i < progress_indicator_width; p_bar_i++)
{
printf("-");
}
printf(ENDC);
for (; p_bar_i < progress_bar_width; p_bar_i++)
{
printf(" ");
}
printf(RED "]" ENDC "\r");
fflush(stdout);
return 0;
}

static int check_input(int nr_shaves, int nr_cmx_slices, int nr_NCEs)
{
if (nr_shaves <= 0 || nr_shaves > MAX_SHAVES)
{
std::cerr << RED "Number of shaves must be between (0, 16]" << ENDC << std::endl;
return 1;
}
if (nr_cmx_slices <= 0 || nr_cmx_slices > MAX_CMX_SLICES)
{
std::cerr << RED "Number of shaves must be between (0, 16]" << ENDC << std::endl;
return 2;
}
if (nr_NCEs < 0 || nr_NCEs > MAX_NCES)
{
std::cerr << RED "Number of shaves must be between [0, 2]" << ENDC << std::endl;
return 3;
}
return 0;
}

int download_model(std::string model_name, int nr_shaves, int nr_cmx_slices, int nr_NCEs, std::string output_folder_path)
{
if (check_input(nr_shaves, nr_cmx_slices, nr_NCEs))
return 3;
CURL *hnd = curl_easy_init();

if (0)
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1);

curl_easy_setopt(hnd, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "http://luxonis.com:8080/");

std::string readBuffer;

curl_easy_setopt(hnd, CURLOPT_TIMEOUT, 120L);
curl_easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT, 10L);

struct curl_slist *headers = NULL;
// headers = curl_slist_append(headers, "postman-token: 5824015d-b523-e286-69e0-9a4da23af7da");
// headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, &readBuffer);

// Internal CURL progressmeter must be disabled if we provide our own callback
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0);
// Install the callback function
curl_easy_setopt(hnd, CURLOPT_PROGRESSFUNCTION, progress_func);

std::string boundary = "------WebKitFormBoundary7MA4YWxkTrZu0gW";
std::string conent_header = "Content-Disposition: form-data; ";
std::string end_form_line = "\r\n";
std::string end_form = end_form_line + end_form_line;

std::ostringstream os;

std::string platform = nr_NCEs == 0 ? "VPU_MYRIAD_2450" : "VPU_MYRIAD_2480";

os << boundary << end_form_line;
os << conent_header << "name=\"compile_type\"" << end_form << "zoo\r\n";
os << boundary << end_form_line;
os << conent_header << "name=\"model_name\"" << end_form << model_name << end_form_line;
os << boundary << end_form_line;
os << conent_header << "name=\"model_downloader_params\"" << end_form << "--precisions FP16 --num_attempts 5\r\n";
os << boundary << end_form_line;
os << conent_header << "name=\"intermediate_compiler_params\"" << end_form << "--data_type=FP16 --mean_values [127.5,127.5,127.5] --scale_values [255,255,255]\r\n";
os << boundary << end_form_line;
os << conent_header << "name=\"compiler_params\"" << end_form << "-ip U8 -VPU_MYRIAD_PLATFORM " << platform << " -VPU_NUMBER_OF_SHAVES " << nr_shaves << " -VPU_NUMBER_OF_CMX_SLICES " << nr_cmx_slices << end_form_line;
os << boundary << "--";

std::string options_builder = os.str();
// std::cout << options_builder << std::endl;

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, options_builder.c_str());

CURLcode ret = curl_easy_perform(hnd);
// std::cout << ret << std::endl;
std::cout << std::endl;

int ret_code = 0;

long http_code = 0;
curl_easy_getinfo(hnd, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code == 200 && ret == CURLE_OK)
{
//Succeeded
std::cout << "Model downloaded!" << std::endl;
// std::cout << readBuffer.size() << std::endl;
ret_code = 0;
std::ofstream wf(output_folder_path, std::ios::out | std::ios::binary);
if (!wf)
{
std::cerr << "Cannot open file!" << std::endl;
ret_code = 1;
goto cleanup;
}

wf.write(readBuffer.c_str(), readBuffer.size());
wf.close();
if (!wf.good())
{
std::cerr << "Error occurred at writing time!" << std::endl;
ret_code = 2;
goto cleanup;
}
}
else
{
//Failed
std::cerr << RED "Model download failed with http code : " << http_code << ", curl code: " << ret << ENDC << std::endl;
std::cerr << readBuffer << std::endl;
ret_code = http_code;
}

cleanup:
curl_easy_cleanup(hnd);

return (int)ret_code;
}

// int main(void)
// {
// std::string model_name = "mobilenet-ssd";
// int nr_shaves = 6;
// int nr_cmx_slices = 6;
// int nr_NCEs = 0;
// std::string output_path = ".";
// std::ostringstream path;
// path << output_path << "/" << model_name << ".blob" << ".sh" << nr_shaves << "cmx" << nr_cmx_slices;
// if(nr_NCEs == 0)
// {
// path << "NO_NCE";
// }
// return download_model(model_name, nr_shaves, nr_cmx_slices, nr_NCEs, path.str());
// }
4 changes: 4 additions & 0 deletions host/core/model_downloader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <string>

int download_model(std::string model_name, int nr_shaves, int nr_cmx_slices, int nr_NCEs, std::string output_folder_path);
96 changes: 96 additions & 0 deletions host/core/pipeline/host_pipeline_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include "host_pipeline_config.hpp"


static std::unordered_map<int, int> rgb_cam_supported_configs =
{
{1080, 0},
{2160, 1}
};

static std::unordered_map<int, int> mono_cam_supported_configs =
{
{720, 0},
};

#define WARNING "\033[1;5;31m"
#define ENDC "\033[0m"

Expand Down Expand Up @@ -121,6 +132,44 @@ bool HostPipelineConfig::initWithJSON(const json &json_obj)
{
ai.keep_aspect_ratio = ai_obj.at("keep_aspect_ratio").get<bool>();
}

if (ai_obj.contains("shaves"))
{
ai.shaves = ai_obj.at("shaves").get<int32_t>();
}
if (ai.shaves <= 0 || ai.shaves > 16)
{
std::cerr << WARNING "ai.shaves should be in the range (0 .. 16]\n" ENDC;
break;
}

if (ai_obj.contains("cmx_slices"))
{
ai.cmx_slices = ai_obj.at("cmx_slices").get<int32_t>();
}
if (ai.cmx_slices <= 0 || ai.cmx_slices > 19)
{
std::cerr << WARNING "ai.cmx_slices should be in the range (0 .. 19]\n" ENDC;
break;
}

if (ai.shaves > ai.cmx_slices)
{
std::cerr << WARNING "ai.shaves should be <= than ai.cmx_slices\n" ENDC;
break;
}


if (ai_obj.contains("NCEs"))
{
ai.NCEs = ai_obj.at("NCEs").get<int32_t>();
}
if (ai.NCEs < 0 || ai.NCEs > 2)
{
std::cerr << WARNING "ai.NCEs should be in the range [0 .. 2]\n" ENDC;
break;
}

}

// "ot"
Expand Down Expand Up @@ -215,6 +264,53 @@ bool HostPipelineConfig::initWithJSON(const json &json_obj)
}
}

if (json_obj.contains("camera"))
{
auto& camera_conf_obj = json_obj.at("camera");

if (camera_conf_obj.contains("rgb"))
{
auto& rgb_camera_conf_obj = camera_conf_obj.at("rgb");

rgb_cam_config.resolution_h = rgb_camera_conf_obj.at("resolution_h").get<int32_t>();
rgb_cam_config.fps = rgb_camera_conf_obj.at("fps").get<int32_t>();

auto it = rgb_cam_supported_configs.find(rgb_cam_config.resolution_h);

if (it == rgb_cam_supported_configs.end()) {
std::cerr << WARNING "Requested rgb cam config not available!\n" ENDC;

std::cerr << "Supported configs.\n";
for(auto elem : rgb_cam_supported_configs)
{
std::cerr << elem.first << "\n";
}
break;
}
}

if (camera_conf_obj.contains("mono"))
{
auto& mono_camera_conf_obj = camera_conf_obj.at("mono");

mono_cam_config.resolution_h = mono_camera_conf_obj.at("resolution_h").get<int32_t>();
mono_cam_config.fps = mono_camera_conf_obj.at("fps").get<int32_t>();

auto it = mono_cam_supported_configs.find(mono_cam_config.resolution_h);

if (it == mono_cam_supported_configs.end()) {
std::cerr << WARNING "Requested mono cam config not available!\n" ENDC;

std::cerr << "Supported configs.\n";
for(auto elem : mono_cam_supported_configs)
{
std::cerr << elem.first << "\n";
}
break;
}
}
}

result = true;
}
while (false);
Expand Down
15 changes: 15 additions & 0 deletions host/core/pipeline/host_pipeline_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ struct HostPipelineConfig
std::string blob_file_config;
bool calc_dist_to_bb = false;
bool keep_aspect_ratio = true;
int32_t shaves = 4;
int32_t cmx_slices = 4;
int32_t NCEs = 1;
} ai;

struct OT
Expand All @@ -56,6 +59,18 @@ struct HostPipelineConfig
std::string revision;
} board_config;

struct RGBCamConfig
{
int32_t resolution_h;
int32_t fps;
} rgb_cam_config;

struct MonoCamConfig
{
int32_t resolution_h;
int32_t fps;
} mono_cam_config;


bool initWithJSON(const json &json_obj);
bool hasStream(const std::string &stream_name) const;
Expand Down
6 changes: 5 additions & 1 deletion host/py_module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pybind11_add_module(
../core/host_data_reader.cpp
../core/host_capture_command.cpp
../core/disparity_stream_post_processor.cpp
../core/model_downloader.cpp
../../shared/json_helper.cpp
../../shared/stream/stream_info.cpp
../../shared/xlink/xlink_wrapper.cpp
Expand Down Expand Up @@ -102,7 +103,9 @@ endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../../shared/version.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/../../shared/version.hpp @ONLY)


set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIR})

# link libraries
target_link_libraries(
Expand All @@ -111,6 +114,7 @@ target_link_libraries(
-L/usr/local/lib # For macOS
-lusb-1.0
-lpthread
${CURL_LIBRARIES}
nlohmann_json::nlohmann_json
nlohmann_json_schema_validator
)
Loading