I am new Python and I am trying to find a tutorial on how to use the if statement. This is what I am attempting to accomplish.
I am using the blob_detection tutorial as a basis to detect red, blue or green and turn the LED for each respective color. As you can see in the code, I couldn’t make it work with the if esle, so I had to copy the same code multiple times. Any help would be appreciated.
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([red_threshold])#, blue_threshold, green_threshold])
if blobs:
red_led.on()
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
print("Red color detected!")
red_led.off()
#break
blobs = img.find_blobs([blue_threshold])#, blue_threshold, green_threshold])
if blobs:
blue_led.on()
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
print("Blue color detected!")
blue_led.off()
# break
blobs = img.find_blobs([green_threshold])#, blue_threshold, green_threshold])
if blobs:
green_led.on()
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
print("Green color detected!")
green_led.off()
Sure, I added the entire code under the CODE tab. I do understand C, however, I am not familiar at all with Python and I’m having difficulty formatting it.
# 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
from pyb import LED
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
ir_led = LED(4)
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
red_threshold = ( 40, 60, 60, 90, 50, 70)
blue_threshold = ( 0, 20, -10, 30, -60, 10)
green_threshold = ( 0, 80, -70, -10, -0, 30)
# 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.[b][/b]
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
red_led.off()
blue_led.off()
green_led.off()
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([red_threshold])#, blue_threshold, green_threshold])
if blobs:
red_led.on()
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
print("Red color detected!")
red_led.off()
#break
blobs = img.find_blobs([blue_threshold])#, blue_threshold, green_threshold])
if blobs:
blue_led.on()
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
print("Blue color detected!")
blue_led.off()
# break
blobs = img.find_blobs([green_threshold])#, blue_threshold, green_threshold])
if blobs:
green_led.on()
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
print("Green color detected!")
green_led.off()
# break
# elif blobs == red_threshold: red_led.off()
# print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([red_threshold])#, blue_threshold, green_threshold])
if (blobs == red_threshold):
red_led.on()
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
print("Red color detected!")
red_led.off()
#break
blobs = img.find_blobs([blue_threshold])#, blue_threshold, green_threshold])
else (blobs == blue_threshold):
blue_led.on()
Oh, so, you can’t have Anything in between the if and else. You need to keep those things on the same indentation level and all statements have to be indentation past them.
@hvtronix , if you have Python-specific questions, the net is full of Python tutorials and Google is your best friend. Of course, http://www.python.org is a good place to start. Or search StackOverflow. Although In many cases, the quickest way to learn and figure out how things work in Python is simply install Python on your computer and try things out using the interactive interpreter.