TREx/website/app.py

53 lines
1.7 KiB
Python

from flask import Flask, render_template, Response
import cv2
import imutils
from imutils.video import VideoStream
from pyzbar import pyzbar
import time
import sys
sys.path.append("..")
from TREx.indication_driver import IndicationDriver
app = Flask(__name__)
vs = VideoStream(src=0).start()
time.sleep(2)
frame = None
indication_driver = IndicationDriver()
def stream(camera_index):
global frame
global indication_driver
while True:
raw_frame = vs.read()
stream_frame = imutils.resize(raw_frame, width=400)
decode_frame = imutils.resize(raw_frame, width=800)
barcodes = pyzbar.decode(decode_frame)
for barcode in barcodes:
(x, y, w, h) = barcode.rect
cv2.rectangle(decode_frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
barcode_data = barcode.data.decode("utf-8")
if barcode_data == "X000UP6UQN":
print(f"{barcode_data}: CAN")
indication_driver.indicate("CAN")
else:
print(barcode_data)
indication_driver.clear()
cv2.putText(decode_frame, barcode_data, (x, y -10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
stream_frame = imutils.resize(decode_frame, width=400)
ret, jpg = cv2.imencode('.jpg', stream_frame)
jpg2bytes = jpg.tobytes()
yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + jpg2bytes + b'\r\n\r\n'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/stream_feed')
def stream_fedd():
return Response(stream(0), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(debug=False, port=5000, host='0.0.0.0')