66
77#include < filesystem>
88#include < iostream>
9+ #include < fstream>
910#include < QMessageBox>
1011#include < opencv2/imgproc.hpp>
1112#include < opencv2/imgcodecs.hpp>
1213#include " CommonFramework/ImageTypes/ImageViewRGB32.h"
1314#include " CommonFramework/VideoPipeline/VideoOverlay.h"
1415#include " CommonFramework/VideoPipeline/VideoOverlayScopes.h"
1516#include " CommonFramework/Globals.h"
17+ #include " Common/Cpp/StringTools.h"
18+ #include " Common/Cpp/PrettyPrint.h"
1619#include " ML_YOLOv5Detector.h"
1720
1821// #include <iostream>
@@ -25,18 +28,45 @@ namespace ML{
2528
2629YOLOv5Detector::~YOLOv5Detector () = default ;
2730
28- YOLOv5Detector::YOLOv5Detector ()
31+
32+ YOLOv5Detector::YOLOv5Detector (const std::string& model_path)
2933{
30- const std::string sam_model_path = RESOURCE_PATH () + " ML/yolov5.onnx" ;
31- std::vector<std::string> labels = {" Bidoof" };
32- if (std::filesystem::exists (sam_model_path)){
33- m_yolo_session = std::make_unique<YOLOv5Session>(sam_model_path, std::move (labels));
34- } else {
35- std::cerr << " Error: no such YOLOv5 model path " << sam_model_path << " ." << std::endl;
34+ 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 ;
40+ }
41+
42+ std::string label_file_path = model_path.substr (0 , model_path.size () - 5 ) + " _label.txt" ;
43+ std::vector<std::string> labels;
44+ if (!std::filesystem::exists (label_file_path)){
45+ std::cerr << " Error: no such YOLOv5 label file path " << label_file_path << " ." << std::endl;
3646 QMessageBox box;
37- box.critical (nullptr , " YOLOv5 Model Does Not Exist" ,
38- QString::fromStdString (" YOLOv5 model path" + sam_model_path + " does not exist." ));
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 ;
3950 }
51+ std::ifstream label_file (label_file_path);
52+ 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 ;
58+ }
59+ std::string line;
60+ while (std::getline (label_file, line)){
61+ line = StringTools::strip (line);
62+ if (line.empty () || line[0 ] == ' #' ){
63+ continue ;
64+ }
65+ labels.push_back (line);
66+ }
67+ label_file.close ();
68+
69+ m_yolo_session = std::make_unique<YOLOv5Session>(model_path, std::move (labels));
4070}
4171
4272bool YOLOv5Detector::detect (const ImageViewRGB32& screen){
@@ -55,22 +85,24 @@ bool YOLOv5Detector::detect(const ImageViewRGB32& screen){
5585}
5686
5787
58- YOLOv5Watcher::YOLOv5Watcher (VideoOverlay& overlay)
88+ YOLOv5Watcher::YOLOv5Watcher (VideoOverlay& overlay, const std::string& model_path )
5989 : VisualInferenceCallback(" YOLOv5" )
6090 , m_overlay_set(overlay)
91+ , m_detector(model_path)
6192{
6293}
6394
6495bool YOLOv5Watcher::process_frame (const ImageViewRGB32& frame, WallClock timestamp){
65- if (!m_detector.session ()){
96+ if (!m_detector.model_loaded ()){
6697 return false ;
6798 }
6899
69100 m_detector.detect (frame);
70101
71102 m_overlay_set.clear ();
72103 for (const auto & box : m_detector.detected_boxes ()){
73- m_overlay_set.add (COLOR_RED, box.box , m_detector.session ()->label_name (box.label_idx ));
104+ std::string text = m_detector.session ()->label_name (box.label_idx ) + " : " + tostr_fixed (box.score , 2 );
105+ m_overlay_set.add (COLOR_RED, box.box , text);
74106 }
75107 return false ;
76108}
0 commit comments