I have the python programme below. The program below aims to detect a red ball and display the x and y position values. My question is when the data is sent to Arduino via serial how do I separate the x and y data, I want the x data to enter the x variable and the y data to enter the y variable. Thank you very much.
import cv2
import numpy as np
import serial
import time
ser = serial.Serial(port='COM8', baudrate=115200, timeout=.1)
cam = cv2.VideoCapture(0)
while(1):
_, frame = cam.read()
hsvFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
redLower = np.array([136, 87, 111], np.uint8)
redUpper = np.array([180, 255, 255], np.uint8)
redMask = cv2.inRange(hsvFrame, redLower, redUpper)
kernel = np.ones((5, 5), "uint8")
redMask = cv2.dilate(redMask, kernel)
resRed = cv2.bitwise_and(frame, frame, mask = redMask)
contours, hierarchy = cv2.findContours(redMask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area > 300):
x, y, w, h = cv2.boundingRect(contour)
frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(frame, "Merah", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))
ser.write(f"{x},{y}".encode('utf-8'))
response = ser.readline().decode('utf-8').strip()
print("Arduino Response:", response)
cv2.imshow("Deteksi Warna", frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
cam.release()
cv2.destroyAllWindows()
break