我想使用中斷按鈕將相機視圖順時針旋轉 90 度。但是當我單擊一次并松開按鈕時,按鈕的狀態會恢復到默認狀態。結果,在單擊按鈕時,相機會旋轉一個實體,然后在 while 回圈中回傳默認模式。如何解決這個問題?
這是我的片段:
import cv2
cap = cv2. VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('null', frame)
if (button_a.is_pressed()):
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('null', frame)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
這是button_a的功能:
class Sensor_buttonA:
def __init__(self, board=None):
self.first_flag = True
def is_pressed(self):
if self.first_flag:
self.first_flag = False
self.board = get_globalvar_value("BOARD_A")
self.board.board._report_sensor()
return self.board.board.sensor_buttonA_is_pressed()
def irq(self, trigger=None, handler=None):
if self.first_flag:
self.first_flag = False
self.board = get_globalvar_value("BOARD_A")
self.board.board._report_sensor()
self.Pin = Pin(Pin.P65)
self.board.board.sensor_buttonA_irq(trigger, handler, self.Pin)
button_a = Sensor_buttonA()
任何幫助,將不勝感激。
uj5u.com熱心網友回復:
你應該使用額外的變數 - rotate = False- 來保持這個狀態
rotate = False
while True:
ret, frame = cap.read()
# without imshow()
if button_a.is_pressed():
rotate = True
if rotate:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
# outside `if`
cv2.imshow('null', frame)
這樣它會rotate從Falseto改變,True但永遠不會從TruetoFalse
編輯:
如果下一次按下應該再旋轉 90 度(到 180 度),那么它將需要更復雜的代碼。它會檢查是否is_pressed()在前一個回圈中是False和在當前回圈中是True- 當按鈕被長時間按下時只運行一次。
像這樣的東西。
我用正常space的來測驗它。
import cv2
cap = cv2.VideoCapture(0)
rotate = 0
is_pressed = False
previous = False
current = False
while True:
ret, frame = cap.read()
# without imshow()
current = is_pressed
if (previous is False) and (current is True):
rotate = (rotate 90) % 360
print(rotate)
previous = current
if rotate == 90:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
elif rotate == 180:
frame = cv2.rotate(frame, cv2.ROTATE_180)
elif rotate == 270:
frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
# outside `if`
cv2.imshow('null', frame)
key = cv2.waitKey(40) & 0xff
if key == 27:
break
is_pressed = (key == 32)
cv2.destroyAllWindows()
cap.release()
uj5u.com熱心網友回復:
import cv2
cap = cv2. VideoCapture(0)
button_is_pressed = false;
while True:
ret, frame = cap.read()
if (button):
button_is_pressed = not button_is_pressed
if(button_is_pressed)
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('null', frame)
cap.release()
我會嘗試這樣的事情,當你按下按鈕時,它會改變變數 button_is_pressed 的狀態,而仍然保持不變它會保持旋轉影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/472815.html
