mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
added yolo image object detection to /image
This commit is contained in:
41
src/Python/YOLO/app.py
Normal file
41
src/Python/YOLO/app.py
Normal file
@@ -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()
|
@@ -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/<path:path>')
|
||||
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)
|
BIN
src/Python/flask/web/yolo11n.pt
Normal file
BIN
src/Python/flask/web/yolo11n.pt
Normal file
Binary file not shown.
Reference in New Issue
Block a user