cv2_imshow()无法在Google Colab中呈现视频文件 [英] cv2_imshow() doesn't render video file in Google Colab

查看:571
本文介绍了cv2_imshow()无法在Google Colab中呈现视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些OpenCV图像分析(使用Python3)从本地Jupyter笔记本迁移到Google Colab.

我原来的Jupyter Notebook代码可以正常工作,并且视频可以正常渲染(在其自己的Window中)(请参阅下面的代码子集).此代码使用cv2.imshow()渲染视频.在Colab中使用相同的"cv2.imshow()"代码时,视频不会呈现.

基于此建议-我在Colab中切换为使用cv2_imshow().但是,这种更改会导致产生470张图像(每帧1张)的垂直系列,而不是正在播放的视频.

这里是链接到colab文件.. >

有人可以概述如何在Colab中渲染由OpenCV处理的视频吗?

import numpy as np
import cv2

cap = cv2.VideoCapture(r"C:\.....Blocks.mp4")
counter = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    cv2.imshow(frame)

    print("Frame number: " + str(counter))
    counter = counter+1
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

解决方案

方法cv2.imshow()显示图像.因此,您要做的基本上是逐帧读取整个视频并显示该帧.要观看整个视频,您需要将这些帧写回到VideoWriter对象.

因此,请在while循环之前创建一个VideoWriter对象:

res=(360,240) #resulotion
fourcc = cv2.VideoWriter_fourcc(*'MP4V') #codec
out = cv2.VideoWriter('video.mp4', fourcc, 20.0, res)

使用write()方法处理后写帧

out.write(frame)

最后,以与VideoCapture

相同的方式释放对象

out.release()

现在,将以您的名字 video.mp4

录制视频.

I am attempting to migrate some OpenCV image analysis (using Python3) from a local Jupyter notebook to Google Colab.

My original Jupyter Notebook code works fine, and the video renders fine (in its own Window) (see a subset of the code below). This code uses cv2.imshow() to render the video. When using the same "cv2.imshow()" code in Colab, the video doesn't render.

Based on this suggestion - I switched to using cv2_imshow()in Colab. However, this change leads to a vertical series of 470 images (1 for each frame), rather than the video being played.

Here is a link to the colab file.

Can anyone outline how to render the video processed by OpenCV within Colab?

import numpy as np
import cv2

cap = cv2.VideoCapture(r"C:\.....Blocks.mp4")
counter = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    cv2.imshow(frame)

    print("Frame number: " + str(counter))
    counter = counter+1
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

解决方案

The method cv2.imshow() shows an image. So, what you are doing is basically reading the whole video frame by frame and showing that frame. To see a whole video, you need to write these frames back to a VideoWriter object.

So, create a VideoWriter object before the while loop:

res=(360,240) #resulotion
fourcc = cv2.VideoWriter_fourcc(*'MP4V') #codec
out = cv2.VideoWriter('video.mp4', fourcc, 20.0, res)

Write the frame after processing using write() method

out.write(frame)

Finally, release the object the same way you did with the VideoCapture

out.release()

Now, a video will be written in your with the name video.mp4

这篇关于cv2_imshow()无法在Google Colab中呈现视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆