高清背景移除算法實操!CVPR2021最佳論文提名
點擊下方“AI算法與圖像處理”,一起進步!
重磅干貨,第一時間送達
環(huán)境
ubuntu 18.04 64bit pytorch 1.7.1 + cu101
簡介
BackgroundMattingV2 是一個實時的、可應(yīng)用于高分辨率的背景移除器。論文地址:https://arxiv.org/abs/2012.07810

算法體驗
#?下載源碼
git?clone?https://github.com/PeterL1n/BackgroundMattingV2.git
cd?BackgroundMattingV2
#?安裝pytorch?gpu版
pip?install?torch==1.7.1+cu101?torchvision==0.8.2+cu101?torchaudio==0.7.2?-f?https://download.pytorch.org/whl/torch_stable.html
模型下載地址
鏈接:https://pan.baidu.com/s/1GsPQtCI2HpD_kRlWWhhIfQ
提取碼:p1r1
解壓后放在源碼根目錄,其中 Resource 有下文要用到的視頻及背景圖片
工程中提供了三個演示程序:
inference_images.py: 圖片背景移除inference_video.py: 視頻背景移除inference_webcam.py: 從攝像頭讀取進行背景移除
這里以視頻背景移除為例,其它2個示例類似,執(zhí)行命令
#?沒有g(shù)pu的,device處就寫cpu或不寫這個參數(shù)
python?inference_video.py?--model-type?mattingrefine?--model-backbone?resnet50?--model-backbone-scale?0.25?--model-refine-mode?sampling?--model-refine-sample-pixels?80000?--model-checkpoint?BackgroundMattingV2models/PyTorch/pytorch_resnet50.pth?--device?cuda?--video-src?d2.mp4?--video-bgr?d2.png?--output-dir?video_output?--output-types?com
處理完畢得到的視頻,原背景已經(jīng)被消除了,換成了默認的綠色

如果想要自定義背景,可以通過添加參數(shù) --video-target-bgr images/background.jpg 來實現(xiàn),不過這里,會報錯,出錯信息如下
Traceback?(most?recent?call?last):
??File?"inference_video.py",?line?182,?in?
????for?input_batch?in?tqdm(DataLoader(dataset,?batch_size=1,?pin_memory=True)):
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/tqdm/std.py",?line?989,?in?__init__
????total?=?len(iterable)
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/torch/utils/data/dataloader.py",?line?397,?in?__len__
????return?len(self._index_sampler)
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/torch/utils/data/sampler.py",?line?243,?in?__len__
????return?(len(self.sampler)?+?self.batch_size?-?1)?//?self.batch_size??#?type:?ignore
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/torch/utils/data/sampler.py",?line?70,?in?__len__
????return?len(self.data_source)
??File?"/home/xugaoxiang/workshop/github/BackgroundMattingV2/dataset/zip.py",?line?14,?in?__len__
????return?max(len(d)?for?d?in?self.datasets)
??File?"/home/xugaoxiang/workshop/github/BackgroundMattingV2/dataset/zip.py",?line?14,?in?
????return?max(len(d)?for?d?in?self.datasets)
ValueError:?__len__()?should?return?>=?0
Exception?ignored?in:?<function?tqdm.__del__?at?0x7ff206daf9d0>
Traceback?(most?recent?call?last):
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/tqdm/std.py",?line?1147,?in?__del__
????self.close()
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/tqdm/std.py",?line?1266,?in?close
????if?self.disable:
AttributeError:?'tqdm'?object?has?no?attribute?'disable'
在 tqdm 的 issue 里也看到了這個問題,很多人反饋,貌似,問題還是存在,討論鏈接:https://github.com/tqdm/tqdm/issues/487
如果是改純色背景的話,我們就不用這個參數(shù) --video-target-bgr,然后直接去修改代碼 inference_video.py,如下,(120, 255, 155)是默認的綠色,自行替換就可以了

比如將顏色改為 (226, 43, 138),效果如下

上例中使用的是 pytorch 的模型,BackgroundMattingV2 還支持 TorchScript、TensorFlow 和 ONNX,至于它們的使用方法,可以參考文檔 https://github.com/PeterL1n/BackgroundMattingV2/blob/master/doc/model_usage.md
插件應(yīng)用
BackgroundMattingV2 還提供了一個插件( plugin )程序來處理攝像頭的數(shù)據(jù),目前這個插件只能運行在 linux 平臺上,ok,下面我們也來體驗一下
首先還是下載源碼
git?clone?https://github.com/andreyryabtsev/BGMv2-webcam-plugin-linux.git
cd?BGMv2-webcam-plugin-linux
插件依賴于內(nèi)核模塊 v4l2loopback ,通過這個模塊來創(chuàng)建并串流到虛擬的視頻設(shè)備節(jié)點
首先安裝工具
sudo?apt-get?install?v4l2loopback-utils?-y
如果已經(jīng)安裝過了 BackgroundMattingV2 的環(huán)境,那就只需要再裝個 pyfakewebcam,否則就執(zhí)行 pip install -r requirements.txt
pip?install?pyfakewebcam
接下來就開始創(chuàng)建設(shè)備節(jié)點
sudo?modprobe?v4l2loopback?devices=1
但是這里報錯了
(base)?xugaoxiang@1070Ti:~/workshop/github/BackgroundMattingV2/BGMv2-webcam-plugin-linux$?sudo?modprobe?v4l2loopback?devices=1
modprobe:?ERROR:?could?not?insert?'v4l2loopback':?Bad?address
解決方法如下,首先卸載掉原來安裝的包
sudo?apt-get?remove?v4l2loopback-dkms
然后,下載 v4l2loopback 源碼
git?clone?https://github.com/umlaeute/v4l2loopback.git
編譯源碼,生成內(nèi)核模塊 ko 文件
make

最后安裝
sudo?make?install

安裝過程中出現(xiàn)了 ssl 相關(guān)的錯誤,可以忽略
為了讓模塊在系統(tǒng)啟動時自動加載,執(zhí)行
sudo?depmod?-a
depmod 命令用于生成 modules.dep 和 map 文件,用于在啟動時模塊的自動加載。
再次創(chuàng)建設(shè)備節(jié)點 sudo modprobe v4l2loopback devices=1
最后執(zhí)行
python?demo_webcam.py?--model-checkpoint?../BackgroundMattingV2models/TorchScript/torchscript_resnet50_fp32.pth
但是,我這里報錯了
Corrupt?JPEG?data:?4?extraneous?bytes?before?marker?0xd6
Corrupt?JPEG?data:?1?extraneous?bytes?before?marker?0xd2
Corrupt?JPEG?data:?2?extraneous?bytes?before?marker?0xd6
Corrupt?JPEG?data:?2?extraneous?bytes?before?marker?0xd6
Corrupt?JPEG?data:?4?extraneous?bytes?before?marker?0xd6
Traceback?(most?recent?call?last):
??File?"demo_webcam.py",?line?586,?in?
????fake_camera?=?pyfakewebcam.FakeWebcam(args.camera_device,?cam.width,?cam.height)
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/pyfakewebcam/pyfakewebcam.py",?line?54,?in?__init__
????fcntl.ioctl(self._video_device,?_v4l2.VIDIOC_S_FMT,?self._settings)
OSError:?[Errno?22]?Invalid?argument
Corrupt?JPEG?data:?1?extraneous?bytes?before?marker?0xd2
FATAL:?exception?not?rethrown
Aborted?(core?dumped)
通過命令 v4l2-ctl --list-devices 查看設(shè)備節(jié)點信息
(pytorch1.7)?xugaoxiang@1070Ti:~/workshop/github/BackgroundMattingV2/BGMv2-webcam-plugin-linux$?v4l2-ctl?--list-devices
Dummy?video?device?(0x0000)?(platform:v4l2loopback-000):
????????/dev/video2
UVC?Camera?(046d:081b)?(usb-0000:00:14.0-8):
????????/dev/video0
????????/dev/video1
可以看到 v4l2loopback 節(jié)點是 /dev/video2,因此需要去修改下源碼 demo_webcam.py,將默認的設(shè)備節(jié)點由原來的 dev/video1 改為 dev/video2
parser.add_argument('--camera-device',?type=str,?default='/dev/video2)
然后,再次執(zhí)行命令 python demo_webcam.py --model-checkpoint ../BackgroundMattingV2models/TorchScript/torchscript_resnet50_fp32.pth
模糊背景效果

自定義背景效果

備注
要求測試圖片或者視頻分辨率要能夠被4整除,否則報錯
Traceback?(most?recent?call?last):
??File?"inference_images.py",?line?132,?in?
????pha,?fgr,?_,?_,?err,?ref?=?model(src,?bgr)
??File?"/home/xugaoxiang/anaconda3/envs/pytorch1.7/lib/python3.8/site-packages/torch/nn/modules/module.py",?line?727,?in?_call_impl
????result?=?self.forward(*input,?**kwargs)
??File?"/home/xugaoxiang/workshop/github/BackgroundMattingV2/model/model.py",?line?163,?in?forward
????assert?src.size(2)?//?4?*?4?==?src.size(2)?and?src.size(3)?//?4?*?4?==?src.size(3),?\
AssertionError:?src?and?bgr?must?have?width?and?height?that?are?divisible?by?4
測試圖片及視頻
下載地址: https://drive.google.com/drive/folders/16H6Vz3294J-DEzauw06j4IUARRqYGgRD
關(guān)聯(lián)閱讀
backgroundremover去背 rembg去背 https://github.com/PeterL1n/BackgroundMattingV2 https://github.com/umlaeute/v4l2loopback https://github.com/andreyryabtsev/BGMv2-webcam-plugin-linux
交流群
歡迎加入公眾號讀者群一起和同行交流,目前有美顏、三維視覺、計算攝影、檢測、分割、識別、醫(yī)學影像、GAN、算法競賽等微信群
個人微信(如果沒有備注不拉群!) 請注明:地區(qū)+學校/企業(yè)+研究方向+昵稱
下載1:何愷明頂會分享
在「AI算法與圖像處理」公眾號后臺回復:何愷明,即可下載。總共有6份PDF,涉及 ResNet、Mask RCNN等經(jīng)典工作的總結(jié)分析
下載2:終身受益的編程指南:Google編程風格指南
在「AI算法與圖像處理」公眾號后臺回復:c++,即可下載。歷經(jīng)十年考驗,最權(quán)威的編程規(guī)范!
下載3 CVPR2021 在「AI算法與圖像處理」公眾號后臺回復:CVPR,即可下載1467篇CVPR?2020論文 和 CVPR 2021 最新論文

