1+ /* YOLOv5 Detector
2+ *
3+ * From: https://github.com/PokemonAutomation/Arduino-Source
4+ *
5+ */
6+
7+ #include < filesystem>
8+ #include < iostream>
9+ #include < QMessageBox>
10+ #include < opencv2/imgproc.hpp>
11+ #include < opencv2/imgcodecs.hpp>
12+ #include " CommonFramework/ImageTypes/ImageViewRGB32.h"
13+ #include " CommonFramework/VideoPipeline/VideoOverlay.h"
14+ #include " CommonFramework/VideoPipeline/VideoOverlayScopes.h"
15+ #include " CommonFramework/Globals.h"
16+ #include " ML_YOLOv5Detector.h"
17+
18+ // #include <iostream>
19+ // using std::cout;
20+ // using std::endl;
21+
22+ namespace PokemonAutomation {
23+ namespace ML {
24+
25+
26+ YOLOv5Detector::~YOLOv5Detector () = default ;
27+
28+ YOLOv5Detector::YOLOv5Detector ()
29+ {
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;
36+ QMessageBox box;
37+ box.critical (nullptr , " YOLOv5 Model Does Not Exist" ,
38+ QString::fromStdString (" YOLOv5 model path" + sam_model_path + " does not exist." ));
39+ }
40+ }
41+
42+ bool YOLOv5Detector::detect (const ImageViewRGB32& screen){
43+ if (!m_yolo_session){
44+ return false ;
45+ }
46+
47+ cv::Mat frame_mat_bgra = screen.to_opencv_Mat ();
48+ cv::Mat frame_mat_rgb;
49+ cv::cvtColor (frame_mat_bgra, frame_mat_rgb, cv::COLOR_BGRA2RGB);
50+
51+ m_output_boxes.clear ();
52+ m_yolo_session->run (frame_mat_rgb, m_output_boxes);
53+
54+ return m_output_boxes.size () > 0 ;
55+ }
56+
57+
58+ YOLOv5Watcher::YOLOv5Watcher (VideoOverlay& overlay)
59+ : VisualInferenceCallback(" YOLOv5" )
60+ , m_overlay_set(overlay)
61+ {
62+ }
63+
64+ bool YOLOv5Watcher::process_frame (const ImageViewRGB32& frame, WallClock timestamp){
65+ if (!m_detector.session ()){
66+ return false ;
67+ }
68+
69+ m_detector.detect (frame);
70+
71+ m_overlay_set.clear ();
72+ 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 ));
74+ }
75+ return false ;
76+ }
77+
78+
79+
80+
81+
82+ }
83+ }
0 commit comments