File size: 1,661 Bytes
018cf2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from pathlib import Path
import cv2
import gradio as gr
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from huggingface_hub import hf_hub_download
from pydantic import BaseModel, Field
from livekit_token import generate_token

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

cur_dir = Path(__file__).parent

model_file = hf_hub_download(repo_id="onnx-community/yolov10n", filename="onnx/model.onnx")
model = YOLOv10(model_file)

def detection(image, conf_threshold=0.3):
    image = cv2.resize(image, (model.input_width, model.input_height))
    new_image = model.detect_objects(image, conf_threshold)
    return cv2.resize(new_image, (500, 500))

app = FastAPI()

LIVEKIT_URL = os.getenv("LIVEKIT_URL")
LIVEKIT_API_KEY = os.getenv("LIVEKIT_API_KEY")
LIVEKIT_API_SECRET = os.getenv("LIVEKIT_API_SECRET")

@app.get("/")
async def _():
    token = generate_token(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, identity="user123")
    html_content = open(cur_dir / "index.html").read()
    html_content = html_content.replace("__LIVEKIT_URL__", LIVEKIT_URL)
    html_content = html_content.replace("__LIVEKIT_TOKEN__", f'"{token}"')
    return HTMLResponse(content=html_content)

class InputData(BaseModel):
    identity: str
    conf_threshold: float = Field(ge=0, le=1)

@app.post("/input_hook")
async def _(data: InputData):
    print(f"Received input for {data.identity} with threshold {data.conf_threshold}")
    return {"status": "ok"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)