Hello, I am fairly new to programming and was having trouble getting this program to work properly, and thought this might be the place to ask.
My project's idea is to find a person's face from a camera using OpenCV and determine whether the face is on the left or right side of the screen. Python will send the X-axis coordinate to an Arduino through its serial to tell a DC motor to move left or right based on the given number, and align itself with the person's face. (it only moves on the X axis with the help of a guided rail.) I have figured out how to get the coordinates of the face tracker and send them through to the Arduinos serial. As well as making the motor move with different numbers.
my only problem is that python sends the X and the Y coordinate together I only need the X axis coordinate on its own and I am not sure how to get rid of the Y axis coordinate as it interferes with the reading of the numbers for the DC motor on the Arduinos end.
for example, python will send its data something like this
"(340, 367)"
however, I would like it to be read something like this
"340"
if anyone knows how to help in any way that would be greatly appreciated.
thank you
PYTHON CODE
from cvzone.FaceDetectionModule import FaceDetector
import cv2
import serial
import time
arduino = serial.Serial(port='COM5', baudrate=9600, timeout=.1)
time.sleep(2)
def write_read(x):
arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)
data = arduino.readline()
return data
cap = cv2.VideoCapture(0)
detector = FaceDetector()
while True:
success, img = cap.read()
img, bboxs = detector.findFaces(img)
if bboxs:
# bboxInfo - "id","bbox","score","center"
center = bboxs[0]["center"]
gamerRage = str(center)
cv2.circle(img, center, 5, (255, 255), cv2.FILLED)
print(center)
arduino.write(gamerRage.encode())
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
ARDUINO CODE
int myNumber;//serial number
int i = 20; // motor settup
int R_IS = 5;
int R_EN = 4;
int R_PWM = 3;
int L_IS = 8;
int L_EN = 7;
int L_PWM = 6;
void setup() {
Serial.begin(9600);//opens comunication
pinMode(LED_BUILTIN, OUTPUT);//led
Serial.setTimeout(5);//shortens the checking of a number to 5ms(Serial.parseInt)
pinMode(R_IS, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(R_PWM, OUTPUT);
pinMode(L_IS, OUTPUT);
pinMode(L_EN, OUTPUT);
pinMode(L_PWM, OUTPUT);
digitalWrite(R_IS, LOW);
digitalWrite(L_IS, LOW);
digitalWrite(R_EN, HIGH);
digitalWrite(L_EN, HIGH);
}
void loop(){
if (Serial.available()>0) {
myNumber = Serial.parseInt();//this was holding back the code giving the 4 tic delay
if (myNumber >= 680) {//if less than 680 turn on motor left
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
}
else if (myNumber <= 680) {//if more than 680 turn motor right
analogWrite(R_PWM, i);
analogWrite(L_PWM, 0);
}
}
}