Skip to content

Commit 24a5a93

Browse files
committed
refactor zoo
1 parent f414ac4 commit 24a5a93

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

src/modelzoo/Zoo.cpp

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static std::string MODEL_ZOO_DOWNLOAD_ENDPOINT = "https://easyml.cloud.luxonis.c
2323
static std::string MODEL_ZOO_DEFAULT_CACHE_PATH = ".depthai_cached_models"; // hidden cache folder
2424
static std::string MODEL_ZOO_DEFAULT_MODELS_PATH = "depthai_models"; // folder
2525

26+
#ifdef DEPTHAI_ENABLE_CURL
2627
class ZooManager {
2728
public:
2829
/**
@@ -140,20 +141,6 @@ class ZooManager {
140141
*/
141142
static bool connectionToZooAvailable();
142143

143-
/**
144-
* @brief Get path to yaml file.
145-
* If name is a relative path (e.g. ./yolo.yaml), it is returned as is.
146-
* If name is a full path (e.g. /home/user/models/yolo.yaml), it is returned as is.
147-
* If name is a model name (e.g. yolo) or a model yaml file (e.g. yolo.yaml),
148-
* the function will use modelsPath if provided or the DEPTHAI_ZOO_MODELS_PATH environment variable and return a path to the yaml file.
149-
* For instance, yolo -> ./depthai_models/yolo.yaml (if modelsPath or DEPTHAI_ZOO_MODELS_PATH are ./depthai_models)
150-
*
151-
* @param name: Name of the yaml file
152-
* @param modelsPath: Path to the models folder, use environment variable DEPTHAI_ZOO_MODELS_PATH if not provided
153-
* @return std::string: Path to yaml file
154-
*/
155-
static std::string getYamlFilePath(const std::string& name, const std::string& modelsPath = "");
156-
157144
private:
158145
// Description of the model
159146
NNModelDescription modelDescription;
@@ -165,44 +152,23 @@ class ZooManager {
165152
std::string cacheDirectory;
166153
};
167154

168-
std::string combinePaths(const std::string& path1, const std::string& path2) {
169-
return std::filesystem::path(path1).append(path2).string();
170-
}
171-
172-
std::string ZooManager::getYamlFilePath(const std::string& name, const std::string& modelsPath) {
173-
// No empty names allowed
174-
if(name.empty()) {
175-
throw std::runtime_error("name cannot be empty!");
176-
}
177-
178-
// If the name does not start with any dot or slash, we treat it as the special
179-
// case of where we prepend the DEPTHAI_ZOO_MODELS_PATH environment variable first.
180-
// We check whether the first character is a letter or a number here (model.yaml, model, 3model, ...)
181-
if(isalnum(name[0])) {
182-
std::string useModelsPath = modelsPath;
183-
if(useModelsPath.empty()) {
184-
useModelsPath = utility::getEnvAs<std::string>("DEPTHAI_ZOO_MODELS_PATH", dai::modelzoo::getDefaultModelsPath(), false);
185-
}
186-
std::string path = combinePaths(useModelsPath, name);
187-
188-
// if path does not contain the yaml or yml extension, add it
189-
if(!utility::isYamlFile(path)) {
190-
if(std::filesystem::exists(path + ".yaml")) {
191-
path += ".yaml";
192-
} else if(std::filesystem::exists(path + ".yml")) {
193-
path += ".yml";
194-
} else {
195-
throw std::runtime_error("Model file not found: (neither `" + path + ".yaml` nor `" + path
196-
+ ".yml` exists) | If you meant to use a relative path, prefix with ./ (e.g. `./" + name
197-
+ "`) | Also, make sure the file exists. Read the documentation for more information.");
198-
}
199-
}
200-
return path;
201-
}
155+
#endif
202156

203-
// We treat the name either as a relative path or an absolute path
204-
return name;
205-
}
157+
/**
158+
* @brief Get path to yaml file.
159+
* If name is a relative path (e.g. ./yolo.yaml), it is returned as is.
160+
* If name is a full path (e.g. /home/user/models/yolo.yaml), it is returned as is.
161+
* If name is a model name (e.g. yolo) or a model yaml file (e.g. yolo.yaml),
162+
* the function will use modelsPath if provided or the DEPTHAI_ZOO_MODELS_PATH environment variable and return a path to the yaml file.
163+
* For instance, yolo -> ./depthai_models/yolo.yaml (if modelsPath or DEPTHAI_ZOO_MODELS_PATH are ./depthai_models)
164+
*
165+
* @param name: Name of the yaml file
166+
* @param modelsPath: Path to the models folder, use environment variable DEPTHAI_ZOO_MODELS_PATH if not provided
167+
* @return std::string: Path to yaml file
168+
*/
169+
std::string getYamlFilePath(const std::string& name, const std::string& modelsPath = "");
170+
171+
std::string combinePaths(const std::string& path1, const std::string& path2);
206172

207173
#ifdef DEPTHAI_ENABLE_CURL
208174
std::string generateErrorMessageHub(const cpr::Response& response) {
@@ -597,6 +563,45 @@ bool downloadModelsFromZoo(const std::string& path, const std::string& cacheDire
597563

598564
#endif
599565

566+
std::string combinePaths(const std::string& path1, const std::string& path2) {
567+
return std::filesystem::path(path1).append(path2).string();
568+
}
569+
570+
std::string getYamlFilePath(const std::string& name, const std::string& modelsPath) {
571+
// No empty names allowed
572+
if(name.empty()) {
573+
throw std::runtime_error("name cannot be empty!");
574+
}
575+
576+
// If the name does not start with any dot or slash, we treat it as the special
577+
// case of where we prepend the DEPTHAI_ZOO_MODELS_PATH environment variable first.
578+
// We check whether the first character is a letter or a number here (model.yaml, model, 3model, ...)
579+
if(isalnum(name[0])) {
580+
std::string useModelsPath = modelsPath;
581+
if(useModelsPath.empty()) {
582+
useModelsPath = utility::getEnvAs<std::string>("DEPTHAI_ZOO_MODELS_PATH", dai::modelzoo::getDefaultModelsPath(), false);
583+
}
584+
std::string path = combinePaths(useModelsPath, name);
585+
586+
// if path does not contain the yaml or yml extension, add it
587+
if(!utility::isYamlFile(path)) {
588+
if(std::filesystem::exists(path + ".yaml")) {
589+
path += ".yaml";
590+
} else if(std::filesystem::exists(path + ".yml")) {
591+
path += ".yml";
592+
} else {
593+
throw std::runtime_error("Model file not found: (neither `" + path + ".yaml` nor `" + path
594+
+ ".yml` exists) | If you meant to use a relative path, prefix with ./ (e.g. `./" + name
595+
+ "`) | Also, make sure the file exists. Read the documentation for more information.");
596+
}
597+
}
598+
return path;
599+
}
600+
601+
// We treat the name either as a relative path or an absolute path
602+
return name;
603+
}
604+
600605
std::string SlugComponents::merge() const {
601606
std::ostringstream oss;
602607
if(!teamName.empty()) {
@@ -640,7 +645,7 @@ SlugComponents SlugComponents::split(const std::string& slug) {
640645
}
641646

642647
NNModelDescription NNModelDescription::fromYamlFile(const std::string& modelName, const std::string& modelsPath) {
643-
std::string yamlPath = ZooManager::getYamlFilePath(modelName, modelsPath);
648+
std::string yamlPath = getYamlFilePath(modelName, modelsPath);
644649
if(!std::filesystem::exists(yamlPath)) {
645650
throw std::runtime_error("Model file not found: `" + yamlPath + "` | If you meant to use a relative path, prefix with ./ (e.g. `./" + modelName
646651
+ "`) | Also, make sure the file exists. Read the documentation for more information.");

0 commit comments

Comments
 (0)