# Blob Detection Example
#
# This example shows off how to use the find_blobs function to find color
# blobs in the image. This example in particular looks for dark green objects.
import sensor, image, time, pyb
from pyb import UART
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
#sensor.set_windowing((100, 100))
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
green_threshold = (54, 95, -4, 30, 22, 70)
size_threshold = 1500
blue_led = pyb.LED(3)
green_led = pyb.LED(2)
red_led = pyb.LED(1)
uart = UART(3, 9600)
sensor.set_auto_exposure(True, exposure_us=5000) # make smaller to go faster
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
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([green_threshold])
#FZ = bytearray([0x55,0x55,0x12,0x03,0x03,0xd0,0x07,0x01,0xa1,0x02,0x02,0x04,0x05,0x03,0x6c,0x06])
#FZ1 = bytearray([0x55,0x55,0x14,0x03,0x05,0xd0,0x07,0x01,0x05,0x09,0x02,0x4c,0x05,0x03,0xe9,0x02,0x04,0xc1,0x07,0x05,0x24,0x02])
#FZ1 = bytearray([0x55,0x55,0x08,0x03,0x01,0xd0,0x07,0x01,0x05,0x09])
#uart.write(FZ1)
#FZ2 = bytearray([0x55,0x55,0x08,0x03,0x02,0xd0,0x07,0x02,0x4c,0x05])
#uart.write(FZ2)
#time.sleep_ms(1000)
#FZ3 = bytearray([0x55,0x55,0x08,0x03,0x03,0xd0,0x07,0x03,0xe9,0x02])
#uart.write(FZ3)
#time.sleep_ms(1000)
#FZ4 = bytearray([0x55,0x55,0x08,0x03,0x04,0xd0,0x07,0x04,0xc1,0x07])
#uart.write(FZ)
time.sleep_ms(1000)
xiangshu=0
if blobs:
max_blob = find_max(blobs)
max_blob1=find_max1(blobs)
xiangshu = max_blob.blob[4]
img.draw_rectangle(max_blob[0:4]) # rect
img.draw_cross(max_blob[5], max_blob[6]) # cx, cy
if xiangshu > size_threshold:
#FZ = bytearray([0x55,0x55,0x05,0x06,0x64,0x01,0x00])
#art.write(FZ)
red_led.on()
time.sleep_ms(150)#延时150ms
red_led.off()
else:
blue_led.on()
time.sleep_ms(150) #延时150ms
blue_led.off()
else:
green_led.on()
time.sleep_ms(150) #延时150ms
green_led.off()把time.sleep都删掉。延时了当然低。