0
1

More than 1 year has passed since last update.

Remove Image Background using Pythonを実験

Last updated at Posted at 2022-10-24

環境構築

Rembg
インストール。

└─$ pip install rembg

pipの位置がいろいろだめだとエラーになったのでリンクを張って再挑戦

└─$ sudo ln -s ~/.local/bin/pip /usr/bin/pip
└─$ sudo ln -s /usr/bin/pip /usr/bin/pip3
└─$ sudo ln -s /usr/bin/pip /usr/bin/pip3.10

└─$ pip install rembg

確認

└─$ rembg --help

コマンドがみつかりません、エラーなので調べたらなんだかな場所にあったのでPATHを通した。

└─$ sudo find / -name rembg 2>/dev/null
$HOME/.local/lib/python3.10/site-packages/rembg
$HOME/.local/bin/rembg

└─$ cat ~/.bashrc
export PATH="$PATH:$HOME/.local/bin"
└─$ . ~/.bashrc
└─$ printenv | grep PATH
(OK)

もう一度確認。

└─$ rembg --help
Usage: rembg [OPTIONS] COMMAND [ARGS]...
Options:
  --version  Show the version and exit.
  --help     Show this message and exit.
Commands:
  i  for a file as input
  p  for a folder as input
  s  for a http server

背景削除実験開始

Day 120 : Remove Image Background using Python
某ホテルのデザートビュッフェでの写真を試してみる。(ワクワク)
cake.jpeg

実験開始。

└─$ rembg i cake.jpeg cake.png
Access denied with the following error:

        Too many users have viewed or downloaded this file recently. Please
        try accessing the file again later. If the file you are trying to
        access is particularly large or is shared with many people, it may
        take up to 24 hours to be able to view or download the file. If you
        still can't access a file after 24 hours, contact your domain
        administrator. 

You may still be able to access the file from the browser:

         https://drive.google.com/uc?id=1tCU5MM1LhRgGou5OpmpjBQbSrYIUoYab 
・・・
onnxruntime.capi.onnxruntime_pybind11_state.NoSuchFile: [ONNXRuntimeError] : 3 : NO_SUCHFILE : Load model from ・・・/.u2net/u2net.onnx failed:Load model ・・・/.u2net/u2net.onnx failed. File doesn't exist

ファイルがないらしい。
こちらの記事「Remgbを使って画像から背景を削除してみた」を参考にエラーメッセージに表示されたモデルファイルを取得して指定された場所に配置する。

しかしwgetでもcurlでもダウンロードがうまくいかなかったので素直にブラウザーからダウンロードした。
https://drive.google.com/uc?id=1tCU5MM1LhRgGou5OpmpjBQbSrYIUoYab

これを以下に配置。

└─$ ls ~/.u2net/
u2net.onnx

再挑戦

└─$ rembg i cake.jpeg cake.png

おお!
cake.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1