diff --git a/bus.jpg b/bus.jpg new file mode 100644 index 0000000..40eaaf5 Binary files /dev/null and b/bus.jpg differ diff --git a/src/Python/YOLO/app.py b/src/Python/YOLO/app.py new file mode 100644 index 0000000..e7d01ac --- /dev/null +++ b/src/Python/YOLO/app.py @@ -0,0 +1,41 @@ +from ultralytics import YOLO +import cv2 +import numpy as np +import requests +import time + +model = YOLO("yolo11n.pt") + +#try to fetch the image from the given url +def fetch_image(url): + try: + response = requests.get(url) + response.raise_for_status() + image_array = np.frombuffer(response.content, np.uint8) + image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) + return image + except requests.RequestException as e: + print(f"Error: Could not fetch image - {e}") + return None + +# URL of the photostream +url = "http://145.92.224.21/image" + +while True: + frame = fetch_image(url) + if frame is None: + print("Error: Could not fetch image, retrying...") + time.sleep(1) # Wait for 1 second before retrying + continue + + # Predict on the frame + results = model(frame) + + # Display the results + results[0].show() + + # Exit if 'q' is pressed + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +cv2.destroyAllWindows() \ No newline at end of file diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 2438aea..2811122 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -1,20 +1,44 @@ from flask import Flask, Response, request, render_template, jsonify import paho.mqtt.client as mqtt +from ultralytics import YOLO +import cv2 +import numpy as np app = Flask(__name__) +# Load a model +model = YOLO("yolo11n.pt") # pretrained YOLO11n model + +kobuki_message = "" +latest_image = None +yolo_results = [] + def on_message(client, userdata, message): - global kobuki_message, latest_image + global kobuki_message, latest_image, yolo_results if message.topic == "kobuki/data": kobuki_message = str(message.payload.decode("utf-8")) elif message.topic == "kobuki/cam": - latest_image = message.payload - + latest_image = np.frombuffer(message.payload, np.uint8) + latest_image = cv2.imdecode(latest_image, cv2.IMREAD_COLOR) + # Process the image with YOLO + results = model(latest_image) + yolo_results = [] + for result in results: + for box in result.boxes: + yolo_results.append({ + "class": box.cls, + "confidence": box.conf, + "bbox": box.xyxy.tolist() + }) + # Draw bounding box on the image + x1, y1, x2, y2 = map(int, box.xyxy) + cv2.rectangle(latest_image, (x1, y1), (x2, y2), (0, 255, 0), 2) + cv2.putText(latest_image, f"{box.cls} {box.conf:.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) # Create an MQTT client instance mqtt_client = mqtt.Client() mqtt_client.username_pw_set("server", "serverwachtwoordofzo") -mqtt_client.connect("localhost", 1884, 60) +mqtt_client.connect("localhost", 1883, 60) mqtt_client.loop_start() mqtt_client.subscribe("kobuki/data") mqtt_client.subscribe("kobuki/cam") @@ -53,18 +77,15 @@ def data(): def image(): global latest_image if latest_image is not None: - return Response(latest_image, mimetype='image/jpeg') + _, buffer = cv2.imencode('.jpg', latest_image) + return Response(buffer.tobytes(), mimetype='image/jpeg') else: return "No image available", 404 - -@app.route('/phpmyadmin/') -def phpmyadmin_passthrough(path): - # Laat Apache deze route direct afhandelen - return "", 404 - - - +@app.route('/yolo_results', methods=['GET']) +def yolo_results_endpoint(): + global yolo_results + return jsonify(yolo_results) if __name__ == '__main__': - app.run(debug=True, port=5000) + app.run(debug=True, port=5000) \ No newline at end of file diff --git a/src/Python/flask/web/yolo11n.pt b/src/Python/flask/web/yolo11n.pt new file mode 100644 index 0000000..45b273b Binary files /dev/null and b/src/Python/flask/web/yolo11n.pt differ