视频中的帧在提取后颠倒

Jam*_*ewp 6 python video-processing opencv3.0

我的问题是,当我使用 opencv 将视频提取到帧中时,有时我得到的帧会翻转,这发生在我的机器(窗口)和 VM(ubuntu)但我测试的一些视频中,帧不翻转。所以,我想知道应该在我的代码中更改/添加什么因素或什么来使提取物固定而不翻转

def extract_frame(video,folder):
   global fps

   os.mkdir('./green_frame/{folder}/'.format(folder=folder))
   vidcap = cv2.VideoCapture(video)
   success,image = vidcap.read()
   fps = vidcap.get(cv2.CAP_PROP_FPS)
   count = 0
   success = True
   while success:  #os.path.join(pathOut,(name+'.png'))
      cv2.imwrite(os.path.join('./green_frame/{folder}/'.format(folder=folder),"frame%d.png" % count), image)
      success,image = vidcap.read()
      print('Read a new frame: ', success) 
      count += 1
Run Code Online (Sandbox Code Playgroud)

这是我从这段代码中得到的框架示例。 翻动 我使用的原始视频是这样颠倒的:在此处输入图片说明

所以,就我而言,我必须改变什么才能使它不像我的第一张照片那样翻转。它与视频的分辨率或帧率有关吗?我用 1280x720 分辨率的视频进行了测试,提取的所有帧都颠倒了,但是来自具有 568x320 分辨率的视频的帧是正常的

谢谢

编辑:所以,我查看了视频的信息,我发现在元数据中,它对提取到倒置帧的视频进行了 180 度旋转 在此处输入图片说明 但是当我检查产生非倒置帧的普通视频时,它没有旋转:180 在此处输入图片说明

因此,从这里开始,我该如何处理具有旋转功能的视频?

Raf*_*han 17

对于仍在研究此问题的任何人,我只是遇到了同样的问题。事实证明,一些 Android 手机和 iPhone 会在横向拍摄图像/帧,并根据 exif '旋转' 标签即时转换它们以显示图像/帧。

OpenCV 中奇怪的设计选择是cv2.imread(img_file)已经通过读取图像的rotate标签以正确的方向读取图像,但是cv2.VideoStream'sread()方法没有这样做。

所以,为了解决这个问题,我曾经ffmpeg阅读“旋转”标签并将视频帧旋转到正确的方向。(非常感谢上面的评论,为我指明了正确的方向)

以下是代码:

  • 确保你有ffmpegpython。( pip install ffmpeg-python)

  • 创建一个方法来检查 video_file 是否需要旋转:

     import ffmpeg    
    
     def check_rotation(path_video_file):
         # this returns meta-data of the video file in form of a dictionary
         meta_dict = ffmpeg.probe(path_video_file)
    
         # from the dictionary, meta_dict['streams'][0]['tags']['rotate'] is the key
         # we are looking for
         rotateCode = None
         if int(meta_dict['streams'][0]['tags']['rotate']) == 90:
             rotateCode = cv2.ROTATE_90_CLOCKWISE
         elif int(meta_dict['streams'][0]['tags']['rotate']) == 180:
             rotateCode = cv2.ROTATE_180
         elif int(meta_dict['streams'][0]['tags']['rotate']) == 270:
             rotateCode = cv2.ROTATE_90_COUNTERCLOCKWISE
    
         return rotateCode
    
    Run Code Online (Sandbox Code Playgroud)
  • 创建一个方法来纠正视频文件中帧的旋转:

     def correct_rotation(frame, rotateCode):  
         return cv2.rotate(frame, rotateCode) 
    
    Run Code Online (Sandbox Code Playgroud)
  • 最后,在主循环中执行此操作:

     # open a pointer to the video file stream
     vs = cv2.VideoCapture(video_path)
    
     # check if video requires rotation
     rotateCode = check_rotation(video_path)
    
     # loop over frames from the video file stream
     while True:
         # grab the frame from the file
         grabbed, frame = vs.read()
    
         # if frame not grabbed -> end of the video
         if not grabbed:
             break
    
         # check if the frame needs to be rotated
         if rotateCode is not None:
             frame = correct_rotation(frame, rotateCode)
    
         # now your logic can start from here
    
    Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

  • 我有一个应该旋转 180 度的视频样本,但元数据中的旋转信息是 90 度,所以它是错误的。我有另一个视频不需要旋转,但元数据旋转信息也是 90 度。 (3认同)
  • 我发现,“旋转”信息位于不同的流上 (2认同)