def load_from_pascal_xml_filename(cls, classes, filename): "Load image info from a file in the PASCAL VOC XML format." def get_data_from_tag(node, tag): if tag is "bndbox": x1 = int( node.getElementsByTagName(tag) [0].childNodes[1].childNodes[0].data) y1 = int( node.getElementsByTagName(tag) [0].childNodes[3].childNodes[0].data) x2 = int( node.getElementsByTagName(tag) [0].childNodes[5].childNodes[0].data) y2 = int( node.getElementsByTagName(tag) [0].childNodes[7].childNodes[0].data) return (x1, y1, x2, y2) else: return node.getElementsByTagName(tag)[0].childNodes[0].data with open(filename) as f: data = minidom.parseString(f.read()) # image info name = get_data_from_tag(data, "filename") filename = opjoin(config.VOC_dir, 'JPEGImages', name) size = data.getElementsByTagName("size")[0] im_width = int(get_data_from_tag(size, "width")) im_height = int(get_data_from_tag(size, "height")) im_depth = int(get_data_from_tag(size, "depth")) width = im_width height = im_height img = Image(width, height, classes, name) # per-object info objects = [] for obj in data.getElementsByTagName("object"): clas = str(get_data_from_tag(obj, "name")).lower().strip() diff = int(get_data_from_tag(obj, "difficult")) trun = int(get_data_from_tag(obj, "truncated")) rect = get_data_from_tag(obj, "bndbox") bbox = BoundingBox(rect, format='corners') cls_ind = classes.index(clas) objects.append(np.hstack((bbox.get_arr(), cls_ind, diff, trun))) if len(objects) > 0: img.objects_table = Table(np.array(objects), cls.columns) else: img.objects_table = Table(None, cls.columns) return img
def load_from_json_data(cls, classes, data): "Return an Image instantiated from a JSON representation." name = data['name'] width = data['size'][0] height = data['size'][1] img = Image(width, height, classes, name) objects = [] for obj in data['objects']: bbox = BoundingBox(obj['bbox']) cls_name = obj['class'] cls_ind = classes.index(cls_name) diff = obj['diff'] trun = obj['trun'] objects.append(np.hstack((bbox.get_arr(), cls_ind, diff, trun))) if len(objects) > 0: img.objects_table = Table(np.array(objects), cls.columns) else: img.objects_table = Table(None, cls.columns) return img
def test_get_whole_image_bbox(self): image = Image(20, 10, [], 'test_image') assert (image.get_whole_image_bbox() == BoundingBox((0, 0, 20, 10))) image = Image(2, 100, [], 'test_image') assert (image.get_whole_image_bbox() == BoundingBox((0, 0, 2, 100)))
def get_whole_image_bbox(self): "Return a BoundingBox with (0,0,width,height) of the image." return BoundingBox((0, 0, self.width, self.height))