YOLOv8 frames to FASTAPI Streaming Response #9803
Answered
by
gustavosett
gustavosett
asked this question in
Questions
-
First Check
Commit to Help
Example Codefrom fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
import cv2
import numpy as np
from app.core.database import get_db
from app.manager import FireWatchCameraManager
from app.services.utils import LOGGER
router = APIRouter(
prefix="/api",
tags=["stream"]
)
# Instanciando o gerenciador de câmera. Esse objeto será responsável por gerenciar múltiplas câmeras
CAMERA_MANAGER = FireWatchCameraManager()
# Definindo o cabeçalho de imagem HTTP que será usado para cada frame do vídeo
IMAGE_HEADER = (b"--frame\r\n"
b"Content-Type: image/jpeg\r\n\r\n")
async def frame_stream(index):
while CAMERA_MANAGER[index].is_running:
frame = CAMERA_MANAGER[index].get_frame()
_, buffer = cv2.imencode(".jpg", frame)
yield IMAGE_HEADER + buffer.tobytes() + b"\r\n"
# Rota de streaming
@router.get("/video_feed/{index}", responses={200: {"content": {"multipart/x-mixed-replace": {}}}})
async def video_feed(index: int):
camera_quant = len(CAMERA_MANAGER)
if index < 0 or index >= camera_quant or camera_quant == 0:
raise HTTPException(status_code=404, detail="Camera not found.")
return StreamingResponse(frame_stream(index), media_type="multipart/x-mixed-replace")DescriptionGood morning, I would like to know why my stream route isn't working. It's a camera system with yolov8, but I'm testing it only with my webcam. If I use cv2.imshow to show me the frames, I receive them correctly, but this route doesn't work. No error appears, it runs the loop once or twice and then stops, even though my camera is available to send the frames. Operating SystemWindows Operating System DetailsNo response FastAPI Version0.99.1 Python VersionPython 3.11.3 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
gustavosett
Jul 4, 2023
Replies: 1 comment
-
|
it's working now, just missing the "boundary=frame" async def frame_stream(index):
while CAMERA_MANAGER[index].is_running:
frame = CAMERA_MANAGER[index].get_frame()
_, buffer = cv2.imencode(".jpg", frame)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
# Rota de streaming
@router.get("/video_feed/{index}", responses={200: {"content": {"multipart/x-mixed-replace;boundary=frame": {}}}})
async def video_feed(index: int):
camera_quant = len(CAMERA_MANAGER)
if index < 0 or index >= camera_quant or camera_quant == 0:
raise HTTPException(status_code=404, detail="Camera not found.")
return StreamingResponse(frame_stream(index), media_type="multipart/x-mixed-replace;boundary=frame") |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
gustavosett
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's working now, just missing the "boundary=frame"