首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python语言中使用GPIOZero打开和关闭OpenCV窗口

在Python语言中使用GPIOZero打开和关闭OpenCV窗口
EN

Stack Overflow用户
提问于 2020-08-19 05:48:54
回答 1查看 184关注 0票数 0

从下周开始,我已经尝试了12种方法,但没有太多运气。我正在尝试让一个OpenCV窗口在我按下一个按钮时打开,而在一个树莓派上释放时关闭。我很犹豫要不要发布代码,只是这个例子,因为它确实做了一些事情。但是已经尝试将打开的窗口部分放在具有按钮按下的功能中,并且将with _ window部分放在按钮释放功能中。HAve还尝试传递一个变量,所以如果您按下按钮x=1,如果按下x=1,窗口将打开,如果不按,窗口将关闭。到目前为止,我在个人项目中被认为是更容易的部分还没有成功。

下面是一些Janky代码,如果我按下按钮,crash...the窗口就会弹出。如果能给我一些建议,我将不胜感激!

我意识到我注释掉了释放按钮部分,虽然我试图让按钮至少打开一个window...any,但使用它的尝试并不顺利。

代码语言:javascript
复制
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()

这是另一次尝试相同的thing...it也不起作用。

代码语言:javascript
复制
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()

所以这是另一种尝试...

代码语言:javascript
复制
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循环来打开窗口。

EN

回答 1

Stack Overflow用户

发布于 2020-08-19 09:39:57

我没有raspberry pi,但我尝试使用键盘键来模拟它

代码语言:javascript
复制
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函数link的更多信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63477109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档