"""Tiny server that serves the annotated MP4 + live analytics sidebar.
Usage:
uvicorn tools.ui_server:app --host 0.0.0.0 --port 8090
"""
from fastapi import FastAPI
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from pathlib import Path
import json
ROOT = Path('/home/azureuser/helmet_v5/data/t4_analysis')
VIDEO = ROOT / 'annotated.mp4'
JSONL = ROOT / 'detections.jsonl'
app = FastAPI()
@app.get('/')
def index():
html = """
Helmet analytics
GNT_TMS_269 • 9-10 AM • Helmet analytics
videonetics (reference)
326
Latest detections
"""
return HTMLResponse(html)
@app.get('/video.mp4')
def video():
return FileResponse(str(VIDEO), media_type='video/mp4')
@app.get('/stats')
def stats(t: float = 0.0):
total = helmet = no_helmet = 0
events = []
if JSONL.exists():
with open(JSONL) as f:
for line in f:
try: r = json.loads(line)
except Exception: continue
if r.get('video_sec', 0) > t + 0.5:
break # don't reveal future events
total += 1
if r['label'] == 'helmet':
helmet += 1
else:
no_helmet += 1
events.append(r)
return JSONResponse({
'total': total,
'helmet': helmet,
'no_helmet': no_helmet,
'recent': events[-20:][::-1],
})