|
| 1 | +#!/usr/bin/env python |
| 2 | +from importlib import import_module |
| 3 | +import os |
| 4 | +import cv2 |
| 5 | +from flask import Flask, render_template, Response, request |
| 6 | +from sqlConnector import SqlConnector |
| 7 | + |
| 8 | +app = Flask(__name__) |
| 9 | + |
| 10 | +@app.route('/') |
| 11 | +def index(): |
| 12 | + """Contoso Supermarket home page.""" |
| 13 | + cameras_enabled = True |
| 14 | + if os.environ.get('CAMERAS_ENABLED'): |
| 15 | + cameras_enabled = os.environ.get('CAMERAS_ENABLED') == 'True' |
| 16 | + |
| 17 | + head_title = "Contoso Supermarket" |
| 18 | + if os.environ.get('HEAD_TITLE'): |
| 19 | + head_title = os.environ.get('HEAD_TITLE') |
| 20 | + |
| 21 | + new_category = False |
| 22 | + if os.environ.get('NEW_CATEGORY'): |
| 23 | + new_category = os.environ.get('NEW_CATEGORY') == 'True' |
| 24 | + |
| 25 | + return render_template('index2.html' if new_category else 'index.html', head_title = head_title, cameras_enabled = cameras_enabled) |
| 26 | + |
| 27 | +@app.route('/addPurchase',methods = ['POST']) |
| 28 | +def addPurchase(): |
| 29 | + content_type = request.headers.get('Content-Type') |
| 30 | + if (content_type == 'application/json; charset=UTF-8'): |
| 31 | + json = request.get_json() |
| 32 | + sqlDb = SqlConnector() |
| 33 | + successful = sqlDb.addPurchase(json['ProductId']) |
| 34 | + if(successful): |
| 35 | + return "Ok" |
| 36 | + else: |
| 37 | + return "Error processing request" |
| 38 | + else: |
| 39 | + return 'Content-Type not supported!' |
| 40 | + |
| 41 | +@app.route('/video_feed/<feed>') |
| 42 | +def video_feed(feed): |
| 43 | + return Response(gen_frames(feed), |
| 44 | + mimetype='multipart/x-mixed-replace; boundary=frame') |
| 45 | + |
| 46 | + |
| 47 | +def gen_frames(source): |
| 48 | + """Video streaming frame capture function.""" |
| 49 | + baseUrl = "rtsp://localhost:554/media/" |
| 50 | + if os.environ.get('CAMERAS_BASEURL'): |
| 51 | + baseUrl = str(os.environ['CAMERAS_BASEURL']) |
| 52 | + |
| 53 | + cap = cv2.VideoCapture(baseUrl + source) # capture the video from the live feed |
| 54 | + |
| 55 | + while True: |
| 56 | + # # Capture frame-by-frame. Return boolean(True=frame read correctly. ) |
| 57 | + success, frame = cap.read() # read the camera frame |
| 58 | + if not success: |
| 59 | + break |
| 60 | + else: |
| 61 | + ret, buffer = cv2.imencode('.jpg', frame) |
| 62 | + frame = buffer.tobytes() |
| 63 | + yield (b'--frame\r\n' |
| 64 | + b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + app.run(host='0.0.0.0', threaded=True, debug=True) |
0 commit comments