The documentation I've found for using Flask has been fairly straight forward. For Example: The following code accepts parameters and implements a function to call and operate on the parameters.
from flask import request
@app.route('/hello')
def api_hello():
if 'name' in request.args:
return 'Hello ' + request.args['name']
else:
return 'Hello John Doe'
I'm looking for an approach that will allow an endpoint like @app.route('/getData')
to return data from another python script (or set of functions) that is executing.
For instance - I have a python script that captures frames from a video feed and processes them.
import time
import picamera
import numpy as np
import cv2
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.framerate = 24
time.sleep(2)
image = np.empty((240 * 320 * 3,), dtype=np.uint8)
camera.capture(image, 'bgr')
image = image.reshape((240, 320, 3))
I would like to be able to have Python/Flask be able to interact with this stream of data generated by another process. So someone hitting the the Flask API /getData
would receive a JSON response that contains information from the script processing the camera feed.
What options exist to achieve this within Python and Flask? Off the top of my head I can think of writing the JSON to a file and simply have the Flask API read that data, but that seems messy and I would think there are better ways to use Flask to run the video capture script in the background with an API endpoint that can query the current state and return that data whenever requested.
Any suggestions would be appreciated.
Thanks in advance