if else statement

General discussion about topics related to OpenMV.
hvtronix
Posts: 9
Joined: Mon Aug 22, 2016 8:08 pm

if else statement

Postby hvtronix » Wed Nov 23, 2016 9:17 am

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.

Code: Select all

    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()
Last edited by iabdalkader on Wed Nov 23, 2016 11:29 am, edited 1 time in total.
Reason: add code tag
User avatar
kwagyeman
Posts: 2118
Joined: Sun May 24, 2015 2:10 pm

Re: if else statement

Postby kwagyeman » Wed Nov 23, 2016 11:13 am

Hi, can you put the code in the code tag? Python's formatting is important.

As for if else. You do:

if():
else():

And you have to put the code under those cases indented.

The stuff between () is just a regular boolean statement like in C.
Nyamekye,
hvtronix
Posts: 9
Joined: Mon Aug 22, 2016 8:08 pm

Re: if else statement

Postby hvtronix » Wed Nov 23, 2016 11:35 am

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.

Code: Select all

# 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.
hvtronix
Posts: 9
Joined: Mon Aug 22, 2016 8:08 pm

Re: if else statement

Postby hvtronix » Wed Nov 23, 2016 11:40 am

I also tried this

Code: Select all

   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()
User avatar
kwagyeman
Posts: 2118
Joined: Sun May 24, 2015 2:10 pm

Re: if else statement

Postby kwagyeman » Wed Nov 23, 2016 2:17 pm

Ibrahim will have to look at this. My phone is wrapping lines. On Thanksgiving vacation.

Um, what's the error message you are getting?
Nyamekye,
User avatar
kwagyeman
Posts: 2118
Joined: Sun May 24, 2015 2:10 pm

Re: if else statement

Postby kwagyeman » Wed Nov 23, 2016 2:19 pm

Oh, so, python doesn't support else with a statement. You have to do elif():

Else(): is only for the last statement without anything.
Nyamekye,
User avatar
kwagyeman
Posts: 2118
Joined: Sun May 24, 2015 2:10 pm

Re: if else statement

Postby kwagyeman » Wed Nov 23, 2016 2:21 pm

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.
Nyamekye,
User avatar
petri
Posts: 15
Joined: Tue Jul 05, 2016 11:52 am
Location: Finland

Re: if else statement

Postby petri » Thu Nov 24, 2016 2:04 am

@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.
hvtronix
Posts: 9
Joined: Mon Aug 22, 2016 8:08 pm

Re: if else statement

Postby hvtronix » Mon Nov 28, 2016 10:26 am

You guys are awesome. I installed Python and I'm going over the tutorials (I was approaching Python like if it was C or C++). Once again, thanks!

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 6 guests