繁体   English   中英

打开和关闭 OpenCV window 与 GPIOZero 在 Python

[英]Open and close OpenCV window with GPIOZero in Python

从下周开始,我已经尝试了这 12 种方法,但运气不佳。 我试图在按下按钮时打开 OpenCV window,并在树莓派上释放时关闭。 我对发布代码犹豫不决,只发布了这个示例,因为它实际上做了一些事情。 但是尝试通过按下按钮将打开的 window 部分放入 function 中,并将 killallwindows 部分放入按钮释放 function 中。 还尝试传递一个变量,所以如果你按下按钮 x=1 并且如果 x=1 windows 打开,如果它没有关闭。 到目前为止,我对本应是个人项目中较容易的部分之一没有运气。

这是一些不会崩溃的 Janky 代码……如果我按下按钮,window 会弹出。 我非常感谢一些建议!

我知道我在尝试让按钮至少打开 window 时注释掉了释放按钮部分......任何使用它的尝试都没有顺利进行。

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()

这是同一件事的另一种尝试……它也不起作用。

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()

所以这里有另一个 go ......

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()

按钮在 1 和 0 之间更改 X,但它们没有触发 While 循环以打开 window。

我没有树莓派,但我尝试使用键盘键模拟它

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 用于等待或检测任何键盘键。 当按下键盘上的任何按钮时,cv2.waitkey 将返回一个值,我们在这里所做的是将键盘键与我们想要的值匹配。 对于您的情况,您可以用覆盆子按钮替换。

参考 Ascii 表 Esc - 27 r - 114

"ord" 的操作是将 char("r") 转换为十进制。

您可以参考此以了解更多关于 opencv function 链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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