Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save alielbashir/a059aa8999aba2489cee11bdec43e426 to your computer and use it in GitHub Desktop.

Select an option

Save alielbashir/a059aa8999aba2489cee11bdec43e426 to your computer and use it in GitHub Desktop.
Return random image as json frame
from fastapi import FastAPI
from uvicorn import run
import cv2
import numpy as np
import base64
app = FastAPI()
@app.get("/api/video_frame")
async def video_frame():
buffer = cv2.imencode('.jpg', _get_frame())[1]
frame_as_text = base64.b64encode(buffer)
return {"frame": frame_as_text}
def _get_frame():
frame = np.random.randint(low=0, high=255, size=(480, 640, 3), dtype='uint8')
return frame
if __name__ == '__main__':
run(app, host="127.0.0.1", port=8001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment