3

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.

6
  • Put a serial.print incomingBit. Also serial.read returns a byte. Could be that your parse is failing. Commented Apr 21, 2017 at 21:56
  • Can you be a bit more specific? I am new to this and this is not very clear to me.Sorry for that. Commented Apr 22, 2017 at 17:25
  • Put Serial.println(incomingBit) after serial.read() to see if you get an incoming command Commented Apr 22, 2017 at 21:42
  • You need to debug the code. Is the message sent through the COM(serial communication), if so, is it recived on the other end? Anyway put ´Serial.print("I received: "); Serial.println(incomingBit);´ to se if anything is recived on the other end. Then get back to me! =) Commented Apr 23, 2017 at 11:03
  • The "detect" value in Python code is >0 but still when I upload my arduino code , I get no value at arduino IDE. Is there any way to solve it? @sigma Commented Apr 24, 2017 at 13:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.