SoFunction
Updated on 2024-10-29

Python batch keying with 20 lines of code

preamble

Before Keying vs After Python Auto Keying

In our daily work and life, we often encounter the need to key the scene, even if there is only one picture to key, but also keyed us impatient, if you encounter many pictures need to key, this time your expression should be very interesting.

Python can be such a tool: in the case of only one picture, you need to carefully key out the characters, it can help you reduce the keying steps; in the case of multiple pictures need to key out, it can directly help you output the basic outlines of these characters, although not detailed enough, but also enough to use.

DeepLabv3+ is the latest in Google's DeepLab series of semantic segmentation networks, a model that can be used for portrait segmentation and supports any size of image input. If we implement this model by ourselves, it may be very troublesome, but fortunately, Baidu's paddle hub has helped us to realize it, we only need to load the model to segment the image.

1. Preparation

In order to realize this experiment, Python is essential, if you have not installed Python, we recommend reading this article:Super Detailed Python Installation Guide

Then we need to install Baidu's paddlepaddle, go into theirOfficial websiteThere are detailed guidelines

According to your own situation to choose these options, the last CUDA version, because this experiment does not need training data, and does not require too much computation, so directly choose the CPU version can be. Selection is complete, the following installation guidelines will appear, I have to say, Paddlepaddle these aspects of doing or more thoughtful (is the name is not good).

Be aware that if the name of the program in your Python3 environment variable is Python, remember to change the python3 xxx statement to Python xxx as follows to install it:

python -m pip install paddlepaddle -i /pypi/simple

2. Writing code

The whole procedure is divided into three steps:

1. Load model

2. Specify the directory of images to be keyed.

3. Keying

import os
import sys
import paddlehub as hub

# 1. Loading models
humanseg = (name="deeplabv3p_xception65_humanseg")

# 2. Specify the directory of images to be keyed.
path = './source/'
files = []
dirs = (path)
for diretion in dirs:
    (path + diretion)

# 3. Keying
results = (data={"image": files})

for result in results:
    print(result['origin'])
    print(result['processed'])

Not much, not less than 20 lines of code. After the keying is done, a folder called humanseg_output is created in the local folder. This is the folder where the keyed image is stored.

Material access address

3. Analysis of results

Have to admit, Google's algorithms are just plain awesome. As long as the background is a little better, the details of the key out of the details and manual keying are not similar, or even better than manual means.

However, in cases where the background and the color of the person are not similar, some problems arise, such as the result below:

The uncle in the back is completely ignored (begging for the uncle's inner shadow area). Nonetheless, this model is by far the strongest keyed model I've ever seen, bar none.

To this article on Python with 20 lines of code to achieve batch keying function is introduced to this article, more related Python batch keying content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!