2

I am trying to display my Webcam's (a GoPro 8) video onto the computer with OpenCV, but I do not want the autorotation feature -- by that I mean when I shift from holding the GoPro from landscape to portrait (say rotated 90degree), I want the displayed image on my computer to show the rotated view in landscape.

Image displayed on computer when held on Landscape

Image displayed on computer when held on Portrait

So the above two photo shows what is does right now, but I want it to look like the below. Ideal image displayed on computer when held on Portrait

Here is my code:

video = cv2.VideoCapture(1)
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

while(True):
   ret, frame = video.read()
   if ret == True:
      flipped = cv2.flip(frame, 1) #flip frame vertically, I want it flipped for other reasons
      cv2.imshow('window', flipped)
   if cv2.waitKey(1) & 0xFF == ord('q') :
      break
cv2.destroyAllWindows()

Is there any way I can ignore the orientation of the external webcam? I tried rotating the images with cv2.rotate() but it is not what I want.

  • 1
    eventually you can compare `height > width` to recognize `portrait` and then rotate it with `cv2.rotate()` – furas Dec 23 '20 at 05:56
  • it seem cv2 has property [CAP_PROP_ORIENTATION_META](https://docs.opencv.org/3.4/d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704da161e0ba8e8fad2a34880e0fc6753a2b2) but I don't know if it gives information about camer rotation. But even if it gives information about rotation then you would still need to use `cv2.rotate()` – furas Dec 23 '20 at 06:05

1 Answers1

2

I think the best solution is using cv2.rotate in this way you can get your desired output. Btw I'm using a Logitech 720p webcam and when I put it in portrait position it gives me your desired output without using any python function and here is code for your output using cv2.rotate ()

import cv2
import numpy as np
cap = cv2.VideoCapture (0)

width = 400
height = 350

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, (width, height))
    flipped = cv2.flip(frame, 1)
    framerot = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
    framerot = cv2.resize(framerot, (width, height))
    StackImg = np.hstack([frame, flipped, framerot])
    cv2.imshow("ImageStacked", StackImg)
    if cv2.waitKey(1) & 0xff == ord('q'):
        break
cv2.destroyAllWindows()