Have you ever wanted to cut people out of a picture and splice them into other pictures so that you could go anywhere and I could go?
Professional people can use the “magic wand” tool of PhotoShop to matting images, while non-professionals can use a variety of beautiful image apps to achieve this, but their processing capacity is limited after all, 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 a third approach — batch matting with Python.
The preparatory work
If you want to pretend, preparation is necessary. The so-called “standing on the shoulders of giants, getting twice the result with half the effort”, we here “giant” paddlepaddle, Chinese name is “flying paddlepaddle”, then what is this paddlepaddle?
It is “an open source deep learning platform derived from industrial practice, committed to making the innovation and application of deep learning technology easier”. Frankly speaking, I help you to implement the underlying framework of deep learning. As long as you are creative, you can easily achieve it on my platform with a small number of simple codes.
Its website is www.paddlepaddle.org.cn/.
It is also easy to install. There is an installation guide on the home page of the official website. Here, we use PIP to install the CPU version according to the installation guide on the official website.
We first execute the statement:
python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simpleCopy the code
After the installation is successful, let’s test the installation in Python (again follow the instructions on the official website). Let’s switch to Python 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 for trainers:1 I0506 21:47:48. 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 >>>Copy the code
If you see Your Paddle is installed successfully, the Paddle is installed successfully.
The next thing we need to use is the paddleHub tool for this platform, so we also need to install paddleHub: PIP install -i mirror.baidu.com/pypi/simple paddlehub after the installation is complete, we can start to use.
Code implementation
Our implementation steps are simple: import the module -> load the model -> get the image file -> call the module matting.
Let’s look at the code implementation:
import os, Paddlehub as hub huseg = hub.Module(name=' deeplabv3P_xception65_humanseg ') # load model path =' + I for I in os.listdir(path)] # Results = huseg.segmentation(data={'image': files}) # mattingCopy the code
I put the image in the imgs folder of the same directory as the code folder. After running the code, the output cutout image will be automatically placed in the humanseg_output directory of the same directory as the original image. The file name is the same as the original image, but the file format is PNG.
I have 5 images under the IMGS directory, and I have put them together in screenshots for ease of presentation:
After running the program, five images were generated in the humanseg_output directory. Again, I put them together for screenshots:
We can see that the program identifies one or more people in each image and plots them out, with a white background.
There are a few details that are a little flawed, but it looks good.
Based on paddlePaddle platform, this paper implements batch matting with five lines of simple code, which not only liberates the hands and eyes of many people, but also provides a valuable tool for some programmers.
The next time you meet a girl or best friend who is struggling with cutout, don’t forget to pull out your magic weapon and win her heart.
Wenyuan network, for learning purposes only, delete.
You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 800+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc. Shimo. Im/docs/JWCghr… Python Learning Materials
Follow the Python circle and get good articles delivered daily.