I want to connect OpenCV (Python) with arduino Uno when it detects a tennis ball.
OpenCV code:
import numpy as np
import cv2
import serial
face_cascade = cv2.CascadeClassifier('tennisballdetect.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print(faces)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
detect=(x+y)/2
print(detect)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
# Module -2: Trigger Pyserial
if detect > 0:
ser = serial.Serial('COM1', 9600)
print(ser)
ser.write('Y')
else:
ser = serial.Serial('COM1', 9600)
print(ser)
ser.write('N')
My Arduino Code:
#include <Servo.h>
int servoPin = 3;
Servo Servo1;
char incomingBit; // for incoming serial data
void setup() {
// We need to attach the servo to the used pin number
// Servo1.attach(servoPin);
pinMode(servoPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
if (Serial.available() > 0) { // Serial.available(), which detects if
any data has been sent to the
//serial port
// Serial.read() will read the incoming character and store it in
incomingBit
incomingBit = Serial.read();
Serial.print("I received: ");
//if the value of incomingBit is ‘Y’, we need to turn the LED ‘on’, else we turn it ‘off’. So,
//in a nutshell,if detecting faces from a given image was successful, this will turn on the LED
if(incomingBit == 'Y' || incomingBit == 'y') {
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
//exit(0);
}
else {
digitalWrite(servoPin, LOW);
}
}
}
Servo is correctly connected to Arduino and it is moving without serial communication. openCV is also working and detecting tennisball.
But when trying to connect OpenCV to arduino, servo is not moving. Any kind of help is appreciated.