Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import StreamingResponse
|
3 |
+
import cv2
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Open webcam (or use a video file)
|
8 |
+
video_source = 0 # Change to "video.mp4" for a file
|
9 |
+
cap = cv2.VideoCapture(video_source)
|
10 |
+
|
11 |
+
def generate_frames():
|
12 |
+
while True:
|
13 |
+
success, frame = cap.read()
|
14 |
+
if not success:
|
15 |
+
break
|
16 |
+
_, buffer = cv2.imencode(".jpg", frame)
|
17 |
+
yield (b"--frame\r\n"
|
18 |
+
b"Content-Type: image/jpeg\r\n\r\n" +
|
19 |
+
buffer.tobytes() + b"\r\n")
|
20 |
+
|
21 |
+
@app.get("/video")
|
22 |
+
async def video_feed():
|
23 |
+
return StreamingResponse(generate_frames(), media_type="multipart/x-mixed-replace; boundary=frame")
|
24 |
+
|
25 |
+
@app.get("/")
|
26 |
+
def index():
|
27 |
+
return {"message": "Video Streaming with FastAPI"}
|