74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
from flask import Flask, render_template, Response
|
|
from mfrc522 import SimpleMFRC522
|
|
from threading import Thread
|
|
from time import sleep
|
|
|
|
from indication_driver import IndicationDriver
|
|
from camera_driver import CameraDriver
|
|
import config
|
|
|
|
app = Flask(__name__)
|
|
|
|
indication_driver = IndicationDriver()
|
|
camera_driver = CameraDriver()
|
|
|
|
def _camera_stream():
|
|
global indication_driver
|
|
global camera_driver
|
|
last_codes = []
|
|
while True:
|
|
jpg, codes = camera_driver.get_decoded_frame(400, 800)
|
|
if codes and not (set(codes) & set(last_codes)):
|
|
print(codes)
|
|
trash_category = None
|
|
known_code = None
|
|
for code in codes:
|
|
known_code = code if code in config.BARCODE_LOOKUP_TABLE.keys() else known_code
|
|
if known_code:
|
|
trash_category = config.BARCODE_LOOKUP_TABLE[known_code]
|
|
indication_driver.indicate(trash_category)
|
|
else:
|
|
indication_driver.stop()
|
|
last_codes = codes
|
|
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('/camera_stream')
|
|
def camera_stream_feed():
|
|
return Response(_camera_stream(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
|
|
|
@app.route('/trash_category_stream')
|
|
def trash_category_stream():
|
|
def generate():
|
|
global indication_driver
|
|
while True:
|
|
if indication_driver.trash_category == "":
|
|
yield ("READY" + "\\n")
|
|
else:
|
|
yield (indication_driver.trash_category + "\\n")
|
|
sleep(.5)
|
|
return Response(generate(), mimetype='text/plain')
|
|
|
|
def rfid_loop():
|
|
print("RFID Loop started")
|
|
global indication_driver
|
|
reader = SimpleMFRC522()
|
|
last_id = 0
|
|
while True:
|
|
id, _ = reader.read()
|
|
if id in config.RFID_LOOKUP_TABLE.keys():
|
|
trash_category = config.RFID_LOOKUP_TABLE[id]
|
|
print(f"{id}: {trash_category}")
|
|
indication_driver.indicate(trash_category)
|
|
else:
|
|
indication_driver.stop()
|
|
print(id)
|
|
last_id = id
|
|
|
|
if __name__ == '__main__':
|
|
Thread(target = rfid_loop).start()
|
|
app.run(debug=False, port=5000, host='0.0.0.0')
|