Skip to content

Commit

Permalink
Convert points to float in-place without copying the whole list, add …
Browse files Browse the repository at this point in the history
…conversion in CVAT importer

#1893
#1898
  • Loading branch information
Bobronium committed Jul 29, 2024
1 parent 9306dce commit dc342ca
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 57 deletions.
12 changes: 10 additions & 2 deletions cvat/apps/dataset_manager/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,12 @@ def _import_shape(self, shape, parent_label_id=None):
self.soft_attribute_import and attrib.name not in CVAT_INTERNAL_ATTRIBUTES
)
]
_shape['points'] = list(map(float, _shape['points']))

# FIXME: should be done in the importers, here's as a safeguard (see #1893, #1898)
points = _shape["points"]
for i, point in enumerate(map(float, points)):
points[i] = point

_shape['elements'] = [self._import_shape(element, label_id) for element in _shape.get('elements', [])]

return _shape
Expand All @@ -561,7 +566,10 @@ def _import_track(self, track, parent_label_id=None):
for attrib in shape['attributes']
if self._get_mutable_attribute_id(label_id, attrib.name)
]
shape['points'] = list(map(float, shape['points']))
# FIXME: should be done in the importers, here's as a safeguard (see #1893, #1898)
points = shape["points"]
for i, point in enumerate(map(float, points)):
points[i] = point

return _track

Expand Down
110 changes: 55 additions & 55 deletions cvat/apps/dataset_manager/formats/cvat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,33 +1223,33 @@ def load_anno(file_object, annotations):
shape_element['elements'] = []

if el.tag == 'box':
shape_element['points'].append(el.attrib['xtl'])
shape_element['points'].append(el.attrib['ytl'])
shape_element['points'].append(el.attrib['xbr'])
shape_element['points'].append(el.attrib['ybr'])
shape_element['points'].append(float(el.attrib['xtl']))
shape_element['points'].append(float(el.attrib['ytl']))
shape_element['points'].append(float(el.attrib['xbr']))
shape_element['points'].append(float(el.attrib['ybr']))
elif el.tag == 'ellipse':
shape_element['points'].append(el.attrib['cx'])
shape_element['points'].append(el.attrib['cy'])
shape_element['points'].append("{:.2f}".format(float(el.attrib['cx']) + float(el.attrib['rx'])))
shape_element['points'].append("{:.2f}".format(float(el.attrib['cy']) - float(el.attrib['ry'])))
shape_element['points'].append(float(el.attrib['cx']))
shape_element['points'].append(float(el.attrib['cy']))
shape_element['points'].append(float("{:.2f}".format(float(el.attrib['cx']) + float(el.attrib['rx']))))
shape_element['points'].append(float("{:.2f}".format(float(el.attrib['cy']) - float(el.attrib['ry']))))
elif el.tag == 'cuboid':
shape_element['points'].append(el.attrib['xtl1'])
shape_element['points'].append(el.attrib['ytl1'])
shape_element['points'].append(el.attrib['xbl1'])
shape_element['points'].append(el.attrib['ybl1'])
shape_element['points'].append(el.attrib['xtr1'])
shape_element['points'].append(el.attrib['ytr1'])
shape_element['points'].append(el.attrib['xbr1'])
shape_element['points'].append(el.attrib['ybr1'])

shape_element['points'].append(el.attrib['xtl2'])
shape_element['points'].append(el.attrib['ytl2'])
shape_element['points'].append(el.attrib['xbl2'])
shape_element['points'].append(el.attrib['ybl2'])
shape_element['points'].append(el.attrib['xtr2'])
shape_element['points'].append(el.attrib['ytr2'])
shape_element['points'].append(el.attrib['xbr2'])
shape_element['points'].append(el.attrib['ybr2'])
shape_element['points'].append(float(el.attrib['xtl1']))
shape_element['points'].append(float(el.attrib['ytl1']))
shape_element['points'].append(float(el.attrib['xbl1']))
shape_element['points'].append(float(el.attrib['ybl1']))
shape_element['points'].append(float(el.attrib['xtr1']))
shape_element['points'].append(float(el.attrib['ytr1']))
shape_element['points'].append(float(el.attrib['xbr1']))
shape_element['points'].append(float(el.attrib['ybr1']))

shape_element['points'].append(float(el.attrib['xtl2']))
shape_element['points'].append(float(el.attrib['ytl2']))
shape_element['points'].append(float(el.attrib['xbl2']))
shape_element['points'].append(float(el.attrib['ybl2']))
shape_element['points'].append(float(el.attrib['xtr2']))
shape_element['points'].append(float(el.attrib['ytr2']))
shape_element['points'].append(float(el.attrib['xbr2']))
shape_element['points'].append(float(el.attrib['ybr2']))
else:
for pair in el.attrib['points'].split(';'):
shape_element['points'].extend(map(float, pair.split(',')))
Expand Down Expand Up @@ -1282,39 +1282,39 @@ def load_anno(file_object, annotations):
shape['rotation'] = float(el.attrib.get('rotation', 0))

if el.tag == 'box':
shape['points'].append(el.attrib['xtl'])
shape['points'].append(el.attrib['ytl'])
shape['points'].append(el.attrib['xbr'])
shape['points'].append(el.attrib['ybr'])
shape['points'].append(float(el.attrib['xtl']))
shape['points'].append(float(el.attrib['ytl']))
shape['points'].append(float(el.attrib['xbr']))
shape['points'].append(float(el.attrib['ybr']))
elif el.tag == 'ellipse':
shape['points'].append(el.attrib['cx'])
shape['points'].append(el.attrib['cy'])
shape['points'].append("{:.2f}".format(float(el.attrib['cx']) + float(el.attrib['rx'])))
shape['points'].append("{:.2f}".format(float(el.attrib['cy']) - float(el.attrib['ry'])))
shape['points'].append(float(el.attrib['cx']))
shape['points'].append(float(el.attrib['cy']))
shape['points'].append(float("{:.2f}".format(float(el.attrib['cx']) + float(el.attrib['rx']))))
shape['points'].append(float("{:.2f}".format(float(el.attrib['cy']) - float(el.attrib['ry']))))
elif el.tag == 'mask':
shape['points'] = el.attrib['rle'].split(',')
shape['points'].append(el.attrib['left'])
shape['points'].append(el.attrib['top'])
shape['points'].append("{}".format(int(el.attrib['left']) + int(el.attrib['width']) - 1))
shape['points'].append("{}".format(int(el.attrib['top']) + int(el.attrib['height']) - 1))
shape['points'] = list(map(float, el.attrib['rle'].split(',')))
shape['points'].append(float(el.attrib['left']))
shape['points'].append(float(el.attrib['top']))
shape['points'].append(float("{}".format(int(el.attrib['left']) + int(el.attrib['width']) - 1)))
shape['points'].append(float("{}".format(int(el.attrib['top']) + int(el.attrib['height']) - 1)))
elif el.tag == 'cuboid':
shape['points'].append(el.attrib['xtl1'])
shape['points'].append(el.attrib['ytl1'])
shape['points'].append(el.attrib['xbl1'])
shape['points'].append(el.attrib['ybl1'])
shape['points'].append(el.attrib['xtr1'])
shape['points'].append(el.attrib['ytr1'])
shape['points'].append(el.attrib['xbr1'])
shape['points'].append(el.attrib['ybr1'])

shape['points'].append(el.attrib['xtl2'])
shape['points'].append(el.attrib['ytl2'])
shape['points'].append(el.attrib['xbl2'])
shape['points'].append(el.attrib['ybl2'])
shape['points'].append(el.attrib['xtr2'])
shape['points'].append(el.attrib['ytr2'])
shape['points'].append(el.attrib['xbr2'])
shape['points'].append(el.attrib['ybr2'])
shape['points'].append(float(el.attrib['xtl1']))
shape['points'].append(float(el.attrib['ytl1']))
shape['points'].append(float(el.attrib['xbl1']))
shape['points'].append(float(el.attrib['ybl1']))
shape['points'].append(float(el.attrib['xtr1']))
shape['points'].append(float(el.attrib['ytr1']))
shape['points'].append(float(el.attrib['xbr1']))
shape['points'].append(float(el.attrib['ybr1']))

shape['points'].append(float(el.attrib['xtl2']))
shape['points'].append(float(el.attrib['ytl2']))
shape['points'].append(float(el.attrib['xbl2']))
shape['points'].append(float(el.attrib['ybl2']))
shape['points'].append(float(el.attrib['xtr2']))
shape['points'].append(float(el.attrib['ytr2']))
shape['points'].append(float(el.attrib['xbr2']))
shape['points'].append(float(el.attrib['ybr2']))
elif el.tag == 'skeleton':
pass
else:
Expand Down

0 comments on commit dc342ca

Please sign in to comment.