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.
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:
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...
... or to use any kind of application to convert the frames to base64 and send it to the web server:
How it works
The frames sent throught the web server are stored in a temporary (in memory) SQLite DB...
... and the last frame of each stream is retrieved everytime a client wants to visualize the stream.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi_frame_stream-0.1.0.tar.gz
.
File metadata
- Download URL: fastapi_frame_stream-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99aeaa4e79c5eac9d166035e8239669a9fc1e4b24273dd276a109ed0ec75ed07 |
|
MD5 | 6ef7ffe26be991f907940698b09f14f2 |
|
BLAKE2b-256 | 41876ba18314a8c97ea3a648e15fb5e3558d1d6d207df0a30cc330012451b3e0 |
File details
Details for the file fastapi_frame_stream-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_frame_stream-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fe9de479c81dd2eb4528f89b09bbf16640c590d8f1ccc67fd8407046b73b723 |
|
MD5 | 56c03bca27505c133ff53c08cfee8996 |
|
BLAKE2b-256 | aef866836f6c5f3634dc848f7d5e19f819fd62e8e028e9cc50c04f08ecef77b9 |