请在这里粘贴代码
```import sensor, image, time, pyb
from pyb import UART
uart = UART(3, 9600)
red_led = pyb.LED(1)
yellow_led = pyb.LED(2)
blue_led = pyb.LED(3)
red_led.off()
yellow_led.off()
blue_led.off()
yellow_threshold = (40, 85, -20, 5, 15, 75)
red_threshold = (20, 70, 25, 85, 15, 70)
black_threshold = (0, 10, -10, 10, -10, 10)
sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
sensor.set_framesize(sensor.QQVGA) # 使用 QQVGA 速度快一些
sensor.skip_frames(10) # 跳过10帧,使新设置生效
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
#关闭白平衡。白平衡是默认开启的,在颜色识别中,一定要关闭白平衡。
clock = time.clock() # 追踪帧率
yellow_num = 0
yellow_x = 0
yellow_y = 0
red_num = 0
red_x = 0
red_y = 0
black_x = 0
black_y = 0
data = 0
redata = 0
e = 0
f = 0
while(0):
redata = uart.readchar()
print(redata)
if redata ==1:
break
while(True):
clock.tick()
img = sensor.snapshot() # 从感光芯片获得一张图像
blobs_2 = img.find_blobs([red_threshold],pixels_threshold=20)
if blobs_2:
red_led.on()
for i in range(len(blobs_2)):
red_num = blobs_2[i].pixels()
if red_num>65:
blue_led.on()
red_x = blobs_2[i][5]-80
red_y = blobs_2[i][6]-60
if red_x>5:
data |=0x08
if red_x <-5:
data |=0x04
if red_y>5:
data |=0x02
if red_y<-5:
data |=0x01
uart.writechar(data)
data = 0
e = 1
else:
blue_led.off()
uart.writechar(0)
e = 0
for j in blobs_2:
img.draw_rectangle(j[0:4])
img.draw_cross(j[5], j[6])
else:
red_led.off()
blue_led.off()
uart.writechar(0)
e = 0
if e == 0:
blobs_1 = img.find_blobs([yellow_threshold],pixels_threshold=20)
if blobs_1:
yellow_led.on()
for i in range(len(blobs_1)):
yellow_num = blobs_1[i].pixels()
if yellow_num>120:
blue_led.on()
yellow_x = blobs_1[i][5]-80
yellow_y = blobs_1[i][6]-60
if ((yellow_x>0)+(yellow_x<35)) == 2:
data |=0x04
if ((yellow_x<0)+(yellow_x>-35)) == 2:
data |=0x08
#if ((yellow_y>0)+(yellow_y<15)) == 2:
# data |=0x01
#if ((yellow_y<0)+(yellow_y>-15)) == 2:
# data |=0x02
uart.writechar(data)
data = 0
f = 1
else:
blue_led.off()
uart.writechar(0)
f = 0
for j in blobs_1:
img.draw_rectangle(j[0:4])
img.draw_cross(j[5], j[6])
else:
yellow_led.off()
blue_led.off()
uart.writechar(0)
f = 0
else:
yellow_led.off()
if e == 0:
if f == 0:
blobs_3 = img.find_blobs([black_threshold],pixels_threshold=120)
if blobs_3:
most_pixels = 0
largest_blob = 0
for i in range(len(blobs_3)):
if blobs_3[i].pixels() > most_pixels:
most_pixels = blobs_3[i].pixels()
largest_blob = i
img.draw_rectangle(blobs_3[largest_blob][0:4])
img.draw_cross(blobs_3[largest_blob][5],blobs_3[largest_blob][6])
black_x = blobs_3[largest_blob][5]-80
black_y = blobs_3[largest_blob][6]-60
if black_x>9:
data |=0x08
if black_x<-9:
data |=0x04
if black_y>9:
data |=0x02
if black_y<-9:
data |=0x01
uart.writechar(data)
data = 0
6
6xpz
@6xpz
0
声望
1
楼层
302
资料浏览
0
粉丝
0
关注
6xpz 发布的帖子
-
mv程序可以正常下载,但是连接到32板 mv没有任何反应(在电脑上可以正常运行程序)qq