Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 5bc7fb9

Browse files
CreateML fixes (#906)
Support createML format
1 parent eb603c2 commit 5bc7fb9

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

labelImg.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,7 @@ def load_file(self, file_path=None):
11231123
u"<p>Make sure <i>%s</i> is a valid label file.")
11241124
% (e, unicode_file_path))
11251125
self.status("Error reading %s" % unicode_file_path)
1126+
11261127
return False
11271128
self.image_data = self.label_file.image_data
11281129
self.line_color = QColor(*self.label_file.lineColor)
@@ -1156,7 +1157,7 @@ def load_file(self, file_path=None):
11561157
self.paint_canvas()
11571158
self.add_recent_file(self.file_path)
11581159
self.toggle_actions(True)
1159-
self.show_bounding_box_from_annotation_file(file_path)
1160+
self.show_bounding_box_from_annotation_file(self.file_path)
11601161

11611162
counter = self.counter_str()
11621163
self.setWindowTitle(__appname__ + ' ' + file_path + ' ' + counter)
@@ -1196,10 +1197,15 @@ def show_bounding_box_from_annotation_file(self, file_path):
11961197
else:
11971198
xml_path = os.path.splitext(file_path)[0] + XML_EXT
11981199
txt_path = os.path.splitext(file_path)[0] + TXT_EXT
1200+
json_path = os.path.splitext(file_path)[0] + JSON_EXT
1201+
11991202
if os.path.isfile(xml_path):
12001203
self.load_pascal_xml_by_filename(xml_path)
12011204
elif os.path.isfile(txt_path):
12021205
self.load_yolo_txt_by_filename(txt_path)
1206+
elif os.path.isfile(json_path):
1207+
self.load_create_ml_json_by_filename(json_path, file_path)
1208+
12031209

12041210
def resizeEvent(self, event):
12051211
if self.canvas and not self.image.isNull()\
@@ -1300,10 +1306,13 @@ def change_save_dir_dialog(self, _value=False):
13001306
if dir_path is not None and len(dir_path) > 1:
13011307
self.default_save_dir = dir_path
13021308

1309+
self.show_bounding_box_from_annotation_file(self.file_path)
1310+
13031311
self.statusBar().showMessage('%s . Annotation will be saved to %s' %
13041312
('Change saved folder', self.default_save_dir))
13051313
self.statusBar().show()
13061314

1315+
13071316
def open_annotation_dialog(self, _value=False):
13081317
if self.file_path is None:
13091318
self.statusBar().showMessage('Please select image first')
@@ -1320,6 +1329,17 @@ def open_annotation_dialog(self, _value=False):
13201329
filename = filename[0]
13211330
self.load_pascal_xml_by_filename(filename)
13221331

1332+
elif self.label_file_format == LabelFileFormat.CREATE_ML:
1333+
1334+
filters = "Open Annotation JSON file (%s)" % ' '.join(['*.json'])
1335+
filename = ustr(QFileDialog.getOpenFileName(self, '%s - Choose a json file' % __appname__, path, filters))
1336+
if filename:
1337+
if isinstance(filename, (tuple, list)):
1338+
filename = filename[0]
1339+
1340+
self.load_create_ml_json_by_filename(filename, self.file_path)
1341+
1342+
13231343
def open_dir_dialog(self, _value=False, dir_path=None, silent=False):
13241344
if not self.may_continue():
13251345
return
@@ -1337,6 +1357,9 @@ def open_dir_dialog(self, _value=False, dir_path=None, silent=False):
13371357
target_dir_path = ustr(default_open_dir_path)
13381358
self.last_open_dir = target_dir_path
13391359
self.import_dir_images(target_dir_path)
1360+
self.default_save_dir = target_dir_path
1361+
if self.file_path:
1362+
self.show_bounding_box_from_annotation_file(file_path=self.file_path)
13401363

13411364
def import_dir_images(self, dir_path):
13421365
if not self.may_continue() or not dir_path:

libs/create_ml_io.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def write(self):
3232

3333
output_image_dict = {
3434
"image": self.filename,
35+
"verified": self.verified,
3536
"annotations": []
3637
}
3738

@@ -107,12 +108,15 @@ def parse_json(self):
107108
with open(self.json_path, "r") as file:
108109
input_data = file.read()
109110

110-
output_dict = json.loads(input_data)
111-
self.verified = True
111+
# Returns a list
112+
output_list = json.loads(input_data)
113+
114+
if output_list:
115+
self.verified = output_list[0].get("verified", False)
112116

113117
if len(self.shapes) > 0:
114118
self.shapes = []
115-
for image in output_dict:
119+
for image in output_list:
116120
if image["image"] == self.filename:
117121
for shape in image["annotations"]:
118122
self.add_shape(shape["label"], shape["coordinates"])

libs/labelFile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def save_create_ml_format(self, filename, shapes, image_path, image_data, class_
4848
image_shape, shapes, filename, local_img_path=image_path)
4949
writer.verified = self.verified
5050
writer.write()
51+
return
5152

5253

5354
def save_pascal_voc_format(self, filename, shapes, image_path, image_data,

tests/test_io.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def test_a_write(self):
5050

5151
writer = CreateMLWriter('tests', 'test.512.512.bmp', (512, 512, 1), shapes, output_file,
5252
local_img_path='tests/test.512.512.bmp')
53+
54+
writer.verified = True
5355
writer.write()
5456

5557
# check written json
@@ -58,7 +60,7 @@ def test_a_write(self):
5860

5961
import json
6062
data_dict = json.loads(input_data)[0]
61-
63+
self.assertEqual(True, data_dict['verified'], 'verified tag not reflected')
6264
self.assertEqual('test.512.512.bmp', data_dict['image'], 'filename not correct in .json')
6365
self.assertEqual(2, len(data_dict['annotations']), 'output file contains to less annotations')
6466
face = data_dict['annotations'][1]

0 commit comments

Comments
 (0)