官方GitHub: https://github.com/danielgatis/rembg/tree/main
安裝方式
CPU support
1 2 | pip install rembg # for library pip install rembg[cli] # for library + cli |
GPU support
首先要去安裝onnxruntime-gpu,接著
1 2 | pip install rembg[gpu] # for library pip install rembg[gpu,cli] # for library + cli |
使用範例
下面為一個讀取攝影機的簡單去背使用範例
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import VideoStream import cv2 import numpy as np import time from rembg import new_session, remove videostream = VideoStream.VideoStream(( 1280 , 720 ), 30 , 0 ).start() cam_quit = 0 total = 0 frame = 0 while cam_quit = = 0 : imageSource = videostream.read() imageSource = cv2.resize(imageSource, ( 640 , 360 )) aStart = time.time() # u2netp為model名稱 output = remove(imageSource, session = new_session( "u2netp" ) ) aEnd = time.time() if frame < = 60 : total = total + (aEnd - aStart) frame = frame + 1 print ( "detect time: " + str (aEnd - aStart )) cv2.imshow( "output" , output) key = cv2.waitKey( 1 ) & 0xFF if key = = ord ( "q" ): print ( "avg detect time: " + str (total / frame)) cam_quit = 1 videostream.stop() cv2.destroyAllWindows() |
Stable Diffusion WebUI Rembg
Stable Diffusion WebUI所使用的去背功能也是這個唷!
相關介紹文章: https://zhuanlan.zhihu.com/p/648234420
可以參考WebUI的參數去尋找相關程式碼可設定的部分,例如Erode size、Foreground threshold、Background threshold
模型去背測試
原始影像如下圖

模型列表
- u2net (下載, 原始碼): 適用於一般用途的預訓練模型。
- u2netp (下載, 原始碼): u2net模型的輕量化版本。
- u2net_human_seg (下載, 原始碼): 用於人像分割的預訓練模型。
- u2net_cloth_seg (下載, 原始碼): 用於從人像中分析衣物的預訓練模型。這裡的衣物被解析成三個類別:上半身、下半身和全身。
- silueta (下載, 原始碼): 與u2net相同,但大小減少到43Mb。
- isnet-general-use (下載, 原始碼): 新的適用於一般用途的預訓練模型。
- isnet-anime (下載, 原始碼): 用於動漫角色的高精度分割模型。
- sam (下載編碼器, 下載解碼器, 原始碼): 適用於任何用途的預訓練模型。
u2net
- 檔案大小: 171,873KB
- 處理時間: 1.741s

u2netp
- 檔案大小: 4,468KB
- 處理時間: 0.702s

u2net_human_seg
- 檔案大小: 171,873KB
- 處理時間: 1.997s

u2net_cloth_seg
- 檔案大小: 172,066KB
- 處理時間: 5.496s
silueta
- 檔案大小: 43,138KB
- 處理時間: 5.496s

isnet-general-use
- 檔案大小: 174,461KB
- 處理時間: 2.629s

isnet-anime
- 檔案大小: 171,944KB
- 處理時間: 2.533s
