A Python utility artifact with five lines of Python code for one-click bulk snapshots

Today, I'm sharing a Python look-alike and functional. In...

Today, I'm sharing a Python look-alike and functional.

In daily life or work, you often encounter people who want to cut out the characters in one photo and stitch them together into other pictures.Professionals can use PhotoShop's "Magic Wand" tool to cut maps, while non-professionals use a variety of Map APP s, but these two methods have limited processing power, can only process one picture at a time, and more complex images can take a long time.So today we're going to show you the third way to skip maps - using Python code to do one-click bulk skimming.

1. Preparations - Install paddlepaddle

Since you are trying to play hard, preparation is essential.So-called "standing on the shoulders of a giant, doing work with half the effort". Here the "giant" is paddlepaddle. The Chinese name is "paddle". So what is this paddlepaddle?

It is an "open source deep learning platform derived from industrial practice, dedicated to making deep learning technology innovation and application easier". To be frank, I help you to achieve the bottom level of deep learning framework. As long as you are creative, you can easily implement it with a small amount of simple code on my platform.Its official website is:https://www.paddlepaddle.org.cn/.

It is easy to install. There are installation instructions on the front page of the official website. You can find the details and precautions of each system installation through the Installation menu. As shown in the following figure, we use pip method to install the CPU version according to the installation instructions of the official website.

This paper takes the MacOS system as an example:

We first execute the following commands to install (Baidu source is recommended):

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

Or:

python3 -m pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple

From the installation process, you can see that when you install the paddlepaddle library, you need to install the following dependent libraries:

Installing collected packages: pathlib, click, joblib, regex, tqdm, nltk, gast, rarfile, pyyaml, funcsigs, paddlepaddle Running setup.py install for pathlib ... done Running setup.py install for regex ... done Running setup.py install for nltk ... done Running setup.py install for rarfile ... done Running setup.py install for pyyaml ... done Successfully installed click-7.1.2 funcsigs-1.0.2 gast-0.3.3 joblib-0.14.1 nltk-3.5 paddlepaddle-1.8.0 pathlib-1.0.1 pyyaml-5.3.1 rarfile-3.1 regex-2020.5.7 tqdm-4.46.0

After the installation is successful, we test the success of the installation 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:

➜ ~ python3 Python 3.7.4 (default, Jul 9 2019, 18:15:00) [Clang 10.0.0 (clang-1000.11.45.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import paddle.fluid >>> paddle.fluid.install_check.run_check() Running Verify Fluid Program ... Your Paddle Fluid works well on SINGLE GPU or CPU. W0512 17:41:31.037240 2844976000 build_strategy.cc:170] fusion_group is not enabled for Windows/MacOS now, and only effective when running with CUDA GPU. W0512 17:41:31.043959 2844976000 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. Your Paddle Fluid works well on MUTIPLE GPU or CPU. Your Paddle Fluid is installed successfully! Let's start deep Learning with Paddle Fluid now

If you see Your Paddle Fluid is installed successfully, the installation is successful.

2. Preparations - Install paddlehub

To fulfill the one-click batch buckle map requirements in this article, we need to use the PaddleHub portrait segmentation model.

PaddleHub is a pre-training model management tool based on PaddlePaddle, which can facilitate migration learning. Current pre-training models include image classification, target detection, lexical analysis, semantic model, affective analysis, video classification, image generation, image segmentation, text auditing, key point detection and other mainstream models.

PaddleHub:

https://www.paddlepaddle.org.cn/hub

PaddleHub project address:

https://github.com/PaddlePaddle/PaddleHub

More packed PaddleHub pre-training model tutorials are available:

https://aistudio.baidu.com/aistudio/course/introduce/1070

After describing the project, let's start installing paddlehub online:

pip install -i https://mirror.baidu.com/pypi/simple paddlehub

Or install as specified:

pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

Once the installation is complete, we can start using it.

3. Code implementation of one-button button chart

Our steps are simple:

  • Import Module
  • Load Model
  • Get picture files
  • Call Module Matching

The main function of snapshot is PaddleHub DeepLabv3+ model deeplabv3p_Xception65_Human seg.

Let's look at the implementation of the specific button chart code (demo.py):

import os import paddlehub as hub # Load Model humanseg = hub.Module(name='deeplabv3p_xception65_humanseg') base_dir = os.path.abspath(os.path.dirname(__file__)) # Get the current file directory path = os.path.join(base_dir, 'images/') # Get a list of files files = [path + i for i in os.listdir(path)] print(files) # Cutting Map results = humanseg.segmentation(data={'image': files}) for result in results: print(result)

In the example, I put pictures in the image folder of the code folder's sibling directory. After running the code, the output cutout pictures are automatically placed in the human seg_of the code folder's sibling directoryIn the output directory, the file name is the same as the name of the original picture, but the file format is png.

There are nine pictures in the example images catalog. To cater for different readers, the example pictures include both handsome and beautiful women. Their thumbnails are enlarged as follows:

After running the program, the above sample code runs as follows.

After successful run, in human seg_Nine pictures were generated in the output directory. Similarly, the result of the button chart is as follows:

We can see that the program recognizes the people in each picture (can be one person or more people) and cuts them out to make a picture with a white background.Although there are some minor flaws in the details, it looks good.

4. Points needing attention

When running the sample code, the model deeplabv3p_is not installed separatelyXception65_Human seg, which is automatically installed before execution by default.However, after the installation, the execution result did not produce a snapshot result and human seg_Output directory, the output looks like this:

Normally, when generating buckle chart data and printing results, the structure should be similar to the following:

You can solve this by installing the model separately and specifying an installation version.

hub install deeplabv3p_xception65_humanseg==1.0.0

The specific reason is not detailed. When the model is installed automatically by default, the version is 1.2.0. Guess it is due to incompatibility of the model version.

5. Summary

Based on paddlepaddle platform, this paper uses PaddleHub DeepLabv3+ model (deeplabv3p_Xception65_Human seg), using a simple five-line code to achieve bulk picking.Some readers might think that the example above provides more than five lines of code. In the example above, the main code that actually implements the buckle map only needs the following five lines:

humanseg = hub.Module(name='deeplabv3p_xception65_humanseg') base_dir = os.path.abspath(os.path.dirname(__file__)) path = os.path.join(base_dir, 'images/') files = [path + i for i in os.listdir(path)] results = humanseg.segmentation(data={'image': files})

Using the PaddleHub DeepLabv3+ model, not only one button button map but also picture and video compositions can be achieved.Making good use of it not only frees both hands and eyes, but also provides a valuable tool for the loading toolbox of some apes/programs.Next time when you meet a girl or a girl who is worried about making cuts, don't forget to pull out the magic weapon to win the heart!

paddlepaddle is an open source and in-depth learning platform. The button chart training model introduced in this paper is only the tip of the iceberg. There are more kinds of actual training prediction models. With more scenarios combined, readers can dig for themselves.

More good articles will be published as soon as possible under the public number "Test Development Technology" (mikezhou_talk), welcome to your attention!

19 May 2020, 22:40 | Views: 7565

Add new comment

For adding a comment, please log in
or create account

0 comments