Skip to main content

Package to easily stream individual frames (MJPEG) using FastAPI

Project description

fastapi-frame-stream

Package to easily stream individual frames (MJPEG) using FastAPI.

FastAPI server for publishing and viewing MJPEG streams.

Raw image files and images as base64 strings can be sent to a 'video stream' and then consumed by any client.

Quick start

Installing

pip install fastapi-frame-stream

Requirements

NOTE: This package will also automatically install:
  • imutils
  • opencv-python
  • python-miltipart

How to use

Server

You can create a simple FastAPI server where it is possible to publish and get multiple streams.

usage code

full code
from fastapi import FastAPI, File, UploadFile
import uvicorn
from pydantic import BaseModel
from fastapi_frame_stream import FrameStreamer

app = FastAPI()
fs = FrameStreamer()

class InputImg(BaseModel):
    img_base64str : str


@app.post("/send_frame_from_string/{stream_id}")
async def send_frame_from_string(stream_id: str, d:InputImg):
    await fs.send_frame(stream_id, d.img_base64str)


@app.post("/send_frame_from_file/{stream_id}")
async def send_frame_from_file(stream_id: str, file: UploadFile = File(...)):
    await fs.send_frame(stream_id, file)


@app.get("/video_feed/{stream_id}")
async def video_feed(stream_id: str):
    return fs.get_stream(stream_id)


if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=5000)

Client

Any client can view a published image (MJPEG) stream using a simple <img> tag:

usage code

full code
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Testing fastapi-frame-stream</title>
</head>
<body>
    <img src="http://localhost:5000/video_feed/my_new_stream001">
</body>
</html>

All together

It is possible to upload an image file directly...

Server and client

... or to use any kind of application to convert the frames to base64 and send it to the web server:

Server and client

How it works

The frames sent throught the web server are stored in a temporary (in memory) SQLite DB...

User sending frame

... and the last frame of each stream is retrieved everytime a client wants to visualize the stream.

Retrieving stream

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_frame_stream-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

fastapi_frame_stream-0.1.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_frame_stream-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_frame_stream-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for fastapi_frame_stream-0.1.1.tar.gz
Algorithm Hash digest
SHA256 05aaae2405cce97b135948e50ce3ec77fd9afce8b6c5b9cdb26bc83e46f6045e
MD5 d6e30ec7883bc85d8423808a1cea579b
BLAKE2b-256 4736b0e9ef4c0cf2f955e08c29c4d4593177817febda0e1de998bc9c4fd35211

See more details on using hashes here.

File details

Details for the file fastapi_frame_stream-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_frame_stream-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98fcc207b8b4c360519dc8d17419f19624d345709c36ca0c501b79c8e179b053
MD5 e878e7b1064a8090831e268b06fccbd1
BLAKE2b-256 1157cada76bf1a47a0481a9b8baa4cc0aa3efd47e7530c140e0efd30a4518a45

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page