Python example: one click bulk matting

Keywords: Programming Python pip network

Have you ever wanted to pick out the characters in one photo and then put them together in other pictures, so that even if you are in the end of the world, I can visit here?

Professional people can use PhotoShop's "magic wand" tool to matting, and non professional people can use a variety of beauty APP to achieve, but after all, their processing capacity is limited, only one image can be processed at a time, and more complex images may take a long time.

Today, I'm going to show you the third way - batch matting with Python one key.

preparation

Since we have to pretend to be forced, preparations are indispensable. The so-called "standing on the shoulders of giants, doing things twice as much as possible", our giant is paddlepaddle, the Chinese name is "flying Paddle", then what is this paddlepaddle?

It is "an open-source deep learning platform originated from industrial practice, dedicated to making the innovation and application of deep learning technology simpler". The straight white point is that I have helped you realize the underlying framework of deep learning. As long as you are creative, you can easily implement it on my platform with a small amount of simple code.

Its official website is https://www.paddlepaddle.org.cn/ .

Its installation is also relatively simple. There are installation instructions on the homepage of the official website. We use pip mode to install the CPU version according to the installation instructions on the official website.

Let's execute the statement first:

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

After the installation is successful, we test whether the installation is successful in the python environment (this is also done according to the official website guidelines). We switch to the python environment and run the following code:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import paddle.fluid
>>> paddle.fluid.install_check.run_check()
Running Verify Paddle Program ...
Your Paddle works well on SINGLE GPU or CPU.
I0506 21:47:48.657404 2923565952 parallel_executor.cc:440] The Program will be executed on CPU using ParallelExecutor, 2 cards are used, so 2 programs are executed in parallel.
W0506 21:47:48.658407 2923565952 fuse_all_reduce_op_pass.cc:74] Find all_reduce operators: 2. To make the speed faster, some all_reduce ops are fused during training, after fusion, the number of all_reduce ops is 1.
I0506 21:47:48.658516 2923565952 build_strategy.cc:365] SeqOnlyAllReduceOps:0, num_trainers:1
I0506 21:47:48.659137 2923565952 parallel_executor.cc:307] Inplace strategy is enabled, when build_strategy.enable_inplace = True
I0506 21:47:48.659595 2923565952 parallel_executor.cc:375] Garbage collection strategy is enabled, when FLAGS_eager_delete_tensor_gb = 0
Your Paddle works well on MUTIPLE GPU or CPU.
Your Paddle is installed successfully! Let's start deep Learning with Paddle now
>>>

You can see that your paddy is installed successfully.

We need to use the paddlehub tool of this platform next, so we also need to install the paddlehub: pip install -i https://mirror.baidu.com/pypi/simple paddlehub After the installation, we can start to use it.

code implementation

Our implementation steps are simple: import module - > Load Model - > get picture file - > call module matting.

Let's look at the code implementation:

import os, paddlehub as hub
huseg = hub.Module(name='deeplabv3p_xception65_humanseg') # Load model
path = './imgs/' # File directory
files = [path + i for i in os.listdir(path)] # Get file list
results = huseg.segmentation(data={'image': files}) # Matting

I put the picture in the imgs folder of the same level directory of the code folder. After running the code, the output matting picture will be automatically placed in the human SEG output directory of the same level directory of the code. The file name is the same as the original picture, but the file format is png.

I placed 5 pictures in the imgs directory. To facilitate the display, I put them together as screenshots:

After running the program, five pictures are generated under the directory of human SEG ﹣ output. Similarly, I put them together as screenshots:

We can see that the program recognizes the characters in each picture (it can be one or more) and extracts them to make a picture. The background is white.

Although there are some flaws in some details, it looks good.

This article is based on the paddlepaddle platform, using a simple five lines of code to achieve bulk matting. It not only liberates many people's hands and eyes, but also provides a treasure for some program monkey / program element toolbox.

Next time you meet a girl or a girl friend who is worried about scheming, don't forget to take out the artifact to win your heart!

Source network, for learning purposes only, invasion and deletion.

Don't panic. I have a set of learning materials, including 40 + E-books, 800 + teaching videos, involving Python foundation, reptile, framework, data analysis, machine learning, etc. I'm not afraid you won't learn! https://shimo.im/docs/JWCghr8prjCVCxxK/ Python learning materials

Pay attention to the official account [Python circle].

file

Posted by goaman on Tue, 12 May 2020 01:09:48 -0700