Problem loading color detected by Arduin

Asked

Viewed 41 times

0

I have a problem loading the information on Arduino via pyserial.

I’m using a python and opencv script to do color detection via webcam. For each color detected, I want to inform the Arduino so that he can make a decision. But I can’t 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 is the Arduino script:

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 script in python, Arduino only restarts and does not receive data via serial. Someone could help me with this problem?

  • But the program runs normally? Have you tried playing the direct output to the terminal to see if Python is at least generating the expected values?

  • yes the python program works normally, identifies and tracks the specified colors, I haven’t tried, actually I don’t know this command, I will search on and try to apply... Thank you

  • Instead of ser.write, use print and follow the values through the terminal

  • it doesn’t really work, it doesn’t write on the terminal... you would know the reason?

  • 1

    No, so everything indicates that the problem is not with the communication between Python and Arduino, but only in the Python program. Probably some wrong logic/condition. Try running the program with debug turned on to see exactly what is happening.

  • 1

    friend, really the problem was in the program in python, I had to remove the function if (ser.inWaiting() > 0): because she was waiting for a signal from Arduino to release the connection. now works normally... with the debug on I could identify this fault? if yes, how able it?

  • 1

    Read about the library pdb

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.