finished code
This commit is contained in:
parent
b61d514910
commit
24f3baa65d
42
app.py
42
app.py
@ -1,4 +1,7 @@
|
||||
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
|
||||
@ -9,7 +12,7 @@ app = Flask(__name__)
|
||||
indication_driver = IndicationDriver()
|
||||
camera_driver = CameraDriver()
|
||||
|
||||
def stream():
|
||||
def _camera_stream():
|
||||
global indication_driver
|
||||
global camera_driver
|
||||
last_codes = []
|
||||
@ -26,16 +29,45 @@ def stream():
|
||||
indication_driver.indicate(trash_category)
|
||||
else:
|
||||
indication_driver.stop()
|
||||
last_codes = codes
|
||||
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('/stream_feed')
|
||||
def stream_feed():
|
||||
return Response(stream(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
||||
@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')
|
||||
|
@ -1,5 +1,5 @@
|
||||
import neopixel
|
||||
from time import sleep
|
||||
import time
|
||||
from typing import List
|
||||
from threading import Thread
|
||||
|
||||
@ -18,15 +18,19 @@ class IndicationDriver:
|
||||
)
|
||||
self._run_loop = True
|
||||
self._indication_loop = None
|
||||
self.trash_category = ""
|
||||
self.stop()
|
||||
|
||||
def _clear(self):
|
||||
self.pixels.fill((0,0,0))
|
||||
self.pixels.show()
|
||||
self.trash_category = ""
|
||||
|
||||
def stop(self):
|
||||
if self._indication_loop:
|
||||
self._run_loop = False
|
||||
self._indication_loop.join()
|
||||
self.pixels.fill((0,0,0))
|
||||
self.pixels.show()
|
||||
self._clear()
|
||||
|
||||
def _get_trash_location(self, trash_type: str) -> List[int]:
|
||||
if trash_type in config.TRASH_LOCATION:
|
||||
@ -37,6 +41,7 @@ class IndicationDriver:
|
||||
|
||||
def indicate(self, trash_type: str):
|
||||
self.stop()
|
||||
self.trash_category = trash_type
|
||||
location = self._get_trash_location(trash_type)
|
||||
|
||||
def _indication_loop(self, location: List[int]):
|
||||
@ -44,7 +49,8 @@ class IndicationDriver:
|
||||
self.pixels[idx] = (0, 255, 0)
|
||||
self.pixels.show()
|
||||
self._run_loop = True
|
||||
while self._run_loop:
|
||||
start_time = time.process_time()
|
||||
while self._run_loop and time.process_time() < start_time + 10:
|
||||
for step in range (4):
|
||||
for idx in range(location[0]):
|
||||
if idx % 4 == step:
|
||||
@ -52,8 +58,9 @@ class IndicationDriver:
|
||||
else:
|
||||
self.pixels[idx] = (0, 0, 0)
|
||||
self.pixels.show()
|
||||
sleep(.1)
|
||||
time.sleep(.1)
|
||||
self._clear()
|
||||
|
||||
self._indication_loop = Thread(target = _indication_loop, args = (self, location, ))
|
||||
self._indication_loop.start()
|
||||
sleep(1)
|
||||
sleep(.1)
|
@ -2,16 +2,51 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>🔴 Streaming...</title>
|
||||
<title>T.REx</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='style.css') }}">
|
||||
</head>
|
||||
<style>
|
||||
body {
|
||||
background-image: url({{ url_for('static', filename='dinosaur_face.png') }} );
|
||||
background-size: cover;
|
||||
background-repeat: no-repeat;
|
||||
background-attachment: fixed;
|
||||
background-position: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 72px;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
color: #F0F8FF;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<div id="camera" align="center">
|
||||
<img id="camera_feed" src="/camera_stream">
|
||||
<h1 id="message"></h1>
|
||||
<script>
|
||||
var latest = document.getElementById('message');
|
||||
|
||||
<body>
|
||||
<div id="title">Camera Feed</div>
|
||||
<div id="camera">
|
||||
<img id="camera_feed" src="/stream_feed">
|
||||
</div>
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', '/trash_category_stream');
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
var all_lines = xhr.responseText.split('\\n');
|
||||
last_line = all_lines.length - 2
|
||||
latest.textContent = all_lines[last_line]
|
||||
|
||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
||||
/*alert("The End of Stream");*/
|
||||
latest.textContent = "The End of Stream"
|
||||
}
|
||||
}
|
||||
|
||||
xhr.send();
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user