Better than PHOTOSHOP! Python batch matting of 20 lines of code


Before matting vs After Python matting automatically

In daily work and life, we often encounter scenes that need to matting. Even if there is only one picture to be pinched, we will get impatient. If there are many pictures to be pinched, your expression will be very interesting.

Python can be a tool to reduce the number of matting steps you need to take when there is only one image and you need to carefully figure out the person. In the case of a number of pictures need to be scratched, can directly help you output the basic outline of these figures, although not detailed enough, but also enough.

DeepLabv3+ is the latest addition to Google’s DeepLab semantic segmentation network. This model can be used for portrait segmentation and supports image input of any size. If we implemented this model ourselves, it would have been very troublesome, but fortunately baidu’s Paddle Hub has already done this for us, and we just need to load the model to segment the image.

1. Prepare

In order to implement this experiment, Python is essential, and if you haven’t already installed Python, you are advised to read our article: An Ultra-detailed Python Installation Guide.

Then, we need to install the baidu paddlepaddle, enter their official website has detailed guidance: www.paddlepaddle.org.cn/install/qui…

Choose these options according to your own situation, the last CUDA version, since this experiment does not require training data and does not require too much computation, just choose the CPU version. And when you’re done, you’ll see the installation guide at the bottom, and Paddlepaddle, I have to say, does a pretty good job of that

(Bad name)

Note that if the program name in your Python3 environment variable is Python, remember to change the statement to Python XXX and install it as follows:

python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simpleCopy the code

2. Write code

The whole process is divided into three steps:

1. Load the model

2. Specify the image directory to be extracted

3. The cutout

import osimport sysimport paddlehub as hubHumanseg = hub.Module(name="deeplabv3p\ _xception65_humanseg ")# 2. /source/'files = \[\]dirs = os.listdir(path)for diretion in dirs: files.append(path + diretion)# 3. Results = humanseg. Segmentation (data={"image": files})for result in results: print(result\['origin'\]) print(result\['processed'\])Copy the code

No more, no less 20 lines of code. A folder called humanseg_output is generated in the local folder after the cutout is complete. This store is already matting successful pictures.

This article contains 20 lines of code and all images. Please follow this article at the bottom of the public account.

3. Result analysis

I have to admit, Google’s algorithm is pretty lame. As long as the background was good, the details were as good as manual ones, or even better than manual ones.

However, there are some problems when the background and the person are the same color, such as the following result:

The uncle in the back is completely ignored. Still, this model is by far the strongest cutout model I’ve seen.

That’s the end of our article, if you’d like us to todayPython tutorial, please continue to follow us, if it helps you, please click a “like” on the following pageIf you have any questions, please leave them in the comments section below and we will be patient to answer them!


Python Dict.com Is more than a dictatorial model