Piarasingh85 commited on
Commit
864dc88
·
verified ·
1 Parent(s): 2bae497

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -1,31 +1,32 @@
1
- import gradio as gr
2
  import cv2
 
3
 
4
- def video_feed():
5
- cap = cv2.VideoCapture(0) # Use 0 for the default camera
6
- if not cap.isOpened():
7
- raise RuntimeError("Could not start camera.")
 
 
 
 
 
8
 
9
- while True:
10
  ret, frame = cap.read()
11
  if not ret:
12
- continue
13
- # Convert the frame to RGB
14
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
15
- # Encode the frame as JPEG
16
- _, buffer = cv2.imencode('.jpg', frame)
17
- # Yield the frame as bytes
18
- yield buffer.tobytes()
 
 
 
19
 
20
  cap.release()
 
21
 
22
- iface = gr.Interface(
23
- fn=video_feed,
24
- inputs=[],
25
- outputs=gr.Video(label="Live Webcam Feed"),
26
- live=True,
27
- description="A simple live webcam feed using Gradio."
28
- )
29
-
30
- if __name__ == "__main__":
31
- iface.launch()
 
1
+ from fastapi import FastAPI, WebSocket
2
  import cv2
3
+ from transformers import pipeline
4
 
5
+ app = FastAPI()
6
+
7
+ # Load the model from Hugging Face
8
+ model = pipeline("object-detection", model="facebook/detr-resnet-50")
9
+
10
+ @app.websocket("/ws")
11
+ async def websocket_endpoint(websocket: WebSocket):
12
+ await websocket.accept()
13
+ cap = cv2.VideoCapture(0) # Open the first webcam device
14
 
15
+ while cap.isOpened():
16
  ret, frame = cap.read()
17
  if not ret:
18
+ break
19
+
20
+ # Convert the frame to a format suitable for the model
21
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
22
+
23
+ # Use the model to make predictions
24
+ predictions = model(rgb_frame)
25
+
26
+ # Send the predictions back to the client
27
+ await websocket.send_json(predictions)
28
 
29
  cap.release()
30
+ await websocket.close()
31
 
32
+ # To run the app, use: uvicorn app:app --reload