簡體   English   中英

如何旋轉相機錄制的視頻?

[英]How to rotate camera recorded video?

我正在嘗試在相機錄制的視頻中檢測人臉。 當我使用網絡攝像頭視頻進行操作時,它工作正常。 但是,對於攝像機錄制的視頻,視頻會旋轉 -90 度。 請建議我,我如何獲得用於人臉檢測的實際視頻 output?

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 function 根據您的要求旋轉圖像

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

對於旋轉視頻,您可以使用 cv2.flip(),此方法需要 3 個 Args,其中一個是旋轉代碼(0,1,-1),您可以查看此鏈接了解更多詳細信息: https://www.geeksforgeeks。 org/python-opencv-cv2-flip-method/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM