如何将红绿灯识别结果通过串口发送给arduino小车?
-
import sensor, image,time,lcd, pyb
from pyb import UART,Timer,LEDred_threshold_01 = (60, 88, 23, 63, 6, -25)
green_threshold_01 = (85, 98, -90, 1, 4, -63)
yellow_threshold_01 = (64, 99, -23, 18, 14, 66)sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)#320*240
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((240, 240))#sensor.snapshot().save("/snapshot-%d.jpg" % pyb.rng()) # Save Pic.
sensor.skip_frames()
sensor.set_auto_whitebal(False) #关闭白平衡
sensor.set_auto_gain(False) #关闭自动增益
clock = time.clock()
lcd.init() #Initialize the lcd screen.
uart = UART(3,115200,8,None,1) #创建串口对象data = []
LED_Red = LED(1)
LED_Green = LED(2)
LED_Blue = LED(3)
BLUE_LED_PIN = 3def expand_roi(roi):
# set for QQVGA 160120
extra = 5
win_size = (640, 480)
(x, y, width, height) = roi
new_roi = [x-extra, y-extra, width+2extra, height+2*extra]if new_roi[0] < 0: new_roi[0] = 0 if new_roi[1] < 0: new_roi[1] = 0 if new_roi[2] > win_size[0]: new_roi[2] = win_size[0] if new_roi[3] > win_size[1]: new_roi[3] = win_size[1] return tuple(new_roi)
sensor.skip_frames(time = 2000) # Give the user time to get ready.
tim = Timer(4,freq=1) # create a timer object using timer 4
#tim.callback(tick)
tim.deinit()while(True):
img = sensor.snapshot()
clock.tick() # Track elapsed milliseconds between snapshots().
blobs_red = img.find_blobs([red_threshold_01], area_threshold=150)
blobs_green = img.find_blobs([green_threshold_01], area_threshold=150)
blobs_yellow = img.find_blobs([yellow_threshold_01], area_threshold=150)if blobs_yellow: #如果找到了目标颜色 #print(blobs) print("黄灯") #numjtdy = numjtdy+1 for blob in blobs_yellow: #迭代找到的目标颜色区域 is_circle = False max_circle = None max_radius = -1 new_roi = expand_roi(blob.rect()) for c in img.find_circles(threshold = 2000, x_margin = 20, y_margin = 20, r_margin = 10, roi=new_roi): is_circle = True # img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255)) if c.r() > max_radius: max_radius = c.r() max_circle = c if is_circle: # 如果有对应颜色的圆形 标记外框 # Draw a rect around the blob. img.draw_rectangle(new_roi) # rect img.draw_rectangle(blob.rect()) # rect #用矩形标记出目标颜色区域 img.draw_cross(blob[5], blob[6]) # cx, cy img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r(), color = (0, 255, 0)) img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r() + 1, color = (0, 255, 0)) #sensor.snapshot().save("example.jpg") # or "example.bmp" (or others) #pyb.LED(BLUE_LED_PIN).off() #sensor.skip_frames(time = 2000) # Give the user time to get ready. if blobs_green: #如果找到了目标颜色 #print(blobs) print("绿灯") #numjtdg = numjtdg+1 for blob in blobs_green: #迭代找到的目标颜色区域 is_circle = False max_circle = None max_radius = -1 new_roi = expand_roi(blob.rect()) for c in img.find_circles(threshold = 2000, x_margin = 20, y_margin = 20, r_margin = 10, roi=new_roi): is_circle = True # img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255)) if c.r() > max_radius: max_radius = c.r() max_circle = c if is_circle: # 如果有对应颜色的圆形 标记外框 # Draw a rect around the blob. img.draw_rectangle(new_roi) # rect img.draw_rectangle(blob.rect()) # rect #用矩形标记出目标颜色区域 img.draw_cross(blob[5], blob[6]) # cx, cy img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r(), color = (0, 255, 0)) img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r() + 1, color = (0, 255, 0)) if blobs_red: #如果找到了目标颜色 #print(blobs) print("红灯") #numjtdr = numjtdr+1 for blob in blobs_red: #迭代找到的目标颜色区域 is_circle = False max_circle = None max_radius = -1 new_roi = expand_roi(blob.rect()) for c in img.find_circles(threshold = 2000, x_margin = 20, y_margin = 20, r_margin = 10, roi=new_roi): is_circle = True # img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255)) if c.r() > max_radius: max_radius = c.r() max_circle = c if is_circle: # 如果有对应颜色的圆形 标记外框 # Draw a rect around the blob. img.draw_rectangle(new_roi) # rect img.draw_rectangle(blob.rect()) # rect #用矩形标记出目标颜色区域 img.draw_cross(blob[5], blob[6]) # cx, cy img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r(), color = (0, 255, 0)) img.draw_circle(max_circle.x(), max_circle.y(), max_circle.r() + 1, color = (0, 255, 0)) lcd.display(img)
-
现在这个可以识别出结果,主要是通过串口发送
-
-
视频看过了但是跟小车匹配发送数据就难的多了,二维码识别后通过串口发送给小车也多了好多。
import sensor, image,time,lcd from pyb import UART,Timer,LED sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA)#320*240 sensor.skip_frames() sensor.set_auto_whitebal(False) #关闭白平衡 sensor.set_auto_gain(False) #关闭自动增益 clock = time.clock() lcd.init() #Initialize the lcd screen. uart = UART(3,115200,8,None,1) #创建串口对象 data = [] LED_Red = LED(1) LED_Green = LED(2) LED_Blue = LED(3) tim = Timer(4,freq=1) # create a timer object using timer 4 #tim.callback(tick) tim.deinit() FlagOK = 0 show_numTab = ["0","1","2","3","4","5","6","7","8","9"] num = 0 returnData = [0x55,0x02,0x92,0x02,0x02,0x00,0x00,0xBB] #识别失败 runData = [0x55,0x02,0x92,0x03,0x02,0x00,0x00,0xBB] #正在识别 #定时器回调函数 def tick(timer): # we will receive the timer object when being called global FlagOK,num,returnData print("Timer callback") num = num-1 if(num == 0): num = 9 FlagOK = 2 tim.deinit() #串口发送函数 def USART_Send(src,length): for i in range(length): uart.writechar(src[i]) # 二维码识别,并返回识别结果 def Color_Check(srcbuf): global FlagOK,num if(FlagOK == 1): img.draw_string(100, 180,"open"+show_numTab[num],color=[255,0,0]) for code in img.find_qrcodes(): FlagOK = 0 tim.deinit() print(code) qr_Tab = code.payload() uart.writechar(0x55) uart.writechar(0x02) uart.writechar(0x92) uart.writechar(0x01) uart.writechar(len(qr_Tab)) for qrdata in qr_Tab: uart.writechar(ord(qrdata)) uart.writechar(0xBB) if(FlagOK == 2): for rdata in returnData: uart.writechar(rdata) FlagOK = 0 while(True): img = sensor.snapshot() if(uart.any()): data = uart.read(8) if( len(data) >= 8): if((data[0] == 0x55)&(data[1] == 0x02)&(data[7] == 0xBB)): if(data[2] == 0x91): print("识别小球") if(data[2] == 0x92): print("识别二维码") if(data[3] == 0x01): #启动识别 if(FlagOK == 0): FlagOK = 1 num = 9 print("开始识别") tim.callback(tick) else: print("正在识别") for rdata in runData: print(rdata) uart.writechar(rdata) if(data[3] == 0x02): print("停止识别") FlagOK = 2 tim.deinit() #定时器停止 Color_Check(data) img.draw_string(110, 40,"qr_CodeV1.0",color=[0,0,255]) lcd.display(img) 这个是二维码通信的
-
我看你的代码不只是发送数据,还有读取数据。
这种好几百行逻辑复杂,测试复杂的代码,我帮不了。尤其是还有通信协议同步的问题。
-
你好通信协议的同步问题解决了,现在是如何才能将它识别出的颜色结果,发送端应该怎么去写.
-
@ty3p 视频里有发送字符串的例子,你直接生成字符串就行了。