|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +format_spec = { |
| 3 | + "name": "MOT", |
| 4 | + "dumpers": [ |
| 5 | + { |
| 6 | + "display_name": "{name} {format} {version}", |
| 7 | + "format": "CSV", |
| 8 | + "version": "1.0", |
| 9 | + "handler": "dump" |
| 10 | + }, |
| 11 | + ], |
| 12 | + "loaders": [ |
| 13 | + { |
| 14 | + "display_name": "{name} {format} {version}", |
| 15 | + "format": "CSV", |
| 16 | + "version": "1.0", |
| 17 | + "handler": "load", |
| 18 | + } |
| 19 | + ], |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +MOT = [ |
| 24 | + "frame_id", |
| 25 | + "track_id", |
| 26 | + "xtl", |
| 27 | + "ytl", |
| 28 | + "width", |
| 29 | + "height", |
| 30 | + "confidence", |
| 31 | + "class_id", |
| 32 | + "visibility" |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +def dump(file_object, annotations): |
| 37 | + """ Export track shapes in MOT CSV format. Due to limitations of the MOT |
| 38 | + format, this process only supports rectangular interpolation mode |
| 39 | + annotations. |
| 40 | + """ |
| 41 | + import csv |
| 42 | + import io |
| 43 | + |
| 44 | + # csv requires a text buffer |
| 45 | + with io.TextIOWrapper(file_object, encoding="utf-8") as csv_file: |
| 46 | + writer = csv.DictWriter(csv_file, fieldnames=MOT) |
| 47 | + for i, track in enumerate(annotations.tracks): |
| 48 | + for shape in track.shapes: |
| 49 | + # MOT doesn't support polygons or 'outside' property |
| 50 | + if shape.type != 'rectangle': |
| 51 | + continue |
| 52 | + writer.writerow({ |
| 53 | + "frame_id": shape.frame, |
| 54 | + "track_id": i, |
| 55 | + "xtl": shape.points[0], |
| 56 | + "ytl": shape.points[1], |
| 57 | + "width": shape.points[2] - shape.points[0], |
| 58 | + "height": shape.points[3] - shape.points[1], |
| 59 | + "confidence": 1, |
| 60 | + "class_id": track.label, |
| 61 | + "visibility": 1 - int(shape.occluded) |
| 62 | + }) |
| 63 | + |
| 64 | + |
| 65 | +def load(file_object, annotations): |
| 66 | + """ Read MOT CSV format and convert objects to annotated tracks. |
| 67 | + """ |
| 68 | + import csv |
| 69 | + import io |
| 70 | + tracks = {} |
| 71 | + # csv requires a text buffer |
| 72 | + with io.TextIOWrapper(file_object, encoding="utf-8") as csv_file: |
| 73 | + reader = csv.DictReader(csv_file, fieldnames=MOT) |
| 74 | + for row in reader: |
| 75 | + # create one shape per row |
| 76 | + xtl = float(row["xtl"]) |
| 77 | + ytl = float(row["ytl"]) |
| 78 | + xbr = xtl + float(row["width"]) |
| 79 | + ybr = ytl + float(row["height"]) |
| 80 | + shape = annotations.TrackedShape( |
| 81 | + type="rectangle", |
| 82 | + points=[xtl, ytl, xbr, ybr], |
| 83 | + occluded=float(row["visibility"]) == 0, |
| 84 | + outside=False, |
| 85 | + keyframe=False, |
| 86 | + z_order=0, |
| 87 | + frame=int(row["frame_id"]), |
| 88 | + attributes=[], |
| 89 | + ) |
| 90 | + # build trajectories as lists of shapes in track dict |
| 91 | + track_id = int(row["track_id"]) |
| 92 | + if track_id not in tracks: |
| 93 | + tracks[track_id] = annotations.Track(row["class_id"], track_id, []) |
| 94 | + tracks[track_id].shapes.append(shape) |
| 95 | + for track in tracks.values(): |
| 96 | + # Set outside=True for the last shape since MOT has no support |
| 97 | + # for this flag |
| 98 | + last = annotations.TrackedShape( |
| 99 | + type=track.shapes[-1].type, |
| 100 | + points=track.shapes[-1].points, |
| 101 | + occluded=track.shapes[-1].occluded, |
| 102 | + outside=True, |
| 103 | + keyframe=track.shapes[-1].keyframe, |
| 104 | + z_order=track.shapes[-1].z_order, |
| 105 | + frame=track.shapes[-1].frame, |
| 106 | + attributes=track.shapes[-1].attributes, |
| 107 | + ) |
| 108 | + track.shapes[-1] = last |
| 109 | + annotations.add_track(track) |
0 commit comments