从下周开始,我已经尝试了12种方法,但没有太多运气。我正在尝试让一个OpenCV窗口在我按下一个按钮时打开,而在一个树莓派上释放时关闭。我很犹豫要不要发布代码,只是这个例子,因为它确实做了一些事情。但是已经尝试将打开的窗口部分放在具有按钮按下的功能中,并且将with _ window部分放在按钮释放功能中。HAve还尝试传递一个变量,所以如果您按下按钮x=1,如果按下x=1,窗口将打开,如果不按,窗口将关闭。到目前为止,我在个人项目中被认为是更容易的部分还没有成功。
下面是一些Janky代码,如果我按下按钮,crash...the窗口就会弹出。如果能给我一些建议,我将不胜感激!
我意识到我注释掉了释放按钮部分,虽然我试图让按钮至少打开一个window...any,但使用它的尝试并不顺利。
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也不起作用。
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()这些按钮在1和0之间更改X,但它们不会触发While循环来打开窗口。
发布于 2020-08-19 09:39:57
我没有raspberry pi,但我尝试使用键盘键来模拟它
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的更多信息。
https://stackoverflow.com/questions/63477109
复制相似问题