背景と人物の合成(Transparent-Background) †更新日2023-11-06 (月) 20:39:58
作成日:2023年9月4日
人物が写っている写真の背景を別の画像に背景に変更することが出来るソフト Transparent-Background コマンドで、人物のファイルと背景ファイルを指定するだけでOK。昔PhotoShopで苦労していたのは何?と思えるソフト Install方法 †pythonは既にインストールされていれば以下のコマンド一つでOK コマンドプロンプト(管理者)を起動 ''>pip install transparent-background"" >pip install transparent-background Collecting transparent-background Obtaining dependency information for transparent-background from \ https://files.pythonhosted.org/packages/8e/3a/a972483b1ea63e261eed2abed2e99da2dbc9a18e95b4c4ce7217952da9f9/\ transparent_background-1.2.4-py3-none-any.whl.metadata Downloading transparent_background-1.2.4-py3-none-any.whl.metadata (8.6 kB) Collecting torch>=1.7.1 (from transparent-background) Downloading torch-2.0.1-cp310-cp310-win_amd64.whl (172.3 MB) ---------------------------------------- 172.3/172.3 MB 9.0 MB/s eta 0:00:00 (略) Using cached MarkupSafe-2.1.3-cp310-cp310-win_amd64.whl (17 kB) Downloading soupsieve-2.5-py3-none-any.whl (36 kB) Downloading fsspec-2023.9.0-py3-none-any.whl (173 kB) ---------------------------------------- 173.2/173.2 kB ? eta 0:00:00 Installing collected packages: safetensors, soupsieve, pyvirtualcam, PySocks, \ opencv-python, MarkupSafe, fsspec, filelock, jinja2, huggingface-hub, \ beautifulsoup4, torch, gdown, torchvision, kornia, timm, transparent-background Successfully installed MarkupSafe-2.1.3 PySocks-1.7.1 beautifulsoup4-4.12.2 \ filelock-3.12.3 fsspec-2023.9.0 gdown-4.7.1 huggingface-hub-0.16.4 jinja2-3.1.2 kornia-0.7.0 \ opencv-python-4.8.0.76 pyvirtualcam-0.10.2 safetensors-0.3.3 soupsieve-2.5 \ timm-0.9.7 torch-2.0.1 torchvision-0.15.2 transparent-background-1.2.4 使用方法 †背景との合成 †
>transparent-background --source 00044-976353413.png --type 23-06-24_06-28-56_1519.png Settings -> Mode=base, Device=cpu, Torchscript=disabled 00044-976353413.png: 100%|■■■■■■■■■■■■■■■■■■■■■■■■■■| 1/1 [00:03<00:00, 3.79s/it] Done. Results are saved in \transparent-background作成画像\作成画像
切り抜き †sample1>transparent-background --source 00044-976353413.png --type map Settings -> Mode=base, Device=cpu, Torchscript=disabled 00044-976353413.png: 100%|■■■■■■■■■■■■■■■■■■■■■■■■■■| 1/1 [00:03<00:00, 3.56s/it] Done. Results are saved in \sample1 オリジナル MASK 背景をグリーン †\sample1>transparent-background --source 00044-976353413.png --type green Settings -> Mode=base, Device=cpu, Torchscript=disabled 00044-976353413.png: 100%|■■■■■■■■■■■■■■■■■■■■■■■■■■| 1/1 [00:03<00:00, 3.72s/it] Done. Results are saved in sample1 オリジナル GREEN Back 一括処理 †入力フォルダと出力フォルダ指定で一括処理ができる transparent-background --source input --type green --dest output 参考 †境界線の処理 †Transparent-Backgroundで合成した画像は背景との境界線が美しくないので、Stable Diffusionの「Inpaintアップロード」を使用して、境界線をぼかす。 準備物 †
解像度はすべての画像512x768でおこなった 手順 †
「マスクのぼかし」で境界線の調査、「CFGスケール」でマスクとのずれを調整する 実行結果 †人物がプロンプトで作成するため、少しかわるのと、マスクとのずれが気になる場合もある
髪の毛が奇麗になったサンプル †
参考 †Tiled Diffusionを利用して合成 †人物の表示位置を制御するために「Tiled Diffusion」を使用したとき、合成時のプロンプトは人物が表示した領域のプロンプトを使用する。また「Dynamic Prompts」の指定するときの表示種類(({cowboy shot| Three-Quarter Length}, standing))はおおもとののプロンプトとにそこ箇所のみ指定する。 Denoising strength(ノイズ除去強度)について †値が小さいほど、元の画像に近づく (ボケるようだ。)大きいほど、元画像とは異なる画像になる 0.1くらいで、マスクとの差がかなりなくなる。 CFG Scale †大きくすると、プロンプト重視、小さいほどAIの自由度大 |