200餘行程式碼,Github 3.4k星,讓你實時從影片中隱身
- 2020 年 3 月 12 日
- 筆記
Jason Mayes 是一名在Google工作的資深網頁工程師,他長期致力於運用新興技術提供物聯網解決方案。近日,充滿奇思妙想的 Mayes 又使用 TensorFlow.js 製作了一個僅用 200 余行程式碼的項目,名為 Real-Time-Person-Removal。它能夠實時將複雜背景中的人像消除,而且僅基於網頁端。
現在,Mayes 在 GitHub 上開源了他的程式碼,並在 Codepen.io 上提供了演示 Demo。從影片中看到,你現在只需要一台能上網的電腦和一個網路攝影機就能體驗它。
- 項目地址:github.com/jasonmayes/Real-Time-Person-Removal
- Demo 地址:codepen.io/jasonmayes/pen/GRJqgma
目前,該項目異常火熱,在 Github 上已經獲得了 3.4k 的 Star 量。
實時隱身不留痕項目作者:Jason Mayes
我們先來看一下運行的效果。下圖中,上半部分是原始影片,下半部分是使用 TensorFlow.js 對人像進行消除後的影片。可以看到,除了偶爾會在邊緣處留有殘影之外,整體效果還是很不錯的。
為了展現這個程式在複雜背景下消除人像並重建背景的能力,Mayes 特意在床上放了一台正在播放影片的筆記型電腦電腦。當他的身體遮擋住筆記型電腦電腦時,可以看到消除演算法暫停在電腦被遮擋前的播放畫面,並能在人移開時迅速地重建出當前畫面。

此外,Mayes 還在 Codepen.io 上提供了能夠直接運行的示例。只需要點擊 Enable Webcam,離開攝影機一段距離確保演算法能夠較全面的收集到背景影像,之後當你再出現在攝影機前時就能從下方的預覽窗口看到「隱形」後的畫面了。

網友表示有了這個程式,像之前 BBC 直播中孩子闖進門來那樣的大型翻車現場就有救了。
項目運行機制
Mayes 開發的這個人像消除程式背後的運行機制十分簡單,他使用了 TensorFlow.js 中提供的一個預訓練的 MobileNet,用於人像分割。
const bodyPixProperties = { architecture: 'MobileNetV1', outputStride: 16, multiplier: 0.75, quantBytes: 4 };
TensorFlow.js 提供的部分電腦視覺預訓練模型。
MobileNet 是Google在 2017 年針對移動端和嵌入式設備提出的網路,針對影像分割。其核心思想是使用深度可分離卷積構建快速輕量化的網路架構。Mayes 選擇使用它的原因也是出於其輕量化的原因,假如使用 YOLO 或者 Fast-RCNN 這類物體檢測演算法的話,在移動端就很難做到實時性。
通過 MobileNet 的輸出獲得檢測到人物像素的邊界框。
// Go through pixels and figure out bounding box of body pixels. for (let x = 0; x < canvas.width; x++) { for (let y = 0; y < canvas.height; y++) { let n = y * canvas.width + x; // Human pixel found. Update bounds. if (segmentation.data[n] !== 0) { if(x < minX) { minX = x; } if(y < minY) { minY = y; } if(x > maxX) { maxX = x; } if(y > maxY) { maxY = y; } foundBody = true; } } }為避免人物沒有被檢測完全的現象,這裡使用變數額 scale 對檢測區域進行適當放縮。這個 1.3 的參數是測試出來的,感興趣的讀者可以調整試試看。
// Calculate dimensions of bounding box. var width = maxX - minX; var height = maxY - minY; // Define scale factor to use to allow for false negatives around this region. var scale = 1.3; // Define scaled dimensions. var newWidth = width * scale; var newHeight = height * scale; // Caculate the offset to place new bounding box so scaled from center of current bounding box. var offsetX = (newWidth - width) / 2; var offsetY = (newHeight - height) / 2; var newXMin = minX - offsetX; var newYMin = minY - offsetY;
之後對人物 bounding box 之外的區域進行更新,並且當檢測到人物移動時,更新背景區域。
// Now loop through update backgound understanding with new data // if not inside a bounding box. for (let x = 0; x < canvas.width; x++) { for (let y = 0; y < canvas.height; y++) { // If outside bounding box and we found a body, update background. if (foundBody && (x < newXMin || x > newXMin + newWidth) || ( y < newYMin || y > newYMin + newHeight)) { // Convert xy co-ords to array offset. let n = y * canvas.width + x; data[n * 4] = dataL[n * 4]; data[n * 4 + 1] = dataL[n * 4 + 1]; data[n * 4 + 2] = dataL[n * 4 + 2]; data[n * 4 + 3] = 255; } else if (!foundBody) { // No body found at all, update all pixels. let n = y * canvas.width + x; data[n * 4] = dataL[n * 4]; data[n * 4 + 1] = dataL[n * 4 + 1]; data[n * 4 + 2] = dataL[n * 4 + 2]; data[n * 4 + 3] = 255; } } } ctx.putImageData(imageData, 0, 0); if (DEBUG) { ctx.strokeStyle = "#00FF00" ctx.beginPath(); ctx.rect(newXMin, newYMin, newWidth, newHeight); ctx.stroke(); } }至此為演算法的核心部分,用了這個程式,你也可以像滅霸一樣彈一個響指(單擊一下滑鼠)讓人憑空消失。
熱門的「影片隱身術」
其實,這並非機器之心報道的第一個消除影片中人像的項目。
2019 年,我們也曾報道過「video-object-removal」項目。在此項目中,只要畫個邊界框,模型就能自動追蹤邊界框內的物體,並在影片中隱藏它。
項目地址:github.com/zllrunning/video-object-removal

但從項目效果來看,也會有一些瑕疵,例如去掉了行人後,背景內的車道線對不齊等。
與 Mayes 的這個項目類似,video-object-removal 主要借鑒了 SiamMask 與 Deep Video Inpainting,它們都來自 CVPR 2019 的研究。通過 SiamMask 追蹤影片中的目標,並將 Mask 傳遞給 Deep Video Inpainting,然後模型就能重建影像,完成最終的修復了。
對此類技術感興趣的讀者可自行運行下這兩個項目,做下對比。
來源:機器之心

