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:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp",
 "rtsp://username:password@ip_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


Messages In This Thread
Return Frame as well as JSON response using same API in Flask Python Ask - by jenkins43 - May-11-2020, 04:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  response 404 or 500 when trying to get products/sales from woocommerce using python wailoonho 0 572 Dec-17-2023, 11:57 AM
Last Post: wailoonho
  POST requests - different requests return the same response Default_001 3 1,901 Mar-10-2022, 11:26 PM
Last Post: Default_001
  Flask TypeError: Object of type Decimal is not JSON serializable mekacharan 0 3,888 Jul-15-2021, 05:28 AM
Last Post: mekacharan
  flask webhook response onicode5476 1 2,479 Aug-29-2020, 03:27 AM
Last Post: onicode5476
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 3,944 Aug-12-2020, 07:24 PM
Last Post: Alpy
  PUT http query return <Response [500]> rndoma 7 4,192 Jun-10-2020, 09:17 PM
Last Post: rndoma
  problem with the return of flask loutsi 4 2,054 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 2,255 Apr-14-2020, 09:54 PM
Last Post: umen
  Flask rest api How to retrieve json request raysefo 4 6,014 Jan-20-2019, 06:46 PM
Last Post: raysefo
  Flask return http status code 200 but web browser not recive supernoobs 2 10,483 Dec-29-2018, 09:27 PM
Last Post: Unisoftdev

Forum Jump:

User Panel Messages

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