File size: 6,153 Bytes
8dd63ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
RT-DETR Object Detection — streaming variant (Plan B).

Use this ONLY if you specifically need progressive playback while processing.
It is inherently more fragile than app.py (single-file). Differences from upstream:

  * Every yielded segment is re-encoded to real H.264 with ffmpeg. Gradio's
    streaming-outputs guide requires .mp4 or h.264-in-.ts for video chunks.
  * Segment length is time-based and >= 1 s (Gradio requires this for smooth
    playback). Upstream hardcoded exactly 2*desired_fps frames, so any video
    whose frame count was not an exact multiple of that lost its tail.
  * The trailing partial segment is flushed instead of discarded.
  * Even dimensions enforced; frames resized to the writer's exact size.
"""

import os
import time
import uuid
import shutil
import subprocess

import cv2
import numpy as np
import torch
import gradio as gr
import spaces
from PIL import Image
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor

from draw_boxes import draw_bounding_boxes

BUILD_TAG = "PLANB-H264-STREAMING-v1"
print("=" * 70, flush=True)
print(f"[BOOT] RT-DETR build tag: {BUILD_TAG}", flush=True)
print(f"[BOOT] ffmpeg on PATH: {shutil.which('ffmpeg')}", flush=True)
print("=" * 70, flush=True)

MODEL_ID = "PekingU/rtdetr_r50vd"
image_processor = RTDetrImageProcessor.from_pretrained(MODEL_ID)
model = RTDetrForObjectDetection.from_pretrained(MODEL_ID).to("cuda")

SUBSAMPLE = 2
SEGMENT_SECONDS = 1.5          # must stay >= 1.0
WORK_DIR = os.path.join(os.getcwd(), "rtdetr_stream")
os.makedirs(WORK_DIR, exist_ok=True)


def encode_h264(src_path: str) -> str:
    dst_path = src_path.replace(".mp4", "_h264.mp4")
    cmd = [
        "ffmpeg", "-y", "-i", src_path,
        "-c:v", "libx264", "-profile:v", "baseline", "-level", "3.1",
        "-preset", "veryfast", "-crf", "23",
        "-pix_fmt", "yuv420p", "-movflags", "+faststart", "-an",
        dst_path,
    ]
    proc = subprocess.run(cmd, capture_output=True, text=True)
    if proc.returncode != 0 or not os.path.exists(dst_path):
        print("[H264] FAILED:", proc.stderr[-800:], flush=True)
        raise gr.Error("H.264 re-encode failed — see container logs.")
    os.remove(src_path)
    print(f"[H264] OK -> {dst_path} ({os.path.getsize(dst_path)} bytes)", flush=True)
    return dst_path


@spaces.GPU(duration=180)
def stream_object_detection(video, conf_threshold):
    print(f"\n[RUN] {BUILD_TAG} | conf={conf_threshold}", flush=True)
    cap = cv2.VideoCapture(video)
    if not cap.isOpened():
        raise gr.Error("Could not open the uploaded video.")

    src_fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
    out_fps = max(1.0, src_fps / SUBSAMPLE)
    width = max(2, ((int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) // 2) // 2) * 2)
    height = max(2, ((int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) // 2) // 2) * 2)
    per_segment = max(1, int(round(out_fps * SEGMENT_SECONDS)))
    print(f"[RUN] out {width}x{height} @ {out_fps:.2f}fps | {per_segment} frames/segment",
          flush=True)

    def new_writer():
        p = os.path.join(WORK_DIR, f"seg_{uuid.uuid4().hex}.mp4")
        return cv2.VideoWriter(p, cv2.VideoWriter_fourcc(*"mp4v"), out_fps, (width, height)), p

    writer, path = new_writer()
    batch, n_read, seg_no = [], 0, 0

    def flush(frames, wr, p):
        if not frames:
            wr.release()
            if os.path.exists(p):
                os.remove(p)
            return None
        inputs = image_processor(images=frames, return_tensors="pt").to("cuda")
        t0 = time.time()
        with torch.no_grad():
            outputs = model(**inputs)
        boxes = image_processor.post_process_object_detection(
            outputs,
            target_sizes=torch.tensor([(height, width)] * len(frames)),
            threshold=conf_threshold,
        )
        n_det = 0
        for array, box in zip(frames, boxes):
            n_det += int(box["scores"].shape[0])
            pil_image = draw_bounding_boxes(Image.fromarray(array), box, model, conf_threshold)
            wr.write(np.array(pil_image)[:, :, ::-1].copy())
        wr.release()
        print(f"[SEG] frames={len(frames)} inference={time.time() - t0:.3f}s dets={n_det}",
              flush=True)
        return encode_h264(p)

    try:
        while True:
            ok, frame = cap.read()
            if not ok:
                break
            if n_read % SUBSAMPLE == 0:
                frame = cv2.resize(frame, (width, height))
                batch.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
                if len(batch) >= per_segment:
                    out = flush(batch, writer, path)
                    if out:
                        seg_no += 1
                        print(f"[YIELD] #{seg_no} {out}", flush=True)
                        yield out
                    batch = []
                    writer, path = new_writer()
            n_read += 1

        # Trailing frames — upstream dropped these.
        out = flush(batch, writer, path)
        if out:
            seg_no += 1
            print(f"[YIELD] #{seg_no} (tail) {out}", flush=True)
            yield out
    finally:
        cap.release()

    print(f"[RUN] done. read={n_read} segments={seg_no}", flush=True)
    if seg_no == 0:
        raise gr.Error("No segments produced — the video decoded to zero frames.")


with gr.Blocks(title="RT-DETR Streaming") as app:
    gr.HTML(
        "<h1 style='text-align:center;margin-bottom:0'>RT-DETR Object Detection (streaming)</h1>"
        f"<p style='text-align:center;color:#888;font-size:0.85em'>build {BUILD_TAG}</p>"
    )
    with gr.Row():
        with gr.Column():
            video = gr.Video(label="Video Source")
            conf_threshold = gr.Slider(
                label="Confidence Threshold",
                minimum=0.0, maximum=1.0, step=0.01, value=0.30,
            )
        with gr.Column():
            output_video = gr.Video(label="Processed Video", streaming=True, autoplay=True)

    video.upload(
        fn=stream_object_detection,
        inputs=[video, conf_threshold],
        outputs=[output_video],
    )

if __name__ == "__main__":
    app.launch()