Arduino Serial Communication. TX lags RX.

I am using OpenCV and Python for Video Processing. The code detects a binary thresholded object in the camera frame. If the particular colored object is in front of the cam, python sends char '"1" using pyserial. As a while loop is used to go through each frame of the video, RX is active most of the time on arduino. Now if i receive char "1", i want to set Servo to 90 degrees else every time I want to set Servo to 0 degrees.

Python Code -

import opencv
import serial

ser = serial.Serial("/dev/ttyACM0", 115200)
cap = cv2.VideoCapture(0)

while(cap.issOpened()):
       ret, img = cap.read()
       #Thresolding Code
      if(object_detected):
            serial.write("1")

Arduino Code -

#include <Servo.h>

int incomingByte = 0;
 
Servo myservo;

void setup() {
  
  Serial.begin(115200);
  myservo.attach(9);
  
}

void loop() {
  if (Serial.available() > 0) {
      
      incomingByte = Serial.read();
      if(incomingByte == 49){
      myservo.write(90);
      }
      else{
      myservo.write(0);
      }
      }
      }

The RX receives the byte on time but the loop stucks while transmitting. How can I optimize my code to solve the issue? i tried Serial.flush() function which waits for transmission to complete but it was of no help.

Thanks in advance.

Hintif(incomingByte == '1'){

I think its the same as character "1" 's ASCII value is 49 and it would convert to 49 only.

It is the same, but isn't it easier to use '1', than 49?

I don't see any code that tries to write...

This Python - Arduino demo may help. See also Serial Input Basics

...R

It sounds to me like an output buffer is filling up. When the output buffer fills up your Python program will have to wait for a character to be sent before it can add another character to the output buffer.