mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
added mutex in python
This commit is contained in:
@@ -3,6 +3,7 @@ import paho.mqtt.client as mqtt
|
|||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import threading
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -14,6 +15,9 @@ latest_image = None
|
|||||||
processed_image = None
|
processed_image = None
|
||||||
yolo_results = []
|
yolo_results = []
|
||||||
|
|
||||||
|
# Lock for thread-safe access to shared variables
|
||||||
|
lock = threading.Lock()
|
||||||
|
|
||||||
# List of class names (example for COCO dataset)
|
# List of class names (example for COCO dataset)
|
||||||
yolo_classes = list(model.names.values())
|
yolo_classes = list(model.names.values())
|
||||||
|
|
||||||
@@ -22,12 +26,13 @@ def on_message(client, userdata, message):
|
|||||||
if message.topic == "kobuki/data":
|
if message.topic == "kobuki/data":
|
||||||
kobuki_message = str(message.payload.decode("utf-8"))
|
kobuki_message = str(message.payload.decode("utf-8"))
|
||||||
elif message.topic == "kobuki/cam":
|
elif message.topic == "kobuki/cam":
|
||||||
|
with lock: # Lock the shared variables between threads so they can't be accessed at the same time and you cant have half processed images
|
||||||
latest_image = np.frombuffer(message.payload, np.uint8)
|
latest_image = np.frombuffer(message.payload, np.uint8)
|
||||||
latest_image = cv2.imdecode(latest_image, cv2.IMREAD_COLOR)
|
latest_image = cv2.imdecode(latest_image, cv2.IMREAD_COLOR)
|
||||||
processed_image = latest_image.copy() # Create a copy for processing
|
|
||||||
# Process the image with YOLO
|
# Process the image with YOLO
|
||||||
results = model(latest_image)
|
results = model(latest_image)
|
||||||
yolo_results = []
|
yolo_results = []
|
||||||
|
processed_image = latest_image.copy() # Create a copy for processing
|
||||||
for result in results:
|
for result in results:
|
||||||
for box in result.boxes:
|
for box in result.boxes:
|
||||||
class_id = int(box.cls.item())
|
class_id = int(box.cls.item())
|
||||||
@@ -50,7 +55,7 @@ mqtt_client.loop_start()
|
|||||||
mqtt_client.subscribe("kobuki/data")
|
mqtt_client.subscribe("kobuki/data")
|
||||||
mqtt_client.subscribe("kobuki/cam")
|
mqtt_client.subscribe("kobuki/cam")
|
||||||
|
|
||||||
mqtt_client.on_message = on_message # this line needs to be under the function definition otherwise it cant find which function it needs to use
|
mqtt_client.on_message = on_message # this line needs to be under the function definition otherwise it can't find which function it needs to use
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
@@ -64,6 +69,7 @@ def control():
|
|||||||
else:
|
else:
|
||||||
return ('Unauthorized', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
|
return ('Unauthorized', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
|
||||||
|
|
||||||
|
|
||||||
@app.route('/move', methods=['POST'])
|
@app.route('/move', methods=['POST'])
|
||||||
def move():
|
def move():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
@@ -80,19 +86,24 @@ def move():
|
|||||||
def data():
|
def data():
|
||||||
return kobuki_message
|
return kobuki_message
|
||||||
|
|
||||||
|
|
||||||
@app.route('/image')
|
@app.route('/image')
|
||||||
def image():
|
def image():
|
||||||
global processed_image
|
global processed_image
|
||||||
|
with lock: # Lock the shared variables between threads so they can't be accessed at the same time and you cant have half processed images
|
||||||
if processed_image is not None:
|
if processed_image is not None:
|
||||||
_, buffer = cv2.imencode('.jpg', processed_image)
|
_, buffer = cv2.imencode('.jpg', processed_image)
|
||||||
return Response(buffer.tobytes(), mimetype='image/jpeg')
|
return Response(buffer.tobytes(), mimetype='image/jpeg')
|
||||||
else:
|
else:
|
||||||
return "No image available", 404
|
return "No image available", 404
|
||||||
|
|
||||||
|
|
||||||
@app.route('/yolo_results', methods=['GET'])
|
@app.route('/yolo_results', methods=['GET'])
|
||||||
def yolo_results_endpoint():
|
def yolo_results_endpoint():
|
||||||
global yolo_results
|
global yolo_results
|
||||||
|
with lock:
|
||||||
return jsonify(yolo_results)
|
return jsonify(yolo_results)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True, port=5000)
|
app.run(debug=True, port=5000)
|
Reference in New Issue
Block a user