简体   繁体   English

opencv - 视频看起来不错,但帧旋转了 90 度

[英]opencv - video looks good but frames are rotated 90 degrees

We save a video on a mobile client and send it to a server.我们将视频保存在移动客户端上并将其发送到服务器。 On the server, I use the following code to save the frames:>在服务器上,我使用以下代码来保存帧:>

import skvideo.io
import cv2

haar = 
'/home/ubuntu/opencv/data/haarcascades/haarcascade_frontalface_alt.xml'
face_cascade = cv2.CascadeClassifier(haar)

ret = True

video = 'my_video.mov'
i = 0
while ret == True:
    cap = skvideo.io.VideoCapture(video)
    ret, frame = cap.read()
    cv2.imwrite('frame_'+str(i)+'.jpg',frame)
    i+=1

When we play the video on a windows media player or itunes, it looks good.当我们在 windows 媒体播放器或 iTunes 上播放视频时,它看起来不错。 Ie the player knows how to orient it.即玩家知道如何定位它。

But skvideo.io does not know that, and those frames we saved are rotated 90 degrees counterclockwise.但是 skvideo.io 不知道,我们保存的那些帧是逆时针旋转 90 度的。

How can we embed info into the video file (a .mov) file that that skvideo knows the correct orientation?我们如何将信息嵌入到 skvideo 知道正确方向的视频文件(.mov)文件中?

there was a glitch in skvideo, it was not reading the available metadata. skvideo 出现故障,它没有读取可用的元数据。 For videos taken in mobile are rotated, but metadata includes such parameter.对于手机拍摄的视频进行了旋转,但元数据包含此类参数。 The skvideo team committed a fix, and current skvideo version 1.1.7 reads metadata from mobile, that indicates that video should be rorated. skvideo 团队提交了一个修复程序,当前的 skvideo 版本 1.1.7 从移动设备读取元数据,这表明视频应该被旋转。 skvideo.io.vread then rotates the file: skvideo.io.vread 然后旋转文件:

1) use newer skvideo version, 1.1.7 which can be cloned at https://github.com/scikit-video/scikit-video 1) 使用较新的 skvideo 版本 1.1.7,可以在https://github.com/scikit-video/scikit-video克隆

2) You can use following code to read all frames in the video, most likely metadata will be read 2)您可以使用以下代码读取视频中的所有帧,很可能会读取元数据

import skvideo.io
videogen = skvideo.io.vread(f.name)

That will rotate the video automatically if it was taken in portrait mode.如果以纵向模式拍摄,这将自动旋转视频。

3) Created an issue on skvideo repo, take a look for further reference: https://github.com/scikit-video/scikit-video/issues/40 3)在skvideo repo上创建了一个问题,看看进一步的参考: https : //github.com/scikit-video/scikit-video/issues/40

It looks like OpenCV does not record the rotation metadata of the video file with VideoCapture() as you can see by the propId s that it stores.看起来OpenCV没有使用VideoCapture()记录视频文件的旋转元数据,正如您可以通过它存储的propId看到的propId

I'm not sure if scikit-video does.我不确定scikit-video是否可以。 It looks like they have a metadata puller called ffprobe which might be able to pull the rotation.看起来他们有一个名为ffprobe的元数据提取器,它可能能够拉动旋转。 See here for an example of how to call and see the output.有关如何调用和查看输出的示例,请参见此处 This shows a hefty list of metadata---no rotation---but that might just be because it's not set or of a movie type which doesn't have rotation metadata.这显示了大量的元数据列表---没有旋转---但这可能只是因为它没有设置或没有旋转元数据的电影类型。

Another way to grab it would be to read the metadata directly from ffmpeg .另一种获取它的方法是直接从ffmpeg读取元数据。 I found an old StackOverflow answer that wrote a little python code to extract specifically the rotation metadata from a video using ffmpeg .我找到了一个旧的 StackOverflow 答案,它编写了一些python代码,专门使用ffmpeg从视频中提取旋转元数据。

I installed 'ffmpeg' ('ffprobe').我安装了'ffmpeg'('ffprobe')。 My computer is Ubuntu 18.04.我的电脑是 Ubuntu 18.04。 Then Python, get_rotation() function worked for me.然后 Python, get_rotation()函数对我get_rotation()

Function Code:功能代码:

import subprocess
import shlex
import json
def get_rotation(self, file_path_with_file_name):
    cmd = "ffprobe -loglevel error -select_streams v:0 -show_entries stream_tags=rotate -of default=nw=1:nk=1"
    args = shlex.split(cmd)
    args.append(file_path_with_file_name)
    ffprobe_output = subprocess.check_output(args).decode('utf-8')
    if len(ffprobe_output) > 0:  # Output of cmdis None if it should be 0
        ffprobe_output = json.loads(ffprobe_output)
        rotation = ffprobe_output
    else:
        rotation = 0
    return rotation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM