fix: flask api

This commit is contained in:
rendies 2023-01-12 19:45:47 +07:00
parent 1fe9bc33b6
commit e75a0b2c06

View File

@ -12,19 +12,34 @@ import threading
from itertools import zip_longest from itertools import zip_longest
from flask import Response from flask import Response
from flask import Flask from flask import Flask
from flask import render_template
from flask import jsonify from flask import jsonify
from flask_cors import CORS, cross_origin from flask_cors import CORS, cross_origin
t0 = time.time() t0 = time.time()
outputFrame = None outputFrame = None
people_count = 0
lock = threading.Lock() lock = threading.Lock()
# initialize a flask object # initialize a flask object
app = Flask(__name__) app = Flask(__name__)
cors = CORS(app) cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type' app.config['CORS_HEADERS'] = 'Content-Type'
@app.route("/counter")
@cross_origin()
def counter():
# return the rendered template
global people_count
return jsonify(people_count)
@app.route("/video")
@cross_origin()
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate(),
mimetype = "multipart/x-mixed-replace; boundary=frame")
def run(args): def run(args):
global outputFrame, lock, people_count global outputFrame, lock, people_count
# construct the argument parse and parse the arguments # construct the argument parse and parse the arguments
@ -333,14 +348,14 @@ def run(args):
break break
# stop the timer and display FPS information # stop the timer and display FPS information
fps.stop() # fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) # print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# # if we are not using a video file, stop the camera video stream # # if we are not using a video file, stop the camera video stream
# if not args.get("input", False): if not args.get("input", False):
# vs.stop() vs.stop()
# #
# # otherwise, release the video file pointer # # otherwise, release the video file pointer
# else: # else:
@ -373,20 +388,7 @@ def generate():
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n') bytearray(encodedImage) + b'\r\n')
@app.route("/counter")
@cross_origin()
def stats():
# return the rendered template
global people_count
return jsonify(people_count)
@app.route("/video_feed")
@cross_origin()
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate(),
mimetype = "multipart/x-mixed-replace; boundary=frame")
# check to see if this is the main thread of execution # check to see if this is the main thread of execution
if __name__ == '__main__': if __name__ == '__main__':