Skip to content

Commit e01209b

Browse files
committed
2 parents cbb7424 + a035ac2 commit e01209b

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

SerialPrograms/Source/ML/Programs/ML_LabelImages.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ DrawnBoundingBox::~DrawnBoundingBox(){
5757
void DrawnBoundingBox::on_config_value_changed(void* object){
5858
std::lock_guard<std::mutex> lg(m_lock);
5959
m_overlay_set.clear();
60-
m_overlay_set.add(COLOR_RED, {m_program.X, m_program.Y, m_program.WIDTH, m_program.HEIGHT}, "Unknown");
60+
m_overlay_set.add(COLOR_RED, {m_program.X, m_program.Y, m_program.WIDTH, m_program.HEIGHT});
6161
}
6262
void DrawnBoundingBox::on_mouse_press(double x, double y){
6363
m_program.WIDTH.set(0);
@@ -73,7 +73,7 @@ void DrawnBoundingBox::on_mouse_release(double, double){
7373

7474
const size_t source_width = m_program.source_image_width;
7575
const size_t source_height = m_program.source_image_height;
76-
76+
7777
const int box_x = int(m_program.X * source_width + 0.5);
7878
const int box_y = int(m_program.Y * source_height + 0.5);
7979
const int box_width = int(m_program.WIDTH * source_width + 0.5);
@@ -82,25 +82,44 @@ void DrawnBoundingBox::on_mouse_release(double, double){
8282
return;
8383
}
8484

85+
if (m_program.m_image_embedding.size() == 0){
86+
// no embedding file loaded
87+
return;
88+
}
8589
m_program.m_sam_session.run(
8690
m_program.m_image_embedding,
8791
(int)source_height, (int)source_width, {}, {},
8892
{box_x, box_y, box_x + box_width, box_y + box_height},
8993
m_program.m_output_boolean_mask
9094
);
9195

96+
size_t min_mask_x = INT_MAX, max_mask_x = 0;
97+
size_t min_mask_y = INT_MAX, max_mask_y = 0;
9298
for (size_t y = 0; y < source_height; y++){
9399
for (size_t x = 0; x < source_width; x++){
100+
bool mask = m_program.m_output_boolean_mask[y*source_width + x];
94101
uint32_t& pixel = m_program.m_mask_image.pixel(x, y);
95102
// if the pixel's mask value is true, set a semi-transparent 45-degree blue strip color
96103
// otherwise: fully transparent (alpha = 0)
97104
uint32_t color = 0;
98-
if (m_program.m_output_boolean_mask[y*source_width + x]){
105+
if (mask){
99106
color = (std::abs(int(x) - int(y)) % 4 <= 1) ? combine_argb(150, 30, 144, 255) : combine_argb(150, 0, 0, 60);
107+
min_mask_x = std::min(x, min_mask_x);
108+
max_mask_x = std::max(x, max_mask_x);
109+
min_mask_y = std::min(y, min_mask_y);
110+
max_mask_y = std::max(y, max_mask_y);
100111
}
101112
pixel = color;
102113
}
103114
}
115+
if (min_mask_x < INT_MAX && max_mask_x > min_mask_x && min_mask_y < INT_MAX && max_mask_y > min_mask_y){
116+
const int mask_width = max_mask_x - min_mask_x;
117+
const int mask_height = max_mask_y - min_mask_y;
118+
ImageFloatBox mask_box(
119+
min_mask_x/double(source_width), min_mask_y/double(source_height),
120+
mask_width/double(source_width), mask_height/double(source_height));
121+
m_overlay_set.add(COLOR_BLUE, mask_box, "Unknown");
122+
}
104123
if (m_program.m_overlay_image){
105124
m_overlay.remove_image(*m_program.m_overlay_image);
106125
}
@@ -251,6 +270,9 @@ LabelImages_Widget::LabelImages_Widget(
251270
// m_overlay_image = std::make_unique<OverlayImage>(*m_image_mask, 0.0, 0.2, 0.5, 0.5);
252271
// m_session.overlay().add_image(*m_overlay_image);
253272
cout << "LabelImages_Widget built" << endl;
273+
274+
// TODO: create UI to choose pokemon labels: use UI class StringSelectOption
275+
// TODO: create a custom table to display the annotated bounding boxes
254276
}
255277

256278

0 commit comments

Comments
 (0)