gaze-lle-p150 / code /examples /quickstart.py
changh95's picture
tt-model push gaze-lle-p150 (container)
8aa515d verified
Raw History Blame Contribute Delete
2.44 kB
# SPDX-License-Identifier: Apache-2.0
"""Gaze-LLE quickstart: the PYTHON.md snippet on the repo's demo image.
pip install -e . # from the repo root, into an environment that already has ttnn (tt-metal)
python code/examples/quickstart.py # writes gaze_quickstart.png + gaze_quickstart.json
Options: --image PATH --bbox x1,y1,x2,y2 (pixels, repeatable) --out PREFIX --device-id N.
The default image is found relative to this file (any working directory works); a relative --image or --out path is
relative to the current directory.
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
REPO = Path(__file__).resolve().parents[2]
def main() -> None:
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--image", default=str(REPO / "media" / "source_1.png"))
ap.add_argument("--bbox", action="append", type=lambda s: [float(v) for v in s.split(",")],
help="head box x1,y1,x2,y2 in pixels (repeatable); default: the 3 heads of media/source_1.png")
ap.add_argument("--out", default="gaze_quickstart", help="output prefix (.png overlay + .json)")
ap.add_argument("--device-id", type=int, default=0)
args = ap.parse_args()
boxes = args.bbox or [[116, 28, 176, 104], [269, 37, 301, 81], [87, 57, 107, 76]]
# --- the snippet -------------------------------------------------------------------------------
from gaze_lle import GazeLLE, visualize
with GazeLLE.from_pretrained(device_id=args.device_id) as model:
result = model(args.image, boxes, bbox_format="pixels") # one image, one box per person
for i in range(len(result)):
x, y = result.gaze_xy[i]
print(f"head {i}: gaze target ({x:.0f}, {y:.0f}) px, in-frame p={result.inout[i]:.2f}")
visualize(args.image, result).save(f"{args.out}.png")
# -----------------------------------------------------------------------------------------------
Path(f"{args.out}.json").write_text(json.dumps({
"image": str(args.image), "image_size": list(result.image_size), "bboxes_px": boxes,
"gaze_xy": result.gaze_xy.tolist(), "inout": result.inout.tolist(), "score": result.score.tolist(),
}, indent=1))
print(f"wrote {args.out}.png and {args.out}.json; heatmaps {tuple(result.heatmap.shape)}")
if __name__ == "__main__":
main()