File size: 1,887 Bytes
6b41115 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import csv
import json
from pathlib import Path
from ..data.parsers.xml_parser import CLASS_NAMES
FIELDS = [
"video_id",
"frame_id",
"timestamp",
"track_id",
"label",
"class",
"confidence",
"x1",
"y1",
"x2",
"y2",
"world_x",
"world_y",
"speed_kmh",
"zones",
]
class ResultExporter:
"""Write a CSV and streaming JSON array; memory does not grow with video length."""
def __init__(self, output):
output = Path(output)
self.csv_file = output.with_suffix(".csv").open("w", newline="")
self.json_file = output.with_suffix(".json").open("w")
self.writer = csv.DictWriter(self.csv_file, fieldnames=FIELDS)
self.writer.writeheader()
self.json_file.write("[")
self.first = True
def write(self, video_id, frame_id, timestamp, results):
for result in results:
track, world = result["track"], result["world"]
row = dict(
video_id=video_id,
frame_id=frame_id,
timestamp=float(timestamp),
track_id=int(track.track_id),
label=int(track.label),
confidence=float(track.score),
world_x=None if world is None else float(world[0]),
world_y=None if world is None else float(world[1]),
speed_kmh=result["speed_kmh"],
zones=";".join(result["zones"]),
)
row["class"] = CLASS_NAMES[track.label - 1]
row.update(zip(("x1", "y1", "x2", "y2"), map(float, track.box)))
self.writer.writerow(row)
self.json_file.write(("" if self.first else ",\n") + json.dumps(row, allow_nan=False))
self.first = False
def close(self):
self.csv_file.close()
self.json_file.write("]\n")
self.json_file.close()
|