import cv2
import tensorflow as tf
import serial
import time
import numpy as np
# Modeli yükle
model_path = 'path_to_your_model.h5' # Gerçek yolu buraya yazın
try:
model = tf.keras.models.load_model(model_path)
except Exception as e:
print(f"Model yüklenirken hata oluştu: {e}")
exit()
# Seri bağlantıları başlat
try:
lcd = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
robot_arm = serial.Serial('/dev/ttyUSB1', 9600, timeout=1)
except Exception as e:
print(f"Seri bağlantı kurulurken hata: {e}")
exit()
# Kamera ayarı
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Kamera erişilemiyor.")
exit()
# Görüntü ön işleme
def preprocess_image(image):
image = cv2.resize(image, (128, 128))
image = image / 255.0
image = np.reshape(image, (-1, 128, 128, 3))
return image
# Tahmin sonucunu sınıfa çevirme
def get_weed_name_and_method(prediction):
weed_classes = ['Ot Türü 1', 'Ot Türü 2', 'Ot Türü 3']
removal_methods = ['Yöntem 1', 'Yöntem 2', 'Yöntem 3']
index = int(np.argmax(prediction))
return weed_classes[index], removal_methods[index]
print("Sistem çalışıyor. Çıkmak için 'q' tuşuna basın.")
while True:
ret, frame = cap.read()
if not ret:
print("Kare alınamadı.")
continue
# Görüntüyü işle
preprocessed = preprocess_image(frame)
# Tahmin yap
prediction = model.predict(preprocessed)
weed_name, method = get_weed_name_and_method(prediction)
confidence = float(np.max(prediction))
# LCD'ye bilgi gönder
lcd_message = f"{weed_name}, {method}, % {confidence*100:.1f}\n"
try:
lcd.write(lcd_message.encode('utf-8'))
except Exception as e:
print(f"LCD yazma hatası: {e}")
# Robot koluna komut gönder
command_map = {
'Ot Türü 1': b'KOMUT_1',
'Ot Türü 2': b'KOMUT_2',
'Ot Türü 3': b'KOMUT_3'
}
command = command_map.get(weed_name)
if command:
try:
robot_arm.write(command)
except Exception as e:
print(f"Robot kol komut hatası: {e}")
# 'q' tuşuna basılırsa çık
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(1)
cap.release()
cv2.destroyAllWindows()
Merhaba, kodum domates tarlasında ot imhası için çalışacak bir robotun python kodu. Bir yanlışlık varsa düzeltebilirseniz veya bana söylerseniz çok mutlu olurum.