began implementation of async call from web app

This commit is contained in:
paul-loedige 2023-01-17 16:52:20 +00:00
parent 7af627c2b4
commit b5dc9e3e73
6 changed files with 69 additions and 53 deletions

39
app.py Normal file
View File

@ -0,0 +1,39 @@
from flask import Flask, render_template, Response
import asyncio
from indication_driver import IndicationDriver
from camera_driver import CameraDriver
import config
app = Flask(__name__)
indication_driver = IndicationDriver()
camera_driver = CameraDriver()
def stream():
global indication_driver
global camera_driver
indication_task = None
while True:
jpg, codes = camera_driver.get_decoded_frame(400, 800)
trash_category = None
if codes:
if indication_task is not None:
indication_driver.stop_loop()
if codes[0] in config.BARCODE_LOOKUP_TABLE.keys():
trash_category = config.BARCODE_LOOKUP_TABLE[codes[0]]
indication_task = asyncio.create_task(indication_driver.indicate_async(trash_category))
else:
indication_driver.stop_loop()
yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + jpg + b'\r\n\r\n'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/stream_feed')
def stream_feed():
return Response(stream(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(debug=False, port=5000, host='0.0.0.0')

29
camera_driver.py Normal file
View File

@ -0,0 +1,29 @@
import imutils
from imutils.video import VideoStream
import pyzbar.pyzbar as pyzbar
from typing import List, Tuple
import cv2
class CameraDriver:
def __init__(self, camera_index = 0):
self.video_stream = VideoStream(src=camera_index).start()
self.frame = None
def get_decoded_frame(self, stream_frame_width, decode_frame_width) -> Tuple[bytes, List[str]]:
raw_frame = self.video_stream.read()
decode_frame = imutils.resize(raw_frame, width=decode_frame_width)
codes = pyzbar.decode(decode_frame)
data = []
for code in codes:
x, y, w, h = code.rect
cv2.rectangle(decode_frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
data.append(code.data.decode('utf-8'))
cv2.putText(decode_frame, data[-1], (x, y -10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
if codes:
stream_frame = imutils.resize(decode_frame, width=stream_frame_width)
else:
stream_frame = imutils.resize(raw_frame, width=stream_frame_width)
_, jpg = cv2.imencode('.jpg', stream_frame)
return jpg.tobytes(), data

View File

@ -15,7 +15,7 @@ TRASH_LOCATION = {
}
BARCODE_LOOKUP_TABLE = {
'X00UP6UQN' : "PAPER"
'X000UP6UQN' : "PAPER"
}
RFID_LOOKUP_TABLE = {

View File

Before

Width:  |  Height:  |  Size: 548 KiB

After

Width:  |  Height:  |  Size: 548 KiB

View File

@ -1,52 +0,0 @@
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')