Skip to content

Commit 3cfbbe9

Browse files
committed
2 parents afcbb15 + 7846561 commit 3cfbbe9

File tree

7 files changed

+570
-457
lines changed

7 files changed

+570
-457
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ file(GLOB MAIN_SOURCES
138138
../Common/Cpp/Options/BatchOption.h
139139
../Common/Cpp/Options/BooleanCheckBoxOption.cpp
140140
../Common/Cpp/Options/BooleanCheckBoxOption.h
141-
../Common/Cpp/Options/ButtonOption.cpp
142-
../Common/Cpp/Options/ButtonOption.h
143141
../Common/Cpp/Options/BoxFloatOption.cpp
144142
../Common/Cpp/Options/BoxFloatOption.h
143+
../Common/Cpp/Options/ButtonOption.cpp
144+
../Common/Cpp/Options/ButtonOption.h
145145
../Common/Cpp/Options/ColorOption.cpp
146146
../Common/Cpp/Options/ColorOption.h
147147
../Common/Cpp/Options/ConfigOption.cpp
@@ -896,13 +896,17 @@ file(GLOB MAIN_SOURCES
896896
Source/Kernels/Waterfill/Kernels_Waterfill_Types.h
897897
Source/ML/DataLabeling/ML_AnnotationIO.cpp
898898
Source/ML/DataLabeling/ML_AnnotationIO.h
899+
Source/ML/DataLabeling/ML_ObjectAnnotation.cpp
900+
Source/ML/DataLabeling/ML_ObjectAnnotation.h
899901
Source/ML/DataLabeling/ML_SegmentAnythingModel.cpp
900902
Source/ML/DataLabeling/ML_SegmentAnythingModel.h
901903
Source/ML/DataLabeling/ML_SegmentAnythingModelConstants.h
902904
Source/ML/ML_Panels.cpp
903905
Source/ML/ML_Panels.h
904906
Source/ML/Programs/ML_LabelImages.cpp
905907
Source/ML/Programs/ML_LabelImages.h
908+
Source/ML/Programs/ML_LabelImagesWidget.cpp
909+
Source/ML/Programs/ML_LabelImagesWidget.h
906910
Source/ML/UI/ML_ImageAnnotationCommandRow.cpp
907911
Source/ML/UI/ML_ImageAnnotationCommandRow.h
908912
Source/ML/UI/ML_ImageAnnotationDisplayOption.cpp
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* ML Object Annotation
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
* Class to store the annotation manually done on an object in an image or video
6+
*/
7+
8+
#include "Common/Cpp/BitmapConversion.h"
9+
#include "Common/Cpp/Json/JsonArray.h"
10+
#include "Common/Cpp/Json/JsonObject.h"
11+
#include "Common/Cpp/Json/JsonValue.h"
12+
#include "ML_ObjectAnnotation.h"
13+
14+
15+
namespace PokemonAutomation{
16+
namespace ML{
17+
18+
19+
ObjectAnnotation::ObjectAnnotation(): user_box(0,0,0,0), mask_box(0,0,0,0) {}
20+
21+
// if failed to pass, will throw JsonParseException
22+
ObjectAnnotation ObjectAnnotation::from_json(const JsonValue& json){
23+
ObjectAnnotation anno_obj;
24+
25+
const JsonObject& json_obj = json.to_object_throw();
26+
const JsonArray& user_box_array = json_obj.get_array_throw("UserBox");
27+
anno_obj.user_box = ImagePixelBox(
28+
size_t(user_box_array[0].to_integer_throw()),
29+
size_t(user_box_array[1].to_integer_throw()),
30+
size_t(user_box_array[2].to_integer_throw()),
31+
size_t(user_box_array[3].to_integer_throw())
32+
);
33+
const JsonArray& mask_box_array = json_obj.get_array_throw("MaskBox");
34+
anno_obj.mask_box = ImagePixelBox(
35+
size_t(mask_box_array[0].to_integer_throw()),
36+
size_t(mask_box_array[1].to_integer_throw()),
37+
size_t(mask_box_array[2].to_integer_throw()),
38+
size_t(mask_box_array[3].to_integer_throw())
39+
);
40+
const size_t mask_width = anno_obj.mask_box.width(), mask_height = anno_obj.mask_box.height();
41+
const size_t num_mask_ele = mask_width * mask_height;
42+
43+
const std::string mask_base64 = json_obj.get_string_throw("Mask");
44+
anno_obj.mask = unpack_bit_vector_from_base64(mask_base64, num_mask_ele);
45+
if (anno_obj.mask.size() != num_mask_ele){
46+
std::string err_msg = "wrong decoded object annotation mask size: decoded " + std::to_string(anno_obj.mask.size())
47+
+ " but should be " + std::to_string(num_mask_ele);
48+
throw ParseException(err_msg);
49+
}
50+
51+
anno_obj.label = json_obj.get_string_throw("Label");
52+
53+
return anno_obj;
54+
}
55+
56+
JsonValue ObjectAnnotation::to_json() const{
57+
JsonObject json_obj;
58+
JsonArray user_box_arr;
59+
user_box_arr.push_back(int64_t(user_box.min_x));
60+
user_box_arr.push_back(int64_t(user_box.min_y));
61+
user_box_arr.push_back(int64_t(user_box.max_x));
62+
user_box_arr.push_back(int64_t(user_box.max_y));
63+
json_obj["UserBox"] = std::move(user_box_arr);
64+
65+
JsonArray mask_box_arr;
66+
mask_box_arr.push_back(int64_t(mask_box.min_x));
67+
mask_box_arr.push_back(int64_t(mask_box.min_y));
68+
mask_box_arr.push_back(int64_t(mask_box.max_x));
69+
mask_box_arr.push_back(int64_t(mask_box.max_y));
70+
json_obj["MaskBox"] = std::move(mask_box_arr);
71+
72+
json_obj["Mask"] = pack_bit_vector_to_base64(mask);
73+
74+
json_obj["Label"] = label;
75+
76+
return json_obj;
77+
}
78+
79+
80+
}
81+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* ML Object Annotation
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
* Class to store the annotation manually done on an object in an image or video
6+
*/
7+
8+
#ifndef PokemonAutomation_ML_ObjectAnnotation_H
9+
#define PokemonAutomation_ML_ObjectAnnotation_H
10+
11+
#include <vector>
12+
#include <string>
13+
#include "Common/Cpp/Json/JsonValue.h"
14+
#include "CommonFramework/ImageTools/ImageBoxes.h"
15+
16+
17+
namespace PokemonAutomation{
18+
namespace ML{
19+
20+
21+
// Store annotation generated by user and ML model on one object
22+
struct ObjectAnnotation{
23+
// user drawn loose bounding box
24+
ImagePixelBox user_box;
25+
// tight bounding box around a mask
26+
ImagePixelBox mask_box;
27+
// per-pixel mask on whether this pixel belongs to the object
28+
// size() equals the total pixels in mask_box
29+
std::vector<bool> mask;
30+
// label of the object
31+
std::string label = "unknown";
32+
33+
ObjectAnnotation();
34+
35+
// if failed to pass, will throw JsonParseException
36+
static ObjectAnnotation from_json(const JsonValue& json);
37+
JsonValue to_json() const;
38+
};
39+
40+
41+
}
42+
}
43+
#endif

0 commit comments

Comments
 (0)