从下周开始,我已经尝试了12种方法,但运气不佳。我试图在按下按钮时打开OpenCV窗口,然后在树莓派上释放时关闭。我很犹豫要发布代码,仅此示例,因为它实际上可以执行某些操作。但是尝试过按下按钮将打开的窗口部分放入功能中,而将按钮释放功能的killallwindows部分放入其中。 HA还尝试传递变量,因此如果您按下按钮x = 1,并且如果x = 1,则窗口将打开,如果没有,则窗口将关闭。到目前为止,我对个人项目中比较容易的部分还算不上运气。
这里有一些Janky代码不会崩溃...如果按下按钮,窗口确实会弹出。我将不胜感激一些建议!
我知道我在尝试释放按钮以至少打开一个窗口的过程中注释掉了释放按钮部分...任何使用它的尝试都没有顺利进行。
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()
所以这是另一回事...
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()
按钮将X更改为1到0,但它们不会触发While循环来打开窗口。
答案 0 :(得分:0)
我没有树莓派,但我尝试使用键盘键对其进行模仿
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