هذا كود برمجي بلغة بايثون:
import cv2 from ultralytics import YOLO import serial
1. إعداد النموذج (يجب تدريب نموذج YOLO على صور الآفات)
model = YOLO('pest_model.pt')
2. إعداد الاتصال بجهاز التحكم (مثلاً Arduino)
arduino = serial.Serial('COM3', 9600)
def control_laser(x, y): # تحويل الإحداثيات إلى زوايا للمحركات # command = f"{x},{y}\n" # arduino.write(command.encode()) print(f"Laser firing at: {x}, {y}")
3. معالجة الفيديو في الوقت الفعلي
cap = cv2.VideoCapture(0)
while cap.isOpened(): ret, frame = cap.read() if not ret: break
# اكتشاف الآفات
results = model(frame)
for r in results:
for box in r.boxes:
# الحصول على إحداثيات مركز الحشرة
x1, y1, x2, y2 = box.xyxy[0]
center_x = (x1 + x2) / 2
center_y = (y1 + y2) / 2
# إطلاق الليزر
control_laser(center_x, center_y)
cv2.imshow('Pest Control View', frame)
if cv2.waitKey(1) == ord('q'): break
cap.release() cv2.destroyAllWindows()
Log in or sign up for Devpost to join the conversation.