0

I'm having a problem loading the information into Arduino via pyserial.

I'm using a python and opencv script to do color detection via webcam. For every detected color I want to inform Arduino so it can make a decision. But I can not send the data to the Arduino.

This is the program I'm using in python

#importando módulos

import cv2   
import numpy as np
import serial
import time
import os    

#COM 4 = porta serial que está ligada o arduino
#115200 = baudrate
ser = serial.Serial('COM3', 115200, timeout = .5)

#capturando vídeo através da webcam

cap = cv2.VideoCapture(1)

while(1):
    _, imagem = cap.read()
    #quadro de conversão (imagem, ou seja, BGR) para HSV (valor de saturação de matiz)
    hsv = cv2.cvtColor(imagem, cv2.COLOR_BGR2HSV)

    #define a gama de cor vermelha
    red_lower = np.array([136, 87, 111], np.uint8)
    red_upper = np.array([180, 255, 255], np.uint8)

    #define a gama de cor azul
    blue_lower = np.array([99, 115, 150], np.uint8)
    blue_upper = np.array([110, 255, 255], np.uint8)

    #define a gama de cor verde
    green_lower = np.array([22, 60, 200], np.uint8)
    green_upper = np.array([60, 255, 255], np.uint8)

    #encontrar o intervalo de cor vermelha, azul e amarela na imagem
    red = cv2.inRange(hsv, red_lower, red_upper)
    blue = cv2.inRange(hsv, blue_lower, blue_upper)
    green = cv2.inRange(hsv, green_lower, green_upper)

    #Transformação Morfológica, Dilatação   
    kernal = np.ones((5 ,5), "uint8")
    red = cv2.dilate(red, kernal)
    res = cv2.bitwise_and(imagem, imagem, mask = red)
    blue = cv2.dilate(blue, kernal)
    res1 = cv2.bitwise_and(imagem, imagem, mask = blue)
    green = cv2.dilate(green, kernal)
    res2 = cv2.bitwise_and(imagem, imagem, mask = green)

    #Seguindo a cor vermelha
    (_, contours, hierarchy) = cv2.findContours(red, 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) 
            imagem = cv2.rectangle(imagem,(x, y), (x+w, y+h), (0, 0, 255), 2)
            cv2.putText(imagem, "Vermelho",(x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255))
        if (ser.inWaiting() > 0):
            ser.write(b"2")

    #Seguindo a cor azul
    (_, contours, hierarchy) = cv2.findContours(blue, 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) 
           imagem = cv2.rectangle(imagem, (x, y), (x+w, y+h), (255, 0, 0), 2)
           cv2.putText(imagem, "Azul", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0))
       if (ser.inWaiting() > 0):
          ser.write(b'1')
   #Seguindo a cor verde
   (_, contours, hierarchy) = cv2.findContours(green, 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) 
           imagem = cv2.rectangle(imagem, (x, y), (x+w, y+h), (0, 255, 0), 2)
           cv2.putText(imagem, "Verde", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0))
       if (ser.inWaiting() > 0):
           ser.write(b'3')

    cv2.imshow("Seguindo a Cor", imagem)

    if cv2.waitKey(10) & 0xFF == ord('q'):
        ser.close()
        cap.release()            
        cv2.destroyAllWindows()
        break

And this the script for the Arduino:

char incoming;
const int led1 = 2;
const int led2 = 4;
const int led3 = 6;

void setup() {
  Serial.begin(9600);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
}

void loop() {
  if (Serial.available() > 0) {
    char incoming = Serial.read();
    if (incoming == '1') {
      digitalWrite(led1, HIGH);
      Serial.println("Led 1 on");
    }
    else if (incoming == '2') {
      digitalWrite(led2, HIGH);
      Serial.println("Led 2 on");
    }
    else if (incoming == '3') {
      digitalWrite(led3, HIGH);
      Serial.println("Led 3 on");
    }
    else {}
  }
}

However when running the program in python the arduino only restarts and does not receive any data via serial. Could someone help me with this problem?

6
  • What machine and OS is the OpenCV running on? How is it connected to the Arduino? Commented Sep 17, 2018 at 11:10
  • the open cv is running on windows 10, it and python i am using the latest available versions, the arduino is not connected to opencv, it is connected to the python via usb of the notebook, in lines 79 and 80 is where i try to write in arduino when I detect the red color in python Commented Sep 17, 2018 at 11:32
  • 1
    Can I suggest you make a much simpler Python program that just sends "1", waits 5 seconds then sends "2" waits 5 seconds the sends "3". It will be much easier to debug if you exclude all the OpenCV stuff. Commented Sep 17, 2018 at 11:34
  • but is it possible to do this so that this data is sent from the detected color? Sorry for ignorance but I'm still learning to work with python Commented Sep 17, 2018 at 11:37
  • Possible duplicate of Serial communication between Arduino and Matlab is losing data Commented Sep 17, 2018 at 12:58

1 Answer 1

0

are you perhaps using two different baudrates?

ser = serial.Serial('COM3', 115200, timeout = .5)

Serial.begin(9600);
Sign up to request clarification or add additional context in comments.

1 Comment

I really apologize at the time of writing this post I did not pay attention to this, but in the execution of the program yes, and also tried several other values ​​of baud-rate, both without success

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.