def load(self, filename): keys = [ 'version', 'imageData', 'imagePath', 'shapes', # polygonal annotations 'flags', # image level flags 'imageHeight', 'imageWidth', ] try: with open(filename, 'rb' if PY2 else 'r') as f: data = json.load(f) version = data.get('version') if version is None: logger.warn('Loading JSON file ({}) of unknown version'.format( filename)) elif version.split('.')[0] != __version__.split('.')[0]: logger.warn('This JSON file ({}) may be incompatible with ' 'current labelme. version in file: {}, ' 'current version: {}'.format( filename, version, __version__)) if data['imageData'] is not None: imageData = base64.b64decode(data['imageData']) if PY2 and QT4: imageData = utils.img_data_to_png_data(imageData) else: # relative path from label file to relative path from cwd imagePath = osp.join(osp.dirname(filename), data['imagePath']) imageData = self.load_image_file(imagePath) flags = data.get('flags') or {} imagePath = data['imagePath'] self._check_image_height_and_width( base64.b64encode(imageData).decode('utf-8'), data.get('imageHeight'), data.get('imageWidth'), ) shapes = [ dict(label=s['label'], points=s['points'], shape_type=s.get('shape_type', 'polygon'), flags=s.get('flags', {}), group_id=s.get('group_id')) for s in data['shapes'] ] except Exception as e: raise LabelFileError(e) otherData = {} for key, value in data.items(): if key not in keys: otherData[key] = value # Only replace data after everything is loaded. self.flags = flags self.shapes = shapes self.imagePath = imagePath self.imageData = imageData self.filename = filename self.otherData = otherData
def load(self, filename): keys = [ "version", 'Patient_Name', 'Patient_ID', 'Modality', 'Study_Date', "imageData", "imagePath", "shapes", # polygonal annotations "flags", # image level flags "imageHeight", "imageWidth", ] shape_keys = [ "label", "points", "group_id", "shape_type", "flags", ] try: with open(filename, "r") as f: data = json.load(f) version = data.get("version") if version is None: logger.warn( "Loading JSON file ({}) of unknown version".format( filename ) ) elif version.split(".")[0] != __version__.split(".")[0]: logger.warn( "This JSON file ({}) may be incompatible with " "current labelme. version in file: {}, " "current version: {}".format( filename, version, __version__ ) ) if data["imageData"] is not None: imageData = base64.b64decode(data["imageData"]) if PY2 and QT4: imageData = utils.img_data_to_png_data(imageData) else: # relative path from label file to relative path from cwd imagePath = osp.join(osp.dirname(filename), data["imagePath"]) imageData = self.load_image_file(imagePath) flags = data.get("flags") or {} imagePath = data["imagePath"] Patient_Name = data['Patient_Name'] Patient_ID = data['Patient_ID'] Modality = data['Modality'] Study_Date = data['Study_Date'] self._check_image_height_and_width( base64.b64encode(imageData).decode("utf-8"), data.get("imageHeight"), data.get("imageWidth"), ) shapes = [ dict( label=s["label"], points=s["points"], shape_type=s.get("shape_type", "polygon"), flags=s.get("flags", {}), group_id=s.get("group_id"), other_data={ k: v for k, v in s.items() if k not in shape_keys }, ) for s in data["shapes"] ] except Exception as e: raise LabelFileError(e) otherData = {} for key, value in data.items(): if key not in keys: otherData[key] = value # Only replace data after everything is loaded. self.flags = flags self.shapes = shapes self.Patient_Name = Patient_Name self.Patient_ID = Patient_ID self.Modality = Modality self.Study_Date = Study_Date self.imagePath = imagePath self.imageData = imageData self.filename = filename self.otherData = otherData