Serial Communication Between Arduino and Python YOLO model delayed

Hello! I am trying to run a YOLO (you only look once) model on my laptop, and have a python script communicate with my arduino. However, the communication only happens after the script is closed. Please help!\

Python Code:

from ultralytics import YOLO
import serial
import time

arduino = serial.Serial(port='COM5', baudrate=9600, timeout=1)


def send_data(data):
    print(f"Sending: {data}")
    arduino.write(f"{data}\n".encode())
    time.sleep(0.1)

    response = arduino.readline().decode().strip()
    if response:
        print(f"Arduino Responded: {response}")

if __name__ == "__main__":
    time.sleep(2)
    print("Connected To Arduino")

model = YOLO('best.pt') 

# If using a USB camera, change number to 1
results = model(0, show= True)

for results in results:
    boxes = results.boxes
    classes = results.names

for box in results.boxes:
    class_id = int(box.cls[0])
    confidence = float(box.conf[0])


    detected_class = classes[class_id]


    if detected_class == "Garbage" and confidence > 0.65:
        send_data("g")
    
    elif detected_class =="Recycling" and confidence > 0.65:
        send_data("r")

    elif detected_class =="Compost" and confidence >0.65:
        send_data("c")

    time.sleep(10)

    send_data("g") 

Arduino Code

#include <AFMotor.h>

// Number of steps per output rotation
// Change this as per your motor's specification
const int stepsPerRevolution = 2048;

// connect motor to port #2 (M3 and M4)
AF_Stepper left_motor(stepsPerRevolution, 1);
AF_Stepper right_motor(stepsPerRevolution, 2);


void setup() {
  Serial.begin(9600);
  while (!Serial);
  left_motor.setSpeed(10);  // 10 rpm
  right_motor.setSpeed(10);
}

void loop() {
  if (Serial.available()) {
    String data = Serial.readString();
    Serial.print("Recieved:");


    data.trim();
     if (data =="g"){
      left_motor.step(512, FORWARD, DOUBLE);
      delay(1000);
      right_motor.step(512, BACKWARD, DOUBLE);
      delay(5000);
      right_motor.step(512, FORWARD, DOUBLE);
      delay(1000);
      left_motor.step(512, BACKWARD, DOUBLE);
      delay(3000);
    }
    

    data.trim();
    if (data == "r"){
      left_motor.step(1280, FORWARD, DOUBLE);
      delay(1000);
      right_motor.step(256, BACKWARD, DOUBLE);
      delay(5000);
      right_motor.step(256, FORWARD, DOUBLE);
      delay(1000);
      left_motor.step(1280, BACKWARD, DOUBLE);
      delay(3000);
    }


    data.trim();
    if (data == "c") {
      left_motor.step(256, FORWARD, DOUBLE);
      delay(1000);
      right_motor.step(1280, BACKWARD, DOUBLE);
      delay(5000);
      right_motor.step(1280, FORWARD, DOUBLE);
      delay(1000);
      left_motor.step(256, BACKWARD, DOUBLE);
      delay(3000);
    }
  }
}

Try

String data = Serial.readStringUntil('\n');

I don't think there is any point repeating this line more than once. After the first time, it won't do anything.

These lines are repeated 3 times, except for a couple of slightly different numbers. You could make a function for that.