File size: 899 Bytes
864dc88
9239fc4
864dc88
9239fc4
864dc88
 
 
 
 
 
 
 
 
b4b3ab0
864dc88
9239fc4
 
864dc88
 
 
 
 
 
 
 
 
 
9239fc4
 
864dc88
9239fc4
864dc88
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
from fastapi import FastAPI, WebSocket
import cv2
from transformers import pipeline

app = FastAPI()

# Load the model from Hugging Face
model = pipeline("object-detection", model="facebook/detr-resnet-50")

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    cap = cv2.VideoCapture(0)  # Open the first webcam device
    
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        # Convert the frame to a format suitable for the model
        rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        
        # Use the model to make predictions
        predictions = model(rgb_frame)
        
        # Send the predictions back to the client
        await websocket.send_json(predictions)

    cap.release()
    await websocket.close()

# To run the app, use: uvicorn app:app --reload