Files
sign-detector/tools/export_signs_model.py
T
2026-09-27 19:12:14 +03:00

45 lines
1.4 KiB
Python

"""Export an Ultralytics YOLO detect model for this Flutter app.
Class names must be traffic-sign numbers from the Russian traffic rules,
for example 3.27 or 1.23. Their order becomes assets/models/labels.txt.
The app reads float32 NHWC input [1, N, N, 3]. Do not export half or int8.
python tools/export_signs_model.py best.pt --imgsz 320
If the installed Ultralytics build no longer accepts format=tflite, the
script retries format=litert. Both write a .tflite file.
"""
from __future__ import annotations
import argparse
from pathlib import Path
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("weights", type=Path)
parser.add_argument("--imgsz", type=int, default=320)
args = parser.parse_args()
from ultralytics import YOLO
model = YOLO(str(args.weights))
try:
exported = model.export(format="tflite", imgsz=args.imgsz)
except Exception:
exported = model.export(format="litert", imgsz=args.imgsz)
names = model.names
ordered = [str(names[index]) for index in range(len(names))]
labels_path = Path("assets/models/labels.txt")
labels_path.write_text("\n".join(ordered) + "\n", encoding="utf-8")
print(f"Exported: {exported}")
print(f"Wrote {labels_path} ({len(ordered)} classes)")
print("Copy the .tflite file to assets/models/signs.tflite and rebuild.")
if __name__ == "__main__":
main()