File size: 1,372 Bytes
b0e946c
c8120da
306ee04
9503367
c8120da
 
 
 
080458f
109ac73
c8120da
 
306ee04
 
 
c8120da
 
306ee04
c8120da
9503367
 
c8120da
 
 
 
 
 
 
 
4ac5c78
c8120da
 
752e4f7
 
 
 
c8120da
 
534fffb
 
752e4f7
534fffb
 
752e4f7
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
import cv2
import gradio as gr
import spaces
from fastrtc import Stream, get_twilio_turn_credentials
from gradio.utils import get_space

try:
    from demo.object_detection.inference import YOLOv10
except (ImportError, ModuleNotFoundError):
    from inference import YOLOv10


# Load the model and place it on CUDA at module level. On ZeroGPU this uses the
# PyTorch CUDA emulation; the real GPU is attached inside @spaces.GPU below.
model = YOLOv10("yolov10n.pt").to("cuda")


@spaces.GPU
def detection(image, conf_threshold=0.3):
    new_image = model.detect_objects(image, conf_threshold)
    return cv2.resize(new_image, (500, 500))


stream = Stream(
    handler=detection,
    modality="video",
    mode="send-receive",
    additional_inputs=[gr.Slider(minimum=0, maximum=1, step=0.01, value=0.3)],
    rtc_configuration=get_twilio_turn_credentials() if get_space() else None,
    concurrency_limit=2 if get_space() else None,
)

# ZeroGPU only detects @spaces.GPU functions when Gradio is the launched app,
# so we expose and launch the built-in fastrtc UI (a Gradio Blocks) rather than
# mounting the stream on a FastAPI/uvicorn app.
demo = stream.ui

if __name__ == "__main__":
    import os

    if (mode := os.getenv("MODE")) == "PHONE":
        stream.fastphone(host="0.0.0.0", port=7860)
    else:
        demo.launch(server_name="0.0.0.0", server_port=7860)