Skip to content

Commit 84d4db8

Browse files
author
Gin
committed
enable labeling and storage on ML Labeling program
1 parent f7ea444 commit 84d4db8

File tree

6 files changed

+224
-73
lines changed

6 files changed

+224
-73
lines changed

SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoOverlayWidget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ void VideoOverlayWidget::render_images(QPainter& painter){
224224
QRectF source_rect(0.0, 0.0, static_cast<double>(q_image.width()), static_cast<double>(q_image.height()));
225225
// build a target_rect. target_rect is what region the overlay image should appear inside the overlay viewport.
226226
// target_rect is in pixel units of the viewport
227-
const double target_start_x = width * image_overlay.x;
228-
const double target_start_y = height * image_overlay.y;
229-
const double target_width = width * image_overlay.width;
230-
const double target_height = height * image_overlay.height;
227+
const double target_start_x = width * image_overlay.box.x;
228+
const double target_start_y = height * image_overlay.box.y;
229+
const double target_width = width * image_overlay.box.width;
230+
const double target_height = height * image_overlay.box.height;
231231
QRectF target_rect(target_start_x, target_start_y, target_width, target_height);
232232
painter.drawImage(target_rect, q_image, source_rect);
233233
}

SerialPrograms/Source/CommonFramework/VideoPipeline/VideoOverlayScopes.h

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
#ifndef PokemonAutomation_VideoOverlayScopes_H
88
#define PokemonAutomation_VideoOverlayScopes_H
99

10-
//#include <vector>
1110
#include <deque>
12-
//#include "VideoOverlayTypes.h"
11+
#include "CommonFramework/ImageTypes/ImageRGB32.h"
1312
#include "VideoOverlay.h"
1413

1514
namespace PokemonAutomation{
@@ -55,8 +54,6 @@ class OverlayBoxScope : public OverlayBox{
5554
};
5655

5756

58-
59-
6057
// A text as part of the video overlay.
6158
// It handles its own life time on video overlay: once it's destroyed, it removes itself from VideoOverlay.
6259
class OverlayTextScope : public OverlayText{
@@ -86,6 +83,54 @@ class OverlayTextScope : public OverlayText{
8683
};
8784

8885

86+
// An image as part of the video overlay.
87+
// It owns the image data and handles its own life time on video overlay: once it's destroyed,
88+
// it removes itself from VideoOverlay.
89+
class OverlayImageScope : public OverlayImage{
90+
OverlayImageScope(const OverlayImageScope&) = delete;
91+
void operator=(const OverlayImageScope&) = delete;
92+
93+
public:
94+
~OverlayImageScope(){
95+
m_overlay.remove_image(*this);
96+
}
97+
98+
public:
99+
// the copied `image` is moved into OverlayImageScope
100+
// so after this constructer, the caller can freely modify or delete `image`.
101+
OverlayImageScope(
102+
VideoOverlay& overlay,
103+
ImageRGB32 image,
104+
const ImageFloatBox& box
105+
)
106+
: OverlayImage(ImageViewRGB32(), box)
107+
, m_overlay(overlay)
108+
, m_image_data(std::move(image))
109+
{
110+
this->image = m_image_data;
111+
overlay.add_image(*this);
112+
}
113+
114+
OverlayImageScope(
115+
VideoOverlay& overlay,
116+
ImageViewRGB32 image,
117+
const ImageFloatBox& box
118+
)
119+
: OverlayImage(ImageViewRGB32(), box)
120+
, m_overlay(overlay)
121+
, m_image_data(image.copy())
122+
{
123+
this->image = m_image_data;
124+
overlay.add_image(*this);
125+
}
126+
127+
private:
128+
VideoOverlay& m_overlay;
129+
// owns image content, in contrast to OverlayImage::image which is just a pointer
130+
ImageRGB32 m_image_data;
131+
};
132+
133+
89134

90135
// Used to clear log messages on video overlay automatically.
91136
// Place this at the beginning of a program, so that when the program exits, it will
@@ -114,8 +159,6 @@ class OverlayLogTextScope{
114159
// automatically when VideoOverlaySet is destroyed.
115160
// In this way, the user will see the inference boxes on the video overlay UI
116161
// and those boxes will leave the UI after the session ends.
117-
//
118-
// TODO: shall we add text management to this class, or rename this class to BoxOverleySet?
119162
class VideoOverlaySet{
120163
public:
121164
VideoOverlaySet(VideoOverlay& overlay)
@@ -124,14 +167,22 @@ class VideoOverlaySet{
124167

125168
void clear(){
126169
m_boxes.clear();
170+
m_images.clear();
127171
}
128172
void add(Color color, const ImageFloatBox& box, std::string label = ""){
129173
m_boxes.emplace_back(m_overlay, color, box, std::move(label));
130174
}
175+
void add(ImageRGB32 image, const ImageFloatBox& box){
176+
m_images.emplace_back(m_overlay, std::move(image), box);
177+
}
178+
void add(ImageViewRGB32 image, const ImageFloatBox& box){
179+
m_images.emplace_back(m_overlay, image, box);
180+
}
131181

132182
private:
133183
VideoOverlay& m_overlay;
134184
std::deque<OverlayBoxScope> m_boxes;
185+
std::deque<OverlayImageScope> m_images;
135186
};
136187

137188

SerialPrograms/Source/CommonFramework/VideoPipeline/VideoOverlaySession.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,17 @@ class VideoOverlaySession : public VideoOverlay{
4646
virtual void on_overlay_enabled_log (bool enabled){}
4747
virtual void on_overlay_enabled_stats (bool enabled){}
4848

49+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
50+
// is modified by VideoOverlaySession::add/remove_box().
4951
virtual void on_overlay_update_boxes (const std::shared_ptr<const std::vector<OverlayBox>>& boxes){}
52+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
53+
// is modified by VideoOverlaySession::add/remove_text().
5054
virtual void on_overlay_update_text (const std::shared_ptr<const std::vector<OverlayText>>& texts){}
55+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
56+
// is modified by VideoOverlaySession::add/remove_image().
5157
virtual void on_overlay_update_images (const std::shared_ptr<const std::vector<OverlayImage>>& images){}
58+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
59+
// is modified by VideoOverlaySession::add/remove_log().
5260
virtual void on_overlay_update_log (const std::shared_ptr<const std::vector<OverlayLogLine>>& boxes){}
5361

5462
// This one is different from the others. The listeners will store this
@@ -89,13 +97,21 @@ class VideoOverlaySession : public VideoOverlay{
8997
void set_enabled_log (bool enabled);
9098
void set_enabled_stats (bool enabled);
9199

92-
// Called by rendering infra to access the overlay boxes
100+
// Called by rendering infra to access a copy of the stored overlay boxes.
101+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
102+
// is modified by VideoOverlaySession::add/remove_box().
93103
std::vector<OverlayBox> boxes() const;
94-
// Called by rendering infra to access the overlay texts
104+
// Called by rendering infra to access the overlay texts.
105+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
106+
// is modified by VideoOverlaySession::add/remove_text().
95107
std::vector<OverlayText> texts() const;
96-
// Called by rendering infra to access the overlay images
108+
// Called by rendering infra to access the overlay images.
109+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
110+
// is modified by VideoOverlaySession::add/remove_image().
97111
std::vector<OverlayImage> images() const;
98-
// Called by rendering infra to access the overlay logs
112+
// Called by rendering infra to access the overlay logs.
113+
// Returns a copy to avoid the caller (UI object) accessing the data async while the data
114+
// is modified by VideoOverlaySession::add/remove_log().
99115
std::vector<OverlayLogLine> log_texts() const;
100116

101117
virtual void add_box(const OverlayBox& box) override;

SerialPrograms/Source/CommonFramework/VideoPipeline/VideoOverlayTypes.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,25 @@ struct OverlayText{
6666
// An image as part of the video overlay.
6767
// Allow transparency to create mask overlay.
6868
// Check CommonFramework/VideoPipeline/VideoOverlay.h to see how it's used.
69-
// e.g. OverlayImage(image, x=0.5, y=0.5, width=0.5, height=0.5) will render
69+
// e.g. OverlayImage(image, {x=0.5, y=0.5, width=0.5, height=0.5}) will render
7070
// an overlay image on the lower right quadrant of the view.
71+
// Note: for efficiency, the image stored is just a pointer. Be careful not to
72+
// modify the image data async or release it before the rendering code calls it.
7173
struct OverlayImage{
7274
// Image view. The image data must live longer than the OverlayImage.
7375
// Its alpha channel will be used during rendering.
7476
ImageViewRGB32 image;
7577
// starting x coordinate of the image in the video window, range: 0.0-1.0.
76-
double x = 0.0;
7778
// starting y coordinate of the image in the video window, range: 0.0-1.0.
78-
double y = 0.0;
7979
// relative width of the image in the video window, range: 0.0-1.0
80-
double width = 0.0;
8180
// relative height of the image in the video window, range: 0.0-1.0
82-
double height = 0.0;
83-
81+
ImageFloatBox box;
82+
8483
OverlayImage(
85-
ImageViewRGB32 image,
86-
double x, double y, double width, double height
84+
ImageViewRGB32 image, const ImageFloatBox& box
8785
)
88-
: image(std::move(image))
89-
, x(x), y(y), width(width), height(height)
86+
: image(image)
87+
, box(box)
9088
{}
9189
};
9290

0 commit comments

Comments
 (0)