动作追踪模型CoTracker,动态光学分割的视频酷炫彩虹轨迹!

大家好,我是千寻哥,在上一次的卡通人物转动画的项目的结尾,留下了一个彩蛋,我说找到了一个“运动光学”有关的项目,效果真的很酷炫!今天就来验证了!这个项目就是CoTracker物体追踪模型。


一、CoTracker物体追踪模型简介

我们直接看一个使用CoTracker实际的视频动作跟踪,已经精确到了每个像素!而且加上了七彩的彩虹流线特效!感觉对于拍摄小红书里面健身以及运动视频的博主,简直不要太帅!



Meta公司最新推出的视频跟踪工具CoTracker,这个工程发布没多久就在GitHub上斩获了1.4k星标。


二、CoTracker模型部署应用


不过,在线的demo是个简易版本,还不支持自定义跟踪位置,只能根据输入的跟踪数量等距分配。


但是如果自己部署、用代码操纵的话,就可以设置任意跟踪点了。说到这我们正好来看一下CoTracker该怎么部署。

首先是Colab版本,我们刚刚说到的自定义跟踪点也在Colab当中。


传送门:

https://colab.research.google.com/github/facebookresearch/co-tracker/blob/master/notebooks/demo.ipynb



Colab的过程不必过多介绍,进入之后运行笔记中的代码就可以了。

而且如果想自己动手的话,最简单的方式是从torch.hub中直接调用已完成预训练的版本。


1、安装运行依赖

pip install einops timm tqdm


2、导入torch.hub的预训练版本

import torch
import timm
import einops
import tqdm

cotracker = torch.hub.load("facebookresearch/co-tracker", "cotracker_w8")


如果要对模型进行评估或训练,那么使用GitHub Repo更为合适。


3. 首先要安装一下程序代码和相关依赖:


git clone https://github.com/facebookresearch/co-tracker
cd co-tracker
pip install -e .
pip install opencv-python einops timm matplotlib moviepy flow_vis 


4.创建模型文件夹,下载推理模型:


mkdir checkpoints
cd checkpoints
wget https://dl.fbaipublicfiles.com/cotracker/cotracker_stride_4_wind_8.pth
wget https://dl.fbaipublicfiles.com/cotracker/cotracker_stride_4_wind_12.pth
wget https://dl.fbaipublicfiles.com/cotracker/cotracker_stride_8_wind_16.pth
cd ..


5.安装gradio重要依赖包,写了这么多教程,大家肯定知道gradio的安装包

!pip3 install gradio


6. 千寻测试一下,直接使用Google Colab实验室里面部署的效果更高

app = gr.Interface(
title = "🎨 CoTracker: It is Better to Track Together",
description = "<div style='text-align: left;'> \
<p>Welcome to <a href='http://co-tracker.github.io' target='_blank'>CoTracker</a>! This space demonstrates point (pixel) tracking in videos. \
Points are sampled on a regular grid and are tracked jointly. </p> \
<p> To get started, simply upload your <b>.mp4</b> video in landscape orientation or click on one of the example videos to load them. The shorter the video, the faster the processing. We recommend submitting short videos of length <b>2-7 seconds</b>.</p> \
<ul style='display: inline-block; text-align: left;'> \
<li>The total number of grid points is the square of <b>Grid Size</b>.</li> \
<li>To specify the starting frame for tracking, adjust <b>Grid Query Frame</b>. Tracks will be visualized only after the selected frame.</li> \
<li>Use <b>Backward Tracking</b> to track points from the selected frame in both directions.</li> \
<li>Check <b>Visualize Track Traces</b> to visualize traces of all the tracked points. </li> \
</ul> \
<p style='text-align: left'>For more details, check out our <a href='https://github.com/facebookresearch/co-tracker' target='_blank'>GitHub Repo</a> ⭐</p> \
</div>",
fn=cotracker_demo,
inputs=[
gr.Video(type="file", label="Input video", interactive=True),
gr.Slider(minimum=1, maximum=30, step=1, value=10, label="Grid Size"),
gr.Slider(minimum=0, maximum=30, step=1, default=0, label="Grid Query Frame"),
gr.Checkbox(label="Backward Tracking"),
gr.Checkbox(label="Visualize Track Traces"),
],
outputs=gr.Video(label="Video with predicted tracks"),
examples=[
[ "./assets/apple.mp4", 20, 0, False, False ],
[ "./assets/apple.mp4", 10, 30, True, False ],
],
cache_examples=False
)


运行的窗口效果:


点击公共访问链接:


三、CoTracker运行原理分析

虽然都是追踪,但CoTracker和物体追踪模型有很大区别。

CoTracker并没有基于语义理解对视频中物体进行分割的过程,而是把重点放在了像素点上。

底层方面,CoTracker采用了Transformer架构。


Transformer编码了视频中点的跟踪信息,并迭代更新点的位置。

推理上,CoTracker还采用了一种窗口机制,在时间轴上划分出滑动窗口。


CoTracker使用上个窗口的输出对后面的窗口进行初始化,并在每个窗口上运行多次Transformer迭代。

这样就使得CoTracker能够对更长的视频进行像素级跟踪。

总之,经过一番训练之后,CoTracker取得了一份不俗的成绩单。

在FastCapture数据集测试上,CoTracker的成绩在一众模型中脱颖而出,其中也包括Meta自家的DINOv2。


总之,喜欢的话,就赶紧体验一下试试吧!


四、CoTracker模型推理效果归总

1. 一场马术比赛中,马匹在骑手的操纵下优雅地跨过障碍,画出了优美的弧线。


2. 还有一架帆船乘风破浪,仿佛捉住了风的影子。


3. 一位跳伞运动员从空中划过,留下了一道绚丽的彩虹……



GitHub项目页:

https://github.com/facebookresearch/co-tracker


怎么样是不是效果不错,大家赶快试一试吧!


我是千寻哥,一个只讲干货的码农!我们下期见~

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
千与编程
下载 APP