Home > Software design >  Opencv rotate camera mode
Opencv rotate camera mode

Time:05-11

I want to rotate the camera view by 90 degree clockwise with an interrupt button. But the button's state goes back to it's default state when I click once and unpress the button. As a result, on clicking the button, the camera rotates for an instance and then goes back to the default mode in the while loop. How to solve this issue?

Here is my snippet:

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)
        
cap.release()

Any help would be appreciated.

CodePudding user response:

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

I would try something like this, when you press the button it changes the state of variable button_is_pressed, while still unchanged it keeps rotating image.

CodePudding user response:

@pedro_bb7. The cv2.waitKey() is missing. Using same as your.

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)
        cv2.waitKey(0)
        
cap.release()
  • Related