如何旋转摄像机录制的视频?

How to rotate camera recorded video?

我正在尝试检测摄像机录制的视频中的人脸。当我使用网络摄像头视频时,它工作正常。但是,对于摄像机录制的视频,视频会旋转 -90 度。请建议我,如何获得人脸检测的实际视频输出?

import cv2
import sys

cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier('C:/Users/HP/Anaconda2/pkgs/opencv-3.2.0-np112py27_204/Library/etc/haarcascades/haarcascade_frontalface_default.xml')

#video_capture = cv2.videoCapture(0)
video_capture = cv2.VideoCapture('C:/Users/HP/sample1.mp4')
w=int(video_capture.get(3))
h=int(video_capture.get(4))

#output = cv2.VideoWriter('output_1.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 60,frameSize = (w,h))

while True:

    ret, frame = video_capture.read()
    frame = rotateImage(frame,90)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(gray, 1.3, 5) 

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    #cv2.imshow('face',i)

    #output.write(frame)

    cv2.imshow('Video', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    output.release()

cv2.destroyAllWindows()

在 cv2 中,您可以使用 cv2.rotate 函数根据您的要求旋转图像

rotated=cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

对于旋转视频,您可以使用 cv2.flip(),此方法采用 3 个参数,其中一个是旋转代码 (0,1,-1) 您可以检查此 link更多细节: https://www.geeksforgeeks.org/python-opencv-cv2-flip-method/