【发布时间】:2018-09-17 10:58:55
【问题描述】:
我在通过 pyserial 将信息加载到 Arduino 时遇到问题。
我正在使用 python 和 opencv 脚本通过网络摄像头进行颜色检测。对于每一种检测到的颜色,我都想通知 Arduino,以便它做出决定。但我无法将数据发送到 Arduino。
这是我在 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
这是 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 {}
}
}
但是,在 python 中运行程序时,arduino 只会重新启动,并且不会通过串行接收任何数据。有人可以帮我解决这个问题吗?
【问题讨论】:
-
OpenCV 运行在什么机器和操作系统上?它是如何连接到 Arduino 的?
-
open cv 在 windows 10 上运行,它和 python 我正在使用最新的可用版本,arduino 没有连接到 opencv,它通过笔记本的 usb 连接到 python,在行当我在 python 中检测到红色时,我尝试在 arduino 中编写 79 和 80
-
我可以建议你做一个更简单的 Python 程序,它只发送“1”,等待 5 秒然后发送“2”,等待 5 秒发送“3”。如果排除所有 OpenCV 内容,调试会容易得多。
-
但是否可以这样做,以便从检测到的颜色发送此数据?抱歉无知,但我仍在学习使用 python
标签: python opencv image-processing arduino pyserial