ndimage.rotate for rotating a live video makes it very slow (python)

346 Views Asked by At

My objective is to have the webcam transmitting live feed and I could rotate the image feed by some degries (this part would be controlled by the values of a gyroscope). The problem is although the image rotates when using ndimage.rotate by the correct angle the video becomes really slow (below 10 fps). This is an example code of how im using it (in here I use a fixed angle just to demonstrate):

import cv2
from scipy import ndimage

cap = cv2.VideoCapture(0)
mode=2 #mode 1 = Normal mode, mode 2 = Rotated mode
# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, None, fx=1, fy=1, interpolation=cv2.INTER_AREA)
    
    if mode==1:
        cv2.imshow('Input', frame) #Original version
    else:
        rotate=ndimage.rotate(frame, angle=45, reshape=False)
        cv2.imshow('Input', rotate) #Rotated version (very slow)
    

    c = cv2.waitKey(1)
    if c == 27:
        break

cap.release()
cv2.destroyAllWindows()

How can I make the video to run as fast as the original mode? Or at least somewhat faster.

Thanks, its my first question so I apologize if Im missing something.

0

There are 0 best solutions below