File size: 29,801 Bytes
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d06707
62064b6
 
21c159e
62064b6
 
 
 
aa4beee
 
 
62064b6
 
 
 
 
 
 
 
 
 
407953c
 
 
 
62064b6
8d06707
 
 
 
 
 
 
62064b6
2634224
5671fc1
2634224
 
 
 
 
 
 
 
 
5671fc1
2634224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5671fc1
2634224
5671fc1
2634224
 
5671fc1
8d06707
2634224
8d06707
2634224
 
 
 
 
 
62064b6
8d06707
 
 
 
 
 
 
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d06707
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d27e9c
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488b594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62064b6
 
 
 
 
 
 
488b594
 
 
 
 
 
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d27e9c
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4beee
 
81eea78
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
81eea78
62064b6
 
 
 
81eea78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62064b6
81eea78
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4beee
 
 
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bafb5d4
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4beee
 
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d27e9c
62064b6
 
 
5d27e9c
62064b6
 
 
 
 
 
 
 
 
5d27e9c
 
 
62064b6
 
 
 
5d27e9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62064b6
 
 
5d27e9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62064b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
#!/usr/bin/env python3
"""
ROI-aware compression server (FastAPI)
- Uploads a video and prompt
- Runs YOLOv8x detection + simple tracking
- Produces 3 outputs: overlay (tracking), compressed, ROI-preserved
- Serves MJPEG stream of live overlay

Endpoints:
    POST /track/async
    POST /process/compress/{job_id}
  GET  /process/status/{job_id}
  GET  /process/video/overlay/{job_id}
  GET  /process/video/compressed/{job_id}
  GET  /process/video/roi/{job_id}
  GET  /detect/stream/{job_id}
"""

import os
import uuid
import time
import math
import threading
import shutil
import subprocess
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any

import cv2
import numpy as np
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse, JSONResponse
from ultralytics import YOLO, RTDETR

DEFAULT_WEIGHTS = os.environ.get("YOLO_WEIGHTS", "yolov8s.pt")
WEIGHTS_DIR = os.environ.get("WEIGHTS_DIR", os.path.dirname(__file__))
DEFAULT_CONF = float(os.environ.get("YOLO_CONF", "0.25"))
DEFAULT_DEVICE = os.environ.get("YOLO_DEVICE", "auto")
FAST_DETECT_SCALE = float(os.environ.get("FAST_DETECT_SCALE", "0.65"))
FAST_DETECT_IMGSZ = int(os.environ.get("FAST_DETECT_IMGSZ", "512"))
DATA_DIR = os.environ.get("DATA_DIR", "/tmp/roi_demo")
UPLOAD_DIR = os.path.join(DATA_DIR, "uploads")
OUTPUT_DIR = os.path.join(DATA_DIR, "outputs")

app = FastAPI(title="ROI Compression Server", version="1.0.0")
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def root():
    return {"status": "ok", "service": "roi-compression"}

_model_lock = threading.Lock()
_models: Dict[str, Any] = {}

def _infer_model_type(weights: str) -> str:
    name = os.path.basename(str(weights or "")).lower()
    if name.startswith("rtdetr"):
        return "rtdetr"
    return "yolo"

def _resolve_weights_path(weights: str) -> (str, List[str]):
    if not weights:
        return DEFAULT_WEIGHTS, []
    w = str(weights).strip()
    if not w:
        return DEFAULT_WEIGHTS, []
    if os.path.isabs(w) and os.path.exists(w):
        return os.path.abspath(w), [os.path.abspath(w)]
    if os.path.exists(w):
        return os.path.abspath(w), [os.path.abspath(w)]
    search_dirs: List[str] = []
    if WEIGHTS_DIR:
        search_dirs.append(WEIGHTS_DIR)
    search_dirs.extend([
        os.getcwd(),
        os.path.dirname(__file__),
        os.path.abspath(os.path.dirname(__file__)),
        os.path.abspath(os.path.join(os.path.dirname(__file__), "..")),
        DATA_DIR,
        "/home/user/app",
        "/app",
        "/workspace",
        "/data",
    ])
    checked: List[str] = []
    for base in search_dirs:
        if not base:
            continue
        cand = os.path.join(base, w)
        checked.append(cand)
        if os.path.exists(cand):
            return os.path.abspath(cand), checked
    return w, checked

def get_model(weights: str) -> Any:
    key, checked = _resolve_weights_path(weights or DEFAULT_WEIGHTS)
    model_type = _infer_model_type(key)
    if str(key).endswith(".pt") and not os.path.exists(key):
        search_list = ", ".join(checked) if checked else "(no local paths searched)"
        raise RuntimeError(
            f"Weights not found locally: {weights}. Searched: {search_list}. "
            f"Set WEIGHTS_DIR or upload the weights to the app directory."
        )
    with _model_lock:
        cache_key = f"{model_type}:{key}"
        if cache_key not in _models:
            if model_type == "rtdetr":
                _models[cache_key] = RTDETR(key)
            else:
                _models[cache_key] = YOLO(key)
        return _models[cache_key]


def _parse_queries(q: str) -> List[str]:
    if not q:
        return []
    parts = [p.strip().lower() for p in q.replace("\n", ",").split(",")]
    return [p for p in parts if p]


def _keep_det(label: str, queries: List[str]) -> bool:
    if not queries:
        return True
    l = (label or "").strip().lower()
    if not l:
        return False
    return any((q == l) or (q in l) or (l in q) for q in queries)


def _yolo_detect_frame(
    model: Any,
    frame_bgr: np.ndarray,
    conf: float,
    queries: List[str],
    device: str,
    fast_mode: bool = False,
) -> List[Dict[str, Any]]:
    scale = 1.0
    if fast_mode:
        scale = max(0.1, min(1.0, float(FAST_DETECT_SCALE)))
    if scale < 1.0:
        h, w = frame_bgr.shape[:2]
        sw, sh = max(64, int(w * scale)), max(64, int(h * scale))
        small = cv2.resize(frame_bgr, (sw, sh), interpolation=cv2.INTER_AREA)
        img = cv2.cvtColor(small, cv2.COLOR_BGR2RGB)
    else:
        img = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)

    pred_kwargs = {"conf": conf, "verbose": False}
    if fast_mode:
        pred_kwargs["imgsz"] = FAST_DETECT_IMGSZ
    if device and str(device).lower() != "auto":
        pred_kwargs["device"] = device
        if fast_mode and str(device).lower() != "cpu":
            pred_kwargs["half"] = True
    try:
        res = model.predict(img, **pred_kwargs)
    except Exception as e:
        msg = str(e)
        if ("cuda" in msg.lower()) and (str(device).lower() != "cpu"):
            pred_kwargs["device"] = "cpu"
            res = model.predict(img, **pred_kwargs)
        else:
            raise
    if not res:
        return []
    r0 = res[0]
    names = getattr(r0, "names", None) or getattr(model, "names", None) or {}
    boxes = []
    if r0.boxes is None:
        return boxes
    for b in r0.boxes:
        try:
            xyxy = b.xyxy[0].cpu().numpy().tolist()
            if scale < 1.0:
                inv = 1.0 / scale
                xyxy = [v * inv for v in xyxy]
            score = float(b.conf[0].cpu().numpy())
            cls_i = int(b.cls[0].cpu().numpy())
            label = str(names.get(cls_i, cls_i))
            if not _keep_det(label, queries):
                continue
            boxes.append({"bbox_xyxy": xyxy, "label": label, "score": score})
        except Exception:
            continue
    return boxes


def _draw_boxes(frame_bgr: np.ndarray, dets: List[Dict[str, Any]]) -> np.ndarray:
    out = frame_bgr.copy()
    for d in dets:
        b = d.get("bbox_xyxy")
        if not (isinstance(b, (list, tuple)) and len(b) == 4):
            continue
        x1, y1, x2, y2 = [int(max(0, v)) for v in b]
        label = str(d.get("label", ""))
        score = d.get("score", None)
        tid = d.get("track_id", None)
        tag = f"#{tid}" if isinstance(tid, int) else ""
        txt = f"{label}{tag} {score:.2f}" if isinstance(score, (float, int)) else f"{label}{tag}"
        cv2.rectangle(out, (x1, y1), (x2, y2), (0, 255, 0), 2)
        if txt:
            cv2.putText(out, txt, (x1, max(12, y1 - 6)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1, cv2.LINE_AA)
    return out


def _iou_xyxy(a: List[float], b: List[float]) -> float:
    ax1, ay1, ax2, ay2 = a
    bx1, by1, bx2, by2 = b
    inter_x1 = max(ax1, bx1)
    inter_y1 = max(ay1, by1)
    inter_x2 = min(ax2, bx2)
    inter_y2 = min(ay2, by2)
    if inter_x2 <= inter_x1 or inter_y2 <= inter_y1:
        return 0.0
    inter = (inter_x2 - inter_x1) * (inter_y2 - inter_y1)
    area_a = max(0.0, (ax2 - ax1)) * max(0.0, (ay2 - ay1))
    area_b = max(0.0, (bx2 - bx1)) * max(0.0, (by2 - by1))
    denom = area_a + area_b - inter
    if denom <= 0:
        return 0.0
    return float(inter / denom)


def _assign_tracks(dets: List[Dict[str, Any]], tracker: Dict[str, Any], iou_thresh: float = 0.3) -> List[Dict[str, Any]]:
    prev = tracker.get("tracks", [])
    used_prev = set()
    out = []
    for d in dets:
        b = d.get("bbox_xyxy")
        label = str(d.get("label", ""))
        best_i = None
        best_iou = 0.0
        if isinstance(b, (list, tuple)) and len(b) == 4:
            for i, tr in enumerate(prev):
                if i in used_prev:
                    continue
                if label and tr.get("label") and tr.get("label") != label:
                    continue
                iou = _iou_xyxy(b, tr.get("bbox_xyxy", [0, 0, 0, 0]))
                if iou > best_iou:
                    best_iou = iou
                    best_i = i
        if best_i is not None and best_iou >= iou_thresh:
            d["track_id"] = int(prev[best_i].get("id"))
            used_prev.add(best_i)
        else:
            d["track_id"] = int(tracker.get("next_id", 1))
            tracker["next_id"] = int(d["track_id"]) + 1
        out.append(d)
    tracker["tracks"] = [
        {"id": int(d.get("track_id")), "bbox_xyxy": d.get("bbox_xyxy"), "label": d.get("label", "")}
        for d in out
    ]
    return out


def _ensure_even(v: int, min_v: int = 64) -> int:
    v = max(min_v, int(v))
    return v - (v % 2)


def _fit_aspect(w: int, h: int, target_w: int, target_h: int) -> Optional[List[int]]:
    if w <= 0 or h <= 0:
        return None
    if target_w and target_h:
        scale = min(float(target_w) / float(w), float(target_h) / float(h))
    elif target_w:
        scale = float(target_w) / float(w)
    elif target_h:
        scale = float(target_h) / float(h)
    else:
        return None
    if not math.isfinite(scale) or scale <= 0:
        return None
    return [int(w * scale), int(h * scale)]


def _compute_target_params(w: int, h: int, fps: float, bandwidth_kbps: int, target_fps: int, target_w: int, target_h: int, scale: float):
    fps = max(1.0, float(fps or 1.0))
    budget = max(100, int(bandwidth_kbps or 1500))
    base_kbps_720p30 = 2500.0
    base_kbps_orig = base_kbps_720p30 * (float(w) * float(h) * fps) / (1280.0 * 720.0 * 30.0)
    if not math.isfinite(base_kbps_orig) or base_kbps_orig <= 0:
        base_kbps_orig = base_kbps_720p30
    if target_w or target_h:
        fitted = _fit_aspect(w, h, int(target_w or 0), int(target_h or 0))
        if fitted:
            tw, th = fitted
        else:
            tw, th = int(target_w or w), int(target_h or h)
    else:
        scale = float(scale or 1.0)
        if scale < 0.1:
            scale = 0.1
        if scale > 1.0:
            scale = 1.0
        tw, th = int(w * scale), int(h * scale)
    tfps = int(target_fps or fps)
    scale_r = min(1.0, math.sqrt(budget / base_kbps_orig))
    tw = min(tw, int(w * scale_r))
    th = min(th, int(h * scale_r))
    tfps = min(int(fps), tfps)
    tw = _ensure_even(max(64, tw))
    th = _ensure_even(max(64, th))
    tfps = max(1, tfps)
    frame_step = max(1, int(round(fps / max(1, tfps))))
    return tw, th, tfps, frame_step


def _open_writer(path: str, w: int, h: int, fps: float) -> Optional[cv2.VideoWriter]:
    if w <= 0 or h <= 0:
        return None
    # for codec in ("avc1", "H264", "mp4v"):
    #     try:
    #         fourcc = cv2.VideoWriter_fourcc(*codec)
    #         wtmp = cv2.VideoWriter(path, fourcc, float(fps or 30.0), (int(w), int(h)))
    #         if wtmp is not None and wtmp.isOpened():
    #             return wtmp
    #     except Exception:
    #         continue

    # Force software-friendly codec to avoid hardware H.264 failures on some systems.
    try:
        fourcc = cv2.VideoWriter_fourcc(*"mp4v")
        wtmp = cv2.VideoWriter(path, fourcc, float(fps or 30.0), (int(w), int(h)))
        if wtmp is not None and wtmp.isOpened():
            return wtmp
    except Exception:
        pass
    return None


def _ffmpeg_available() -> bool:
    return shutil.which("ffmpeg") is not None


def _transcode_h264(src_path: str) -> Optional[str]:
    if not src_path or not os.path.exists(src_path):
        return None
    if not _ffmpeg_available():
        return None
    dst_path = os.path.splitext(src_path)[0] + "_h264.mp4"
    cmd = [
        "ffmpeg",
        "-y",
        "-i",
        src_path,
        "-c:v",
        "libx264",
        "-preset",
        "veryfast",
        "-pix_fmt",
        "yuv420p",
        dst_path,
    ]
    try:
        subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        if os.path.exists(dst_path) and os.path.getsize(dst_path) > 1024:
            return dst_path
    except Exception:
        return None
    return None


def _apply_roi_overlay(frame_bgr: np.ndarray, dets: List[Dict[str, Any]], target_w: int, target_h: int) -> np.ndarray:
    h, w = frame_bgr.shape[:2]
    bg_small = cv2.resize(frame_bgr, (int(target_w), int(target_h)), interpolation=cv2.INTER_AREA)
    bg = cv2.resize(bg_small, (int(w), int(h)), interpolation=cv2.INTER_LINEAR)
    out = bg.copy()
    pad = max(2, int(min(w, h) * 0.005))
    for d in dets:
        b = d.get("bbox_xyxy")
        if not (isinstance(b, (list, tuple)) and len(b) == 4):
            continue
        x1, y1, x2, y2 = [int(v) for v in b]
        x1 = max(0, x1 - pad)
        y1 = max(0, y1 - pad)
        x2 = min(w, x2 + pad)
        y2 = min(h, y2 + pad)
        if x2 <= x1 or y2 <= y1:
            continue
        out[y1:y2, x1:x2] = frame_bgr[y1:y2, x1:x2]
    return out


@dataclass
class Job:
    id: str
    video_path: str
    created: float = field(default_factory=time.time)
    status: str = "tracking"
    error: Optional[str] = None
    fps: float = 30.0
    w: int = 0
    h: int = 0
    frame_step: int = 1
    target_fps: int = 15
    target_width: int = 0
    target_height: int = 0
    bandwidth_kbps: int = 1500
    conf: float = DEFAULT_CONF
    weights: str = DEFAULT_WEIGHTS
    device: str = DEFAULT_DEVICE
    fast_mode: bool = False
    queries: List[str] = field(default_factory=list)
    overlay_video_path: Optional[str] = None
    compressed_video_path: Optional[str] = None
    roi_video_path: Optional[str] = None
    det_by_frame: Dict[int, List[Dict[str, Any]]] = field(default_factory=dict)
    latest_jpeg: Optional[bytes] = None
    latest_compressed_jpeg: Optional[bytes] = None
    latest_roi_jpeg: Optional[bytes] = None
    lock: threading.Lock = field(default_factory=threading.Lock)
    tracker_state: Dict[str, Any] = field(default_factory=lambda: {"next_id": 1, "tracks": []})


jobs: Dict[str, Job] = {}


def _process_job(job: Job):
    try:
        model = get_model(job.weights)
        cap = cv2.VideoCapture(job.video_path)
        if not cap.isOpened():
            raise RuntimeError("Could not open video.")
        fps = float(cap.get(cv2.CAP_PROP_FPS) or 30.0)
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)

        tw, th, tfps, frame_step = _compute_target_params(
            w=w,
            h=h,
            fps=fps,
            bandwidth_kbps=job.bandwidth_kbps,
            target_fps=job.target_fps,
            target_w=job.target_width,
            target_h=job.target_height,
            scale=max(0.25, min(1.0, (job.target_width / w) if (job.target_width and w) else 1.0)),
        )

        os.makedirs(OUTPUT_DIR, exist_ok=True)
        overlay_path = os.path.join(OUTPUT_DIR, f"{job.id}_overlay.mp4")
        overlay_writer = _open_writer(overlay_path, w, h, fps)

        with job.lock:
            job.fps = fps
            job.w = w
            job.h = h
            job.frame_step = frame_step
            job.target_fps = tfps
            job.target_width = tw
            job.target_height = th
            job.overlay_video_path = overlay_path if overlay_writer is not None else None
            job.status = "tracking"

        frame_idx = 0
        tracker = job.tracker_state
        last_dets: List[Dict[str, Any]] = []
        while True:
            ok, frame = cap.read()
            if not ok:
                break
            if frame_idx % frame_step == 0:
                dets = _yolo_detect_frame(model, frame, conf=job.conf, queries=job.queries, device=job.device, fast_mode=job.fast_mode)
                if dets and not any("track_id" in d for d in dets):
                    dets = _assign_tracks(dets, tracker)
                elif dets:
                    tracker["tracks"] = [
                        {"id": int(d.get("track_id")), "bbox_xyxy": d.get("bbox_xyxy"), "label": d.get("label", "")}
                        for d in dets
                    ]
                    max_id = max((int(d.get("track_id", 0)) for d in dets), default=0)
                    tracker["next_id"] = max(tracker.get("next_id", 1), max_id + 1)
                with job.lock:
                    job.det_by_frame[int(frame_idx)] = dets
                last_dets = dets
            else:
                dets = last_dets

            overlay = _draw_boxes(frame, dets or [])
            ok2, jpg = cv2.imencode(".jpg", overlay, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
            if ok2:
                with job.lock:
                    job.latest_jpeg = jpg.tobytes()

            if overlay_writer is not None:
                overlay_writer.write(overlay)

            frame_idx += 1

        cap.release()
        if overlay_writer is not None:
            try:
                overlay_writer.release()
            except Exception:
                pass

        h264_overlay = _transcode_h264(overlay_path) if overlay_writer is not None else None
        with job.lock:
            if h264_overlay:
                job.overlay_video_path = h264_overlay
            job.status = "tracked"
    except Exception as e:
        with job.lock:
            job.status = "error"
            job.error = str(e)


def _compress_job(job: Job, bandwidth_kbps: int, target_fps: int, target_w: int, target_h: int, resolution_scale: float):
    try:
        cap = cv2.VideoCapture(job.video_path)
        if not cap.isOpened():
            raise RuntimeError("Could not open video.")
        fps = float(cap.get(cv2.CAP_PROP_FPS) or 30.0)
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)

        tw, th, tfps, frame_step = _compute_target_params(
            w=w,
            h=h,
            fps=fps,
            bandwidth_kbps=bandwidth_kbps,
            target_fps=target_fps,
            target_w=target_w,
            target_h=target_h,
            scale=resolution_scale,
        )

        os.makedirs(OUTPUT_DIR, exist_ok=True)
        compressed_path = os.path.join(OUTPUT_DIR, f"{job.id}_compressed_rt.mp4")
        roi_path = os.path.join(OUTPUT_DIR, f"{job.id}_roi_rt.mp4")

        compressed_writer = _open_writer(compressed_path, tw, th, tfps)
        roi_writer = _open_writer(roi_path, w, h, tfps)

        with job.lock:
            job.status = "compressing"
            job.bandwidth_kbps = int(bandwidth_kbps)
            job.target_fps = int(tfps)
            job.target_width = int(tw)
            job.target_height = int(th)

        frame_idx = 0
        last_dets: List[Dict[str, Any]] = []
        while True:
            ok, frame = cap.read()
            if not ok:
                break
            if frame_idx % frame_step != 0:
                frame_idx += 1
                continue

            dets = job.det_by_frame.get(int(frame_idx))
            if dets is None:
                dets = last_dets
            else:
                last_dets = dets

            compressed_frame = None
            roi_frame = None

            if compressed_writer is not None:
                try:
                    compressed_frame = cv2.resize(frame, (tw, th), interpolation=cv2.INTER_AREA)
                    compressed_writer.write(compressed_frame)
                except Exception:
                    compressed_frame = None

            if roi_writer is not None:
                try:
                    roi_frame = _apply_roi_overlay(frame, dets, tw, th)
                    roi_writer.write(roi_frame)
                except Exception:
                    roi_frame = None

            try:
                if compressed_frame is not None:
                    okc, jpgc = cv2.imencode(".jpg", compressed_frame, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
                    if okc:
                        with job.lock:
                            job.latest_compressed_jpeg = jpgc.tobytes()
                if roi_frame is not None:
                    okr, jpgr = cv2.imencode(".jpg", roi_frame, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
                    if okr:
                        with job.lock:
                            job.latest_roi_jpeg = jpgr.tobytes()
            except Exception:
                pass

            frame_idx += 1

        cap.release()
        for wtr in (compressed_writer, roi_writer):
            if wtr is not None:
                try:
                    wtr.release()
                except Exception:
                    pass

        h264_compressed = _transcode_h264(compressed_path) if compressed_writer is not None else None
        h264_roi = _transcode_h264(roi_path) if roi_writer is not None else None

        with job.lock:
            if h264_compressed:
                job.compressed_video_path = h264_compressed
            else:
                job.compressed_video_path = compressed_path if os.path.exists(compressed_path) else job.compressed_video_path
            if h264_roi:
                job.roi_video_path = h264_roi
            else:
                job.roi_video_path = roi_path if os.path.exists(roi_path) else job.roi_video_path
            job.status = "completed"
    except Exception as e:
        with job.lock:
            job.status = "error"
            job.error = str(e)


@app.post("/track/async")
async def track_async(
    video: UploadFile = File(...),
    queries: str = Form(""),
    conf: float = Form(DEFAULT_CONF),
    weights: str = Form(DEFAULT_WEIGHTS),
    device: str = Form(""),
    fast_mode: bool = Form(False),
    bandwidth_kbps: int = Form(1500),
    target_fps: int = Form(15),
    target_width: int = Form(0),
    target_height: int = Form(0),
    resolution_scale: float = Form(1.0),
):
    job_id = uuid.uuid4().hex[:12]
    os.makedirs(UPLOAD_DIR, exist_ok=True)
    dst = os.path.join(UPLOAD_DIR, f"{job_id}_{os.path.basename(video.filename or 'input.mp4')}")
    data = await video.read()
    with open(dst, "wb") as f:
        f.write(data)

    job = Job(
        id=job_id,
        video_path=dst,
        status="tracking",
        conf=float(conf),
        weights=str(weights),
        device=str(device).strip() or DEFAULT_DEVICE,
        queries=_parse_queries(queries),
        fast_mode=bool(fast_mode),
        target_fps=int(target_fps or 15),
        bandwidth_kbps=int(bandwidth_kbps or 1500),
        target_width=int(target_width or 0),
        target_height=int(target_height or 0),
    )
    jobs[job_id] = job

    # fast preview for MJPEG
    try:
        cap = cv2.VideoCapture(dst)
        ok, frame0 = cap.read()
        cap.release()
        if ok and frame0 is not None:
            model = get_model(job.weights)
            det0 = _yolo_detect_frame(model, frame0, conf=job.conf, queries=job.queries, device=job.device, fast_mode=job.fast_mode)
            det0 = _assign_tracks(det0, job.tracker_state)
            with job.lock:
                job.det_by_frame[0] = det0
            vis0 = _draw_boxes(frame0, det0)
            ok2, jpg = cv2.imencode(".jpg", vis0, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
            if ok2:
                with job.lock:
                    job.latest_jpeg = jpg.tobytes()
    except Exception:
        pass

    t = threading.Thread(target=_process_job, args=(job,), daemon=True)
    t.start()

    return JSONResponse({
        "job_id": job_id,
        "status_url": f"/process/status/{job_id}",
        "stream_url": f"/detect/stream/{job_id}",
        "overlay_video_url": f"/process/video/overlay/{job_id}",
        "compressed_video_url": f"/process/video/compressed/{job_id}",
        "roi_video_url": f"/process/video/roi/{job_id}",
    })


@app.post("/process/compress/{job_id}")
async def process_compress(
    job_id: str,
    bandwidth_kbps: int = Form(1500),
    target_fps: int = Form(15),
    target_width: int = Form(0),
    target_height: int = Form(0),
    resolution_scale: float = Form(1.0),
):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    with job.lock:
        if job.status in ("tracking", "compressing"):
            raise HTTPException(status_code=409, detail="Job still running")
        if job.status not in ("tracked", "completed"):
            raise HTTPException(status_code=409, detail="Tracking not ready")

    t = threading.Thread(
        target=_compress_job,
        args=(job, int(bandwidth_kbps), int(target_fps), int(target_width), int(target_height), float(resolution_scale)),
        daemon=True,
    )
    t.start()
    return JSONResponse({"job_id": job_id, "status": "compressing"})


@app.get("/process/status/{job_id}")
def process_status(job_id: str):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    with job.lock:
        return {
            "job_id": job.id,
            "status": job.status,
            "error": job.error,
            "target_width": job.target_width,
            "target_height": job.target_height,
            "target_fps": job.target_fps,
            "bandwidth_kbps": job.bandwidth_kbps,
        }


def _mjpeg_generator(job: Job):
    boundary = b"--frame"
    while True:
        with job.lock:
            jpg = job.latest_jpeg
            status = job.status
            err = job.error
        if err:
            break
        if jpg:
            yield boundary + b"\r\n"
            yield b"Content-Type: image/jpeg\r\n"
            yield f"Content-Length: {len(jpg)}\r\n\r\n".encode("ascii")
            yield jpg + b"\r\n"
        time.sleep(0.15)
        if status in ("completed", "error"):
            time.sleep(0.5)
            break


def _mjpeg_generator_compressed(job: Job):
    boundary = b"--frame"
    while True:
        with job.lock:
            jpg = job.latest_compressed_jpeg
            status = job.status
            err = job.error
        if err:
            break
        if jpg:
            yield boundary + b"\r\n"
            yield b"Content-Type: image/jpeg\r\n"
            yield f"Content-Length: {len(jpg)}\r\n\r\n".encode("ascii")
            yield jpg + b"\r\n"
        time.sleep(0.15)
        if status in ("completed", "error"):
            time.sleep(0.5)
            break


def _mjpeg_generator_roi(job: Job):
    boundary = b"--frame"
    while True:
        with job.lock:
            jpg = job.latest_roi_jpeg
            status = job.status
            err = job.error
        if err:
            break
        if jpg:
            yield boundary + b"\r\n"
            yield b"Content-Type: image/jpeg\r\n"
            yield f"Content-Length: {len(jpg)}\r\n\r\n".encode("ascii")
            yield jpg + b"\r\n"
        time.sleep(0.15)
        if status in ("completed", "error"):
            time.sleep(0.5)
            break


@app.get("/detect/stream/{job_id}")
def detect_stream(job_id: str):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    return StreamingResponse(_mjpeg_generator(job), media_type="multipart/x-mixed-replace; boundary=frame")


@app.get("/process/stream/compressed/{job_id}")
def process_stream_compressed(job_id: str):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    return StreamingResponse(_mjpeg_generator_compressed(job), media_type="multipart/x-mixed-replace; boundary=frame")


@app.get("/process/stream/roi/{job_id}")
def process_stream_roi(job_id: str):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    return StreamingResponse(_mjpeg_generator_roi(job), media_type="multipart/x-mixed-replace; boundary=frame")


@app.get("/process/video/overlay/{job_id}")
def process_video_overlay(job_id: str):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    path = job.overlay_video_path if job.overlay_video_path and os.path.exists(job.overlay_video_path) and os.path.getsize(job.overlay_video_path) > 1024 else job.video_path
    return FileResponse(path, media_type="video/mp4")


@app.get("/process/video/compressed/{job_id}")
def process_video_compressed(job_id: str):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    path = job.compressed_video_path if job.compressed_video_path and os.path.exists(job.compressed_video_path) and os.path.getsize(job.compressed_video_path) > 1024 else job.video_path
    return FileResponse(path, media_type="video/mp4")


@app.get("/process/video/roi/{job_id}")
def process_video_roi(job_id: str):
    job = jobs.get(job_id)
    if not job:
        raise HTTPException(status_code=404, detail="Unknown job_id")
    path = job.roi_video_path if job.roi_video_path and os.path.exists(job.roi_video_path) and os.path.getsize(job.roi_video_path) > 1024 else job.video_path
    return FileResponse(path, media_type="video/mp4")


if __name__ == "__main__":
    import argparse
    import uvicorn

    p = argparse.ArgumentParser()
    p.add_argument("--host", default="127.0.0.1")
    p.add_argument("--port", default=8000, type=int)
    p.add_argument("--weights", default=DEFAULT_WEIGHTS)
    p.add_argument("--device", default=DEFAULT_DEVICE)
    args = p.parse_args()

    DEFAULT_WEIGHTS = args.weights
    DEFAULT_DEVICE = args.device
    get_model(args.weights)

    host = os.environ.get("HOST", args.host or "0.0.0.0")
    port = int(os.environ.get("PORT", args.port))
    uvicorn.run(app, host=host, port=port)