Docker_APP / app.py
Raumkommander's picture
Update app.py
b6ff8ba verified
raw
history blame
1.15 kB
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from starlette.requests import Request
import cv2
app = FastAPI()
# Serve static files (for HTML & JS)
app.mount("/static", StaticFiles(directory="static"), name="static")
# Jinja2 template rendering
templates = Jinja2Templates(directory="templates")
# Open webcam (or use a file)
video_source = 0 # Change to "video.mp4" for a file
cap = cv2.VideoCapture(video_source)
def generate_frames():
"""Stream video frames as MJPEG."""
while True:
success, frame = cap.read()
if not success:
break
_, buffer = cv2.imencode(".jpg", frame)
yield (b"--frame\r\n"
b"Content-Type: image/jpeg\r\n\r\n" +
buffer.tobytes() + b"\r\n")
@app.get("/")
async def homepage(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/video")
async def video_feed():
return StreamingResponse(generate_frames(), media_type="multipart/x-mixed-replace; boundary=frame")