From b5dc9e3e7366efcbfc4731ec6ab5c80f1d87adb0 Mon Sep 17 00:00:00 2001 From: ploedige Date: Tue, 17 Jan 2023 16:52:20 +0000 Subject: [PATCH] began implementation of async call from web app --- app.py | 39 ++++++++++++++ camera_driver.py | 29 +++++++++++ config.py | 2 +- {website/static => static}/dinosaur_face.png | Bin {website/templates => templates}/index.html | 0 website/app.py | 52 ------------------- 6 files changed, 69 insertions(+), 53 deletions(-) create mode 100644 app.py create mode 100644 camera_driver.py rename {website/static => static}/dinosaur_face.png (100%) rename {website/templates => templates}/index.html (100%) delete mode 100644 website/app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..4fd71e7 --- /dev/null +++ b/app.py @@ -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') diff --git a/camera_driver.py b/camera_driver.py new file mode 100644 index 0000000..1a6ec3f --- /dev/null +++ b/camera_driver.py @@ -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 + diff --git a/config.py b/config.py index cfdb53e..9af7dab 100644 --- a/config.py +++ b/config.py @@ -15,7 +15,7 @@ TRASH_LOCATION = { } BARCODE_LOOKUP_TABLE = { - 'X00UP6UQN' : "PAPER" + 'X000UP6UQN' : "PAPER" } RFID_LOOKUP_TABLE = { diff --git a/website/static/dinosaur_face.png b/static/dinosaur_face.png similarity index 100% rename from website/static/dinosaur_face.png rename to static/dinosaur_face.png diff --git a/website/templates/index.html b/templates/index.html similarity index 100% rename from website/templates/index.html rename to templates/index.html diff --git a/website/app.py b/website/app.py deleted file mode 100644 index 2531cde..0000000 --- a/website/app.py +++ /dev/null @@ -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')