Skip to content

Commit 47e0bf1

Browse files
committed
ML: throw errors when fail to load .onnx or _label.txt
1 parent d2dd5b2 commit 47e0bf1

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

SerialPrograms/Source/ML/Inference/ML_YOLOv5Detector.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QMessageBox>
1111
#include <opencv2/imgproc.hpp>
1212
#include <opencv2/imgcodecs.hpp>
13+
#include "Common/Cpp/Exceptions.h"
1314
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
1415
#include "CommonFramework/VideoPipeline/VideoOverlay.h"
1516
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
@@ -32,29 +33,38 @@ YOLOv5Detector::~YOLOv5Detector() = default;
3233
YOLOv5Detector::YOLOv5Detector(const std::string& model_path)
3334
{
3435
if (!model_path.ends_with(".onnx")){
35-
std::cerr << "Error: wrong model path extension: " << model_path << ". It must be .onnx" << std::endl;
36-
QMessageBox box;
37-
box.critical(nullptr, "Wrong Model Extension",
38-
QString::fromStdString("YOLOv5 model path must end with .onnx. But got " + model_path + "."));
39-
return;
36+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION,
37+
"Error: YOLOv5 model path must end with .onnx. But got " + model_path + ".");
38+
39+
// std::cerr << "Error: wrong model path extension: " << model_path << ". It must be .onnx" << std::endl;
40+
// QMessageBox box;
41+
// box.critical(nullptr, "Wrong Model Extension",
42+
// QString::fromStdString("YOLOv5 model path must end with .onnx. But got " + model_path + "."));
43+
// return;
4044
}
4145

4246
std::string label_file_path = model_path.substr(0, model_path.size() - 5) + "_label.txt";
4347
std::vector<std::string> labels;
4448
if (!std::filesystem::exists(label_file_path)){
45-
std::cerr << "Error: no such YOLOv5 label file path " << label_file_path << "." << std::endl;
46-
QMessageBox box;
47-
box.critical(nullptr, "YOLOv5 Label File Does Not Exist",
48-
QString::fromStdString("YOLOv5 label file path " + label_file_path + " does not exist."));
49-
return;
49+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION,
50+
"Error: YOLOv5 label file path " + label_file_path + " does not exist.");
51+
52+
// std::cerr << "Error: no such YOLOv5 label file path " << label_file_path << "." << std::endl;
53+
// QMessageBox box;
54+
// box.critical(nullptr, "YOLOv5 Label File Does Not Exist",
55+
// QString::fromStdString("YOLOv5 label file path " + label_file_path + " does not exist."));
56+
// return;
5057
}
5158
std::ifstream label_file(label_file_path);
5259
if (!label_file.is_open()){
53-
std::cerr << "Error: failed to open YOLOv5 label file " << label_file_path << "." << std::endl;
54-
QMessageBox box;
55-
box.critical(nullptr, "Cannot Open YOLOv5 Label File",
56-
QString::fromStdString("YOLOv5 label file " + label_file_path + " cannot be opened."));
57-
return;
60+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION,
61+
"Error: YOLOv5 label file " + label_file_path + " cannot be opened.");
62+
63+
// std::cerr << "Error: failed to open YOLOv5 label file " << label_file_path << "." << std::endl;
64+
// QMessageBox box;
65+
// box.critical(nullptr, "Cannot Open YOLOv5 Label File",
66+
// QString::fromStdString("YOLOv5 label file " + label_file_path + " cannot be opened."));
67+
// return;
5868
}
5969
std::string line;
6070
while (std::getline(label_file, line)){

0 commit comments

Comments
 (0)