openmv识别红绿灯并返回识别结果给arduino的python文件,并写一个相对应的arduino的接受程序
时间: 2023-05-31 12:05:56 浏览: 145
openmv神经网络 红绿灯识别
OpenMV识别红绿灯并返回识别结果给Arduino的Python文件:
```python
import sensor
import image
import time
import pyb
# 初始化串口通信
uart = pyb.UART(3, 9600)
uart.init(9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=1000)
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
# 初始化颜色追踪
red_threshold = (30, 100, 15, 127, 15, 127) #调试参数
green_threshold = (30, 100, -64, -8, -32, 32) #调试参数
color = "" # 红灯或绿灯
while(True):
img = sensor.snapshot()
blobs_red = img.find_blobs([red_threshold])
blobs_green = img.find_blobs([green_threshold])
if blobs_red:
uart.write("red")
color = "red"
elif blobs_green:
uart.write("green")
color = "green"
else:
uart.write("unknown")
color = "unknown"
time.sleep(100) # 间隔100ms发送一次识别结果
```
相对应的Arduino接受程序:
```c++
String result = "";
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
result = Serial.readString();
Serial.println(result);
}
}
```
阅读全文