I have a educated model. And model has three classes as "damaged-blade", "solid-blade" and "wind-turbine". My goal is to send this data to Arduino and print it on the LCD, no matter how many of them are detected. Even if I close Python, I want to keep the last value displayed until the power is turned off.
Note: I wrote both codes together
from ultralytics import YOLO
import random
import cv2
import numpy as np
import serial
import time
import os
model = YOLO("hasarlıvesaglamv8x(70-10-20).pt") # Yolov8 148 adet sağlam ve hasarlı fotoğraf.
img = cv2.imread("hasarlı2.jpg")
# if you want all classes
yolo_classes = list(model.names.values())
classes_ids = [yolo_classes.index(clas) for clas in yolo_classes]
conf = 0.5
results = model.predict(img, conf=conf)
colors = [random.choices(range(256), k=3) for _ in classes_ids]
print(results)
# Initialize counts for each class
class_counts = {clas: 0 for clas in yolo_classes}
# Open the serial connection with Arduino
arduino_port = "COM6" # Change this to your Arduino's COM port
arduino = serial.Serial(arduino_port, 9600, timeout=1)
time.sleep(2) # Allow time for Arduino to initialize
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
file_path = os.path.join(desktop_path, "okuma.txt")
file = open(file_path, "w")
for result in results:
for mask, box in zip(result.masks.xy, result.boxes):
points = np.int32([mask])
color_number = classes_ids.index(int(box.cls[0]))
cv2.fillPoly(img, points, colors[color_number])
# Display confidence value in red on the object
text = f"{box.conf.item():.2f}"
font_color = (0, 0, 255) # Red color
center_x = int(np.mean(mask[:, 0]))
center_y = int(np.mean(mask[:, 1]))
cv2.putText(img, text, (center_x, center_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, font_color, 2)
# Count detected objects for each class
class_label = yolo_classes[int(box.cls[0])]
class_counts[class_label] += 1
# Print the counts for each class to the console
for clas, count in class_counts.items():
print(f"{clas}: {count}", end=", ")
# Send the counts for each class to Arduino with a 2-second delay
for count in class_counts.values():
arduino_data = f"{count}"
arduino.write(arduino_data.encode())
time.sleep(2)
# Create and open a text file on the desktop to read sent informations again on console
try:
while True:
# Read incoming data from Arduino
okuma = arduino.readline().decode().strip()
if okuma:
# Split the incoming data into a list of integers
received_values = list(map(int, okuma.split(',')))
# Print received values
print("Received values:", received_values)
# Write the values to the text file
file.write(",".join(map(str, received_values)) + "\n")
time.sleep(2) # Adjust the delay based on your needs
except KeyboardInterrupt:
# Close the file on program exit
file.close()
cv2.imshow("Image", img)
time.sleep(1)
key = cv2.waitKey(0) & 0xFF
if key == ord('q'):
break
cv2.destroyAllWindows()
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
String blades[3] = {"Damaged Blade", "Solid Blade", "Wind Turbine"};
int values[3]; // Array to store three integer values
void setup() {
Serial.begin(9600); // Initialize Serial communication
lcd.begin(16, 2); // Adjust columns and rows based on your LCD specifications
}
void loop() {
if (Serial.available() > 0) {
Serial.read();
for (int i = 0; i < 3; ++i) {
values[i] = Serial.parseInt();
}
// Display the values on the LCD
lcd.clear(); // Clear the LCD screen
for (int i = 0; i < 3; ++i) {
lcd.setCursor(i * 4, 0);
lcd.print(char('A' + i)); // Display A, B, C labels
lcd.print(": ");
lcd.print(values[i]);
}
// Send the values back to the computer
Serial.print(values[0]);
Serial.print(",");
Serial.print(values[1]);
Serial.print(",");
Serial.println(values[2]);
delay(1000); // Adjust the delay based on your needs
}