简体   繁体   中英

Open and close OpenCV window with GPIOZero in Python

I've tried this 12 ways from next week without much luck. I'm trying to make an OpenCV window open when I press a button, and close on release on a raspberry pi. I'm hesitant to post code, and only this example because it actually does something. But have tried putting the open window portions in a function with the button press, and the killallwindows portion in the button release function. HAve also tried passing a variable so if you press the button x=1 and if x=1 the windows opens and if it doesn't it closes. So far I've had no luck with what was supposed to be one of the easier parts of a personal project.

Here's some Janky code that doesn't crash...the window does pop open if I press the button. I'd greatly appreciate some advice!

I am aware I commented out the release button portion while trying to get the button to at least open a window...any attempt at using it has not gone well.

import cv2
from gpiozero import Button 
from signal import pause
from time import sleep

cap = cv2.VideoCapture(0)
buttonFocus = Button(14)

def FocusPeakingStart(): 
    while(True):
       ret, frame = cap.read()
       cv2.imshow('frame', frame)
       if cv2.waitKey(1) & 0xFF ==('q'):
           break
    cap.release()
    cv2.destroyAllWindows()

    
#def FocusPeakingStop(): 
#    print("FocusPeaking Stop") 

buttonFocus.when_pressed = FocusPeakingStart
#buttonFocus.when_released = FocusPeakingStop

pause()

Here's another attempt at the same thing...it doesn't work either.

import cv2
from gpiozero import Button 
from signal import pause
from time import sleep

cap = cv2.VideoCapture(0)
buttonFocus = Button(14)
x=1

while(x == 0):
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if x == 1 & 0xFF ==('q'):
        break
    cap.release()
    cv2.destroyAllWindows()
    if buttonFocus.when_pressed:
        x == 0
    if buttonFocus.when_released:
        x == 1

pause()

So here's another go at things...

import cv2
from gpiozero import Button 
from time import sleep
from signal import pause

cap = cv2.VideoCapture(0)
x=0  #if button press set the X to 1(true) to start your while loop
buttonFocus = Button(27)

def FocusPeakingStart():
    print("Focus Down")
    global x
    x=1
    print(x)
def FocusPeakingStop():
    print("Focus Up")
    global x
    x=0
    print(x)

while(x):
    ret, frame = cap.read()
    cv2.imshow('image',frame)
    k = cv2.waitKey(1) & 0xFF
    if k == ord("r"):
        continue #continue when "r" press on keyboard
    elif k == 27:
        break #break the loop if the button stop press
cv2.destroyAllWindows()

buttonFocus.when_pressed = FocusPeakingStart
buttonFocus.when_released = FocusPeakingStop

pause()

The buttons are changing X between 1 and 0, BUT they're not triggering the While loop to open the window.

I dont have raspberry pi, but i try emulate it using keyboard key

import cv2
from time import sleep

cap = cv2.VideoCapture(0)
x=1  #if button press set the X to 1(true) to start your while loop

while(x):
    ret, frame = cap.read()
    cv2.imshow('image',frame)
    k = cv2.waitKey(1) & 0xFF
    if k == ord("r"):
        continue #continue when "r" press on keyboard
    elif k == 27:
        break #break the loop if the button stop press
cv2.destroyAllWindows()

cv2.waitkey is used to wait or detect any keyboard key. cv2.waitkey will return a value when any button press on keyboard, what we do here is to match the keyboard key with the value we want. For your case you can replace with raspberry button.

Refer to Ascii Table Esc - 27 r - 114

The operation of "ord"is to convert char("r") to decimal.

You can refer to this to understand more on opencv function link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM