在问下thresholds = [(32, 48, 54, 70, 31, 59),
(28, 71, -128, -24, 13, 42),
(38, 62, -7, 31, -79, -41)]如何按顺序去识别呢,比如先识别红,没有识别到则等待,有就继续,绿以此类推
https://forum.singtown.com/topic/1735/%E5%A6%82%E4%BD%95%E8%AF%86%E5%88%AB%E4%B8%89%E7%A7%8D%E9%A2%9C%E8%89%B2-%E8%80%8C%E4%B8%94%E8%83%BD%E5%A4%9F%E5%88%86%E5%88%AB%E8%AE%B0%E4%B8%8B%E5%9D%90%E6%A0%87
@kidswong999 需要判断颜色的code()吗
@13542749802 不需要
@kidswong999
import sensor, image, time,pyb
from pyb import UART
#初始化串口3
uart = UART(3,115200)
uart.init(115200,bits = 8,parity = None,stop = 1)
#初始化RGB
red_led = pyb.LED(1)
grean_led = pyb.LED(2)
blue_led = pyb.LED(3)
#关闭RGB灯
red_led.off()
grean_led.off()
blue_led.off()
red_color = (32, 48, 54, 70, 31, 59) # 一般情况下的红色阈值
grean_color = (42, 71, -63, -29, 16, 42) # 一般情况下的绿色阈值
blue_color = (38, 62, -7, 31, -79, -41) # 一般情况下的蓝色阈值
# 标记当前识别的颜色
def markCurColor(img, cName, cRgb):
img.draw_string(0, 0, cName, color = cRgb)
# 如果是红色,执行此函数
def doWithRed(img):
markCurColor(img, "RED", (255, 0, 0))
red_led.on()
grean_led.off()
blue_led.off()
uart.write("red_ok\r\n")
# 如果是绿色,执行此函数
def doWithGreen(img):
markCurColor(img, "GREEN", (0, 255, 0))
grean_led.on()
red_led.off()
blue_led.off()
uart.write("grean_ok\r\n")
# 如果是绿色,执行此函数
def doWithBlue(img):
markCurColor(img, "BLUE", (0, 0, 255))
blue_led.on()
red_led.off()
grean_led.off()
uart.write("blue_ok\r\n")
# 比较两个色块大小的函数
def compareBlob(blob1, blob2):
tmp = blob1.pixels() - blob2.pixels()
if tmp == 0:
return 0;
elif tmp > 0:
return 1;
else:
return -1;
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
def fun():
while(True):
if uart.any():
uart_buf = uart.readline()
print(uart_buf)
if uart_buf == b'0x01':
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
sensor.skip_frames(time = 2000)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
red_blob = img.find_blobs([red_color], pixels_threshold=200, area_threshold=200, merge=True)
if red_blob:
max_size=0
for blob in red_blob:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithRed(img)
else:
blue_led.off()
red_led.off()
grean_led.off()
grean_blob = img.find_blobs([grean_color], pixels_threshold=200, area_threshold=200, merge=True)
if grean_blob:
max_size=0
for blob in grean_blob:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithGreen(img)
else:
blue_led.off()
red_led.off()
grean_led.off()
blue_blob = img.find_blobs([blue_color], pixels_threshold=200, area_threshold=200, merge=True)
if blue_blob:
max_size=0
for blob in blue_blob:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithBlue(img)
else:
blue_led.off()
red_led.off()
grean_led.off()
if uart_buf == b'0x02':
pase
if __name__=='__main__':
fun()按你那样,为什么我这里的绿色就识别不到了,但是只判断绿色的话就识别的到
@13542749802 你的代码有两个死循环