Python Forum
Return Frame as well as JSON response using same API in Flask Python Ask
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return Frame as well as JSON response using same API in Flask Python Ask
#1
Using mentioned below code I am trying to return video feed as well as some JSON data in response which will help me further in displaying the data in browser for a particular frame, but from
success, frame = cap.read() 
I am not able to fetch data from
variable success
and share the data with API in which I am getting streamed data,

from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)

# list of camera accesses
cameras = [
 "rtsp://username:[email protected]_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp",
 "rtsp://username:[email protected]_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp", 
...
  ]


def find_camera(list_id):
    return cameras[int(list_id)]


def gen_frames(camera_id):
    cam = find_camera(camera_id)  # return the camera access link with credentials. Assume 0?
    # cam = cameras[int(id)]
    cap = cv2.VideoCapture(cam)  # capture the video from the live feed

    while True:

        # # Capture frame-by-frame. Return boolean(True=frame read correctly. )
        success, frame = cap.read()  # read the camera frame
        if not success:
            break
        else:
            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')  # concat frame one by one and show result


@app.route('/video_feed/<string:list_id>/', methods=["GET"])
def video_feed(list_id):
    return Response(gen_frames(list_id),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/', methods=["GET"])
def index():
    return render_template('index.html', camera_list=len(cameras), camera=cameras)


if __name__ == '__main__':
    app.run()
Suggestions will be really helpful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask TypeError: Object of type Decimal is not JSON serializable mekacharan 0 1,249 Jul-15-2021, 05:28 AM
Last Post: mekacharan
  flask webhook response onicode5476 1 970 Aug-29-2020, 03:27 AM
Last Post: onicode5476
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 2,401 Aug-12-2020, 07:24 PM
Last Post: Alpy
  PUT http query return <Response [500]> rndoma 7 1,336 Jun-10-2020, 09:17 PM
Last Post: rndoma
  problem with the return of flask loutsi 4 893 Jun-04-2020, 08:12 AM
Last Post: loutsi
  Flask Create global response function to be called from every where in the web app umen 2 1,027 Apr-14-2020, 09:54 PM
Last Post: umen
  Flask rest api How to retrieve json request raysefo 4 3,955 Jan-20-2019, 06:46 PM
Last Post: raysefo
  Flask return http status code 200 but web browser not recive supernoobs 2 5,728 Dec-29-2018, 09:27 PM
Last Post: Unisoftdev
  Paginate json data in flask or jinja2 soli1994 1 5,863 Jun-28-2018, 06:00 PM
Last Post: gontajones
  flask, I want to send a json from the front end and update it to the postgresql DB wi passion044 1 2,738 Apr-12-2018, 08:40 AM
Last Post: thomasp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020