以下代码在 Featurize 可以点击右上角的保存至自己账号的云盘内
(原文) Featurize 笔记本
!pip install opencv-python matplotlib
制作 Detectron2 的 Docker 镜像
!git clone https://github.com/facebookresearch/detectron2.git
!cd detectron2/docker;docker build -t detectron2 .
创建临时目录用于存放输入文件
!mkdir /home/featurize/tmp
下载测试图片和视频(可以自己选择)
!wget https://featurize.oss-cn-chengdu.aliyuncs.com/slomo.mp4 -O /home/featurize/tmp/slomo.mp4
!wget https://featurize.oss-cn-chengdu.aliyuncs.com/input.jpg -O /home/featurize/tmp/input.jpg
可视化视频文件
from IPython.display import Video
Video("/home/featurize/tmp/slomo.mp4", embed=True)
下载 Detectron2 的预训练模型(当然也可以自己选择)
!wget https://featurize.oss-cn-chengdu.aliyuncs.com/model_final_cafdb1.pkl -O /home/featurize/tmp/model_final_cafdb1.pkl
运行 Docker (需要一点推断时间)
!docker run -it \
--volume="/home/featurize/tmp:/home/appuser/detectron2_repo/tmp" \
--gpus all \
--shm-size=8gb \
--env="DISPLAY" \
detectron2 python demo/demo.py \
--input tmp/input.jpg \
--output tmp/output.jpg \
--config-file ./configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml \
--opts MODEL.WEIGHTS ./tmp/model_final_cafdb1.pkl
看看 Docker 运行结果
import cv2
import matplotlib.pyplot as plt
f, axs = plt.subplots(1,2,figsize=(20,20))
axs[0].imshow(cv2.cvtColor(cv2.imread('/home/featurize/tmp/input.jpg'), cv2.COLOR_BGR2RGB));
axs[1].imshow(cv2.cvtColor(cv2.imread('/home/featurize/tmp/output.jpg'), cv2.COLOR_BGR2RGB));
将输入替换成视频
!docker run -it \
--volume="/home/featurize/tmp:/home/appuser/detectron2_repo/tmp" \
--gpus all \
--shm-size=8gb \
--env="DISPLAY" \
detectron2 python demo/demo.py \
--video-input tmp/slomo.mp4 \
--output tmp/output.mp4 \
--config-file ./configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml \
--opts MODEL.WEIGHTS ./tmp/model_final_cafdb1.pkl
from IPython.display import Video
Video("/home/featurize/tmp/output.mp4", embed=True)