0

I am trying to compute the difference between two images with OpenCV on Raspberry Pi and I want the script to run continually like in a while loop and then after the first button press store the first image then wait for the second and store the second image and once that has happened it should run the function to compute the difference. I am using gpiozero and I've tried using a while loop as well as some of the gpiozero functions but the script doesn't wait correctly.

What I've tried:

def capture():
    # create flag to track second button click
    secondClick = False
    # take the first image
    image1 = capture_stream(True)

    while secondClick == False:
        # pauses the script until button is pressed again
        button.wait_for_press(timeout=None)
        secondClick = True

    # take second picture and compute
    image2 = capture_stream(True)
    diff = compute_diff(image1, image2)
    insert(diff)
    print("Structural difference: %s %%" % (diff*100))

button.when_pressed = capture

print("running...")

pause()

I've tried something similar within a while loop with some if statements on the number of pictures already taken but I feel like the while loop method is messy.

Let me know if I can provide any aditional information and thank you for your time.

1

Debugging can be frustrating--it's amazing how many ways something that should be so simple could break!

Let's let go of the goal here a bit and try to understand what your code is actually doing. The following sounds horrendous but is actually effective at finding out what's going on without a debugger:

1) Add print() statements before each line in capture(). E.g., print("Debug #1"), etc. 2) Comment out every single OpenCV line (e.g., "#TODO: existing line")

Now when you run your program it will execute quickly and respond to button clicks. It will also print out a trail of debug statements that you can follow and match to your assumptions.

I think you'll have some surprises when you do this. I always do.

For example, you might find that it calls capture() on every click, not just the first. Try the print statements first though.

  • 1
    Well capture() does run on every button press, but instead of waiting for the second button press it just immediately takes the second picture. I will add some prints and follow up to see if I've gotten anywhere though. Thank you. – Justin Jun 8 '18 at 5:13
1

This is the script I came up with the solve this problem:

def main():
picture_count = 0
print("picture count ", picture_count)
while True:
    global picture_count
    print("running...")
    # wait for the first picture
    button.wait_for_press(timeout=None)
    # take the irst picture after button pressed
    image1 = capture()
    print("picture count ", picture_count)
    # increment picture count
    picture_count = picture_count + 1
    print("Waiting for second picture")
    # wait for second picture
    button.wait_for_press(timeout=None)
    # take the second image
    image2 = capture()
    print("picture count ", picture_count)
    # increment picture count
    picture_count = picture_count + 1
    # when two pictures have been taken, compute the difference 
    if(picture_count == 2):
        # reset the picture count
        picture_count = 0
        # compute the difference
        diff = compute_diff(image1, image2)
        # insert the difference into the database
        insert(diff)
        print("Structural difference: %s %%" % (diff*100))
  • Awesome! Looks like you took out the button.when_pressed which made the original code go wacko. Great job! Go ahead and accept your own answer if it worked for you. :D – OyaMist Jun 9 '18 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.