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+ }
0 commit comments