Thanks for the start. The right-click tip was very very helpful.
At first I made revisions to only ‘find’ for a single color at a time. It drew a nice rectangle, calculated elapsed time from request-to-look-for-color to the time-color-was-seen.
But as I add more stubs for interfacing to input pins and output pins, I stopped getting the annotated graphics in the IDE frame buffer display. I suspect I’m tired or I am about to learn about some command to update a stale frame buffer.
My intent is to look for each color for 10 seconds, and display in the frame buffer if color-not-found after 10 seconds or what-was-elapsed-time to find the color. The print output looks OK, the frame buffer looks bad.
Here is my current code. I’m keen to learn why the draw commands no longer work or blink super quick when they should persist during the delay(500).
# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.
import sensor, image, time
from pyb import LED
import pyb
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
ir_led = LED(4)
def led_control(x):
if (x&1)==0: red_led.off()
elif (x&1)==1: red_led.on()
if (x&2)==0: green_led.off()
elif (x&2)==2: green_led.on()
if (x&4)==0: blue_led.off()
elif (x&4)==4: blue_led.on()
if (x&8)==0: ir_led.off()
elif (x&8)==8: ir_led.on()
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [( 22, 64, 18, 127, -32, 43), # generic_red_thresholds (code 1)
( 25, 100, -69, -25, -42, 26), # generic_green_thresholds (code 2)
( 5, 45, -101, 21, -97, -12)] # generic_blue_thresholds (code 4)
thresholdRed = [( 22, 64, 18, 127, -32, 43)] # generic_green_thresholds (code 1)
thresholdGreen = [( 25, 100, -69, -25, -42, 26)] # generic_green_thresholds (code 1)
thresholdBlue = [( 5, 45, -101, 21, -97, -12)] # generic_blue_thresholds (code 1)
# You may pass up to 16 thresholds above. However, it's not really possible to segment any
# scene with 16 thresholds before color thresholds start to overlap heavily.
# See the blob documentation about codes. They are basically bit masks for the above
# color positions in the list of colors.
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.
# define input pin look red
# define input pin look green
# define input pin look blue
# define output pin found red
# define output pin found green
# define output pin found blue
lookRed = False
lookGreen = False
lookBlue = False
foundRed = False
foundGreen = False
foundBlue = False
oldLookRed = False
oldLookGreen = False
oldLookBlue = False
start = pyb.millis()
red_bool = 0
green_bool = 0
blue_bool = 0
fauxStart = pyb.millis()
oneOfThree = 0
elapsed = 0 # from request to look to time 1 of 3 colors found
print ("")
print ("Look for only 1 color based on HIGH input pin")
while(True):
clock.tick() # learn what a clock.tick does
img = sensor.snapshot()
#lookRed = pyb.Pin(pyb.Pin.board.P1, pyb.Pin.IN) # which color to look for
#lookGreen = pyb.Pin(pyb.Pin.board.P2, pyb.Pin.IN)
#lookBlue = pyb.Pin(pyb.Pin.board.P3, pyb.Pin.IN)
if (pyb.elapsed_millis(start) > 10000): # fake the look signal from main arduino
if ((red_bool == 0) and (lookRed == True)):
print ("NO RED")
img.draw_string(20, 50, "NO RED", color=(255,0,0), scale =4, mono_space=True )
pyb.delay(500)
if ((green_bool == 0) and (lookGreen == True)):
print ("NO GREEN")
img.draw_string(20, 50, "NO GREEN", color=(0,255,0), scale =4, mono_space=True )
pyb.delay(500)
if ((blue_bool == 0) and (lookBlue == True)):
print ("NO BLUE")
img.draw_string(20, 50, "NO BLUE", color=(0,0,255), scale =4, mono_space=True )
pyb.delay(500)
oneOfThree = oneOfThree + 1 # fake the look signal from main arduino
if (oneOfThree == 1) :
lookRed = True
lookGreen = False
lookBlue = False
start = pyb.millis()
print (start)
print ("RED")
if (oneOfThree == 2):
lookGreen = True
lookRed = False
lookBlue = False
start = pyb.millis()
print (start)
print ("GREEN")
if (oneOfThree == 3):
oneOfThree = 0
lookBlue = True
lookRed = False
lookGreen = False
start = pyb.millis()
print (start)
print ("BLUE")
oldLookRed = lookRed # save I may need to know if this is a new-look or old-look
oldLookGreen = lookGreen
oldLookBlue = lookBlue
if lookRed: # if look for RED PIN HIGH
clock.tick()
img = sensor.snapshot()
blob_list = img.find_blobs(thresholdRed, x_stride=10, y_stride=10, pixels_threshold=20, area_threshold=1500)
red_bool = 0
green_bool = 0
blue_bool = 0
for b in blob_list:
if b.code() & 1: # red blob
red_bool = 1
img.draw_rectangle(b.rect(), color=(255,0,0), thickness=5)
img.draw_cross(b.cx(), b.cy(), color=(255,0,0), thickness=5)
foundRed = True
print ("RED FOUND")
elapsed = pyb.elapsed_millis(start)
print (elapsed)
img.draw_string(20, 50, str(elapsed), color=(255,0,0), scale =4, mono_space=True )
lookRed = False
#pyb.delay(5000)
#print ("")
#pyb.delay(500)
#latch the RED FOUND PIN HIGH
if lookGreen: # look for GREEN PIN HIGH
clock.tick()
img = sensor.snapshot()
blob_list = img.find_blobs(thresholdGreen, x_stride=10, y_stride=10, pixels_threshold=20, area_threshold=1500)
red_bool = 0
green_bool = 0
blue_bool = 0
for b in blob_list:
if b.code() & 1: # green blob
green_bool = 1
img.draw_rectangle(b.rect(), color=(0,255,0), thickness=5)
img.draw_cross(b.cx(), b.cy(), color=(0,255,0), thickness=5)
foundGreen = True
print ("GREEN FOUND")
elapsed = pyb.elapsed_millis(start)
print (elapsed)
img.draw_string(20, 50, str(elapsed), color=(0,255,0), scale =4, mono_space=True )
lookGreen = False
#pyb.delay(5000)
#print ("")
#pyb.delay(500)
#latch thr GREEN FOUND PIN HIGH
if lookBlue: #look for BLUE PIN HIGH
clock.tick()
img = sensor.snapshot()
blob_list = img.find_blobs(thresholdBlue, x_stride=10, y_stride=10, pixels_threshold=20, area_threshold=1500)
red_bool = 0
green_bool = 0
blue_bool = 0
for b in blob_list:
if b.code() & 1: # blue blob
blue_bool = 1
img.draw_rectangle(b.rect(), color=(0,0,255), thickness=5)
img.draw_cross(b.cx(), b.cy(), color=(0,0,255), thickness=5)
foundBlue = True
print ("BLUE FOUND")
elapsed = pyb.elapsed_millis(start)
print (elapsed)
img.draw_string(20, 50, str(elapsed), color=(0,0,255), scale =4, mono_space=True )
lookBlue = False
#pyb.delay(5000)
#print ("")
#pyb.delay(500)
# latch the BLUE found PIN HIGH