Do yourself a matting, Python for batch Cutout with 5 lines of code

Foreword

For people who would PhotoShop, matting is a very simple operation, sometimes a few seconds to fasten a map. But some of the more complex diagram, sometimes still have to draw point in time, it gives us today with a very quick and easy way to use Python to pull the bulk to take portraits.

Show results

Go ahead, I am not optimistic about any automatic digging, always feel not precise enough, to pull not satisfied with the map. Here I'll just show you the effect of drawing. We take a look at picture
Here Insert Picture Description
this picture is not a solid color background, we usually use PhotoShop to pull it is relatively simple for us it is not a computer problem, here are renderings:
Here Insert Picture Description
because the picture itself is PNG, and artwork white background so I do not see any difference. In order to show the effect, I pull the original and put on a good view of yellow background image:
Here Insert Picture Description
this effect is obvious to see more, feel matting effect is still very good. But now, to pull this simple picture, not very fun, let us look a bit more complex picture:
Here Insert Picture Description
This picture background color complex than some, but there is a gradual, how matting effect after we take a look at:
Here Insert Picture Description
the original Figure background is not white, I do not get a yellow background, and quickly this effect is quite satisfactory, so much the character of the picture, let's look at this picture below:
Here Insert Picture Description
there are three people, we take a look at the program can automatically pull out :
Here Insert Picture Description
Although it is a bit flawed, but still very good, let's take a look at the last example:
Here Insert Picture Description
the front all the more complicated than that, then the results so far, we look at:
Here Insert Picture Description
Haha, not only to identify the person, also torch identified and pull out. Overall, in terms of complete figures matting or no problem.

surroundings

Read the results, it should be how to achieve it? That's where paddlepaddle, that paddlepaddle what is it? paddlepaddle depth study is an open source tool, we can use this tool to migrate only a dozen lines of code to achieve learning. Before using, let's install paddlepaddle, we can enter the official website https://www.paddlepaddle.org.cn/ , the official website with the installation instructions. For convenience, here the direct use pip install CPU version.
We execute the following statement:

python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

After installation is complete, we can test to see if successful in the environment. I am here using the command line window, run python.exe (provided that you have configured the environment variables)

C:\Users\zaxwz>python

Then run the following program code:

Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import paddle.fluid
>>> paddle.fluid.install_check.run_check()

If the console display Your Paddle is installed successfully! Let's start deep Learning with Paddle now on behalf of our success has been installed, here we can start writing code.

Cutout achieve

Cutout code implementation is very simple, roughly divided into the following steps:

  1. Import module
  2. Load Model
  3. Get a list of files
  4. Cutout

There's nothing more difficult to implement, easy to read the code, I will write the code more clearly:

# 1、导入模块
import os
import paddlehub as hub

# 2、加载模型
humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')

# 3、获取文件列表
# 图片文件的目录
path = 'D:/CodeField/Workplace/PythonWorkplace/PillowTest/11_yellow/img/'
# 获取目录下的文件
files = os.listdir(path)
# 用来装图片的
imgs = []
# 拼接图片路径
for i in files:
    imgs.append(path + i)
#抠图
results = humanseg.segmentation(data={'image':imgs})

We run in the console about this program:

D:\CodeField\Workplace\PythonWorkplace\PillowTest\11_yellow>python 抠图.py
[2020-03-10 21:42:34,587] [    INFO] - Installing deeplabv3p_xception65_humanseg module
[2020-03-10 21:42:34,605] [    INFO] - Module deeplabv3p_xception65_humanseg already installed in C:\Users\zaxwz\.paddlehub\modules\deeplabv3p_xception65_humanseg
[2020-03-10 21:42:35,472] [    INFO] - 0 pretrained paramaters loaded by PaddleHub

After the run is completed, we can see humanseg_output directory under the project, will pull a good picture stored in the directory. Of course, the above code we get the file list operation can also simplify it:

import os, paddlehub as hub
humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')		# 加载模型
path = 'D:/CodeField/Workplace/PythonWorkplace/PillowTest/11_yellow/img/'	# 文件目录
files = [path + i for i in os.listdir(path)]	# 获取文件列表
results = humanseg.segmentation(data={'image':files})	# 抠图

Then we completed the 5 lines of code batch matting.

Published 32 original articles · won praise 3684 · Views 230,000 +

Guess you like

Origin blog.csdn.net/ZackSock/article/details/104738652