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



我正在尝试在摄像机录制的视频中检测人脸。当我使用网络摄像头视频时,它运行良好。但是,在摄像机录制的视频中,视频会旋转-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个Args,其中一个是旋转代码(0,1,-1(。您可以查看此链接以了解更多详细信息:https://www.geeksforgeeks.org/python-opencv-cv2-flip-method/

最新更新