Skip to content

Commit 5f22792

Browse files
author
Gin
committed
use base64 to store annotation mask
1 parent 0a0c028 commit 5f22792

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

SerialPrograms/Source/ML/Programs/ML_LabelImages.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <filesystem>
2020
#include <cmath>
2121
#include "CommonFramework/Globals.h"
22+
#include "Common/Cpp/BitmapConversion.h"
2223
#include "Common/Cpp/Json/JsonArray.h"
2324
#include "Common/Cpp/Json/JsonObject.h"
2425
#include "Common/Cpp/Json/JsonValue.h"
@@ -64,11 +65,15 @@ ObjectAnnotation json_to_object_annotation(const JsonValue& value){
6465
size_t(mask_box_array[2].to_integer_throw()),
6566
size_t(mask_box_array[3].to_integer_throw())
6667
);
67-
size_t mask_width = anno_obj.mask_box.width(), mask_height = anno_obj.mask_box.height();
68-
anno_obj.mask.resize(mask_width * mask_height);
69-
const JsonArray& mask_values = json_obj.get_array_throw("Mask");
70-
for(size_t i = 0; i < anno_obj.mask.size(); i++){
71-
anno_obj.mask[i] = bool(mask_values[i].to_integer_throw());
68+
const size_t mask_width = anno_obj.mask_box.width(), mask_height = anno_obj.mask_box.height();
69+
const size_t num_mask_ele = mask_width * mask_height;
70+
71+
const std::string mask_base64 = json_obj.get_string_throw("Mask");
72+
anno_obj.mask = unpack_bit_vector_from_base64(mask_base64, num_mask_ele);
73+
if (anno_obj.mask.size() != num_mask_ele){
74+
std::string err_msg = "wrong decoded object annotation mask size: decoded " + std::to_string(anno_obj.mask.size())
75+
+ " but should be " + std::to_string(num_mask_ele);
76+
throw ParseException(err_msg);
7277
}
7378

7479
anno_obj.label = json_obj.get_string_throw("Label");
@@ -92,11 +97,7 @@ JsonObject object_annotation_to_json(const ObjectAnnotation& object_annotation){
9297
mask_box_arr.push_back(int64_t(object_annotation.mask_box.max_y));
9398
json_obj["MaskBox"] = std::move(mask_box_arr);
9499

95-
JsonArray mask_arr;
96-
for(size_t i = 0; i < object_annotation.mask.size(); i++){
97-
mask_arr.push_back(int64_t(object_annotation.mask[i]));
98-
}
99-
json_obj["Mask"] = std::move(mask_arr);
100+
json_obj["Mask"] = pack_bit_vector_to_base64(object_annotation.mask);
100101

101102
json_obj["Label"] = object_annotation.label;
102103

@@ -270,7 +271,7 @@ void LabelImages::load_image_related_data(const std::string& image_path, size_t
270271
return;
271272
}
272273

273-
JsonValue loaded_json = parse_json(json_content);
274+
const JsonValue loaded_json = parse_json(json_content);
274275
const JsonArray* json_array = loaded_json.to_array();
275276
if (json_array == nullptr){
276277
m_fail_to_load_annotation_file = true;

0 commit comments

Comments
 (0)