用于读取视频帧的OpenCV实用程序

videoutils的Python项目详细描述


OpenCV的视频实用程序

videoutils lets you get rid of writing boilerplate code for reading video and adds some convenience on top of that.

安装

pip install videoutils

如何使用

fromvideoutils.ioimportread_video,as_tensor,bgr2rgb,resizefname='files/interstellar-waves-edit.mp4'
^{pr2}$
1578






(480, 720, 3)

默认情况下,read_video返回形状为(height, width, channels)np.array的列表。
但是,您可以通过多种方式精确地定义要抓取的帧。这可以通过使用{{}、end_idxframe_stride}或{}参数来完成。在

抓取第一个n

n=50x=read_video(fname,end_idx=n)x2=read_video(fname,target_frames=(0,n))len(x)len(x)==len(x2)
50






True

每隔第n帧抓取

n=5x=read_video(fname,frame_stride=n,end_idx=50)len(x)
10
x=read_video(fname,frame_stride=50)# total frames = 1578len(x)
32

特定指标下的抓斗框架

x=read_video(fname,target_frames=[10,50,76,420])len(x)
4
x=read_video(fname,start_idx=10,end_idx=15)x2=read_video(fname,target_frames=(10,15))len(x)len(x)==len(x2)
5






True

返回为torch.Tensor

您可以传递任何将形状(height, width, channels)np.array转换为apply参数的函数。videoutils提供as_tensor为方便起见——如果使用此函数,read_video将自动调用torch.stack,并以4D张量的形式返回帧集合,否则它将返回3D数组/张量的list。在

importtorchfromfunctoolsimportpartialx=read_video(fname,end_idx=10,apply=as_tensor)x2=read_video(fname,end_idx=10,apply=partial(as_tensor,normalise=True))x2=torch.stack(x2)# since we aren't using `as_tensor`, but a partial (thus different) functionx.shapex.shape==x2.shapex.mean(),x2.mean()
torch.Size([10, 480, 720, 3])






True






(tensor(36.8276), tensor(0.1443))

调整视频大小

read_video有一个可选参数resize_func,这意味着它是一个调整形状(height, width, channels)np.array大小的函数。
您可以使用预定义的resize函数或在此处传入自定义函数。在

help(resize)
Help on function resize in module videoutils.utils:

resize(image, height=None, width=None, keep_aspect_ratio=True, scale_factor=1.0)
    Resize by `scale_factor` if preserving aspect ratio else
    resize by custom `height` and `width`
x=read_video(fname,target_frames=[0,1,2],apply=as_tensor,resize_func=partial(resize,scale_factor=2.))x.shape
torch.Size([3, 960, 1440, 3])
x=read_video(fname,target_frames=[0,1,2],apply=as_tensor,resize_func=partial(resize,width=200,height=100,keep_aspect_ratio=False))x.shape
^{pr21}$

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何计算大于平均值的数字的百分比。?   JavaFX中后台执行任务的服务   xml解析用于解析java中推荐的xml解析器的小文档   java Nativescript+无法实例化接收器。。。。没找到上课的机会   maven是在数百个Java测试类上应用自定义JUnit测试运行程序的更简单方法   泛型如何引用接口在Java中实现的类类型?   java序列化程序SimpleXML只发送第一行   java字符串组合/排列   java无法创建EMRclient Sdkclientexception   JavaMaven不生成“persistence.xml”文件   java找不到主要方法   java中的正则表达式正负双精度   接口对于我试图解决的问题,有Java设计模式吗?   java在创建bean时获取异常   java如何创建<?使用JCodeModel扩展子类型>?   java在javafx中制作搜索栏   java Compare Hashmap with List=new Arraylist()   java接口与具体类