# Introduction to programming to abandonment

Time：2020-9-11

## preface

The new pneumonia epidemic, which starts to spread at the end of 2019, affects people’s hearts. As individuals, what we can do is to stay at home as much as possible and go out less.

Seeing that some friends asked design classmates to help them put on masks for their head portraits, as technicians, I thought that there must be more people who have such demands. It is better to develop a simple program to realize this demand, which can also be regarded as helping design sister reduce the workload.

So I spent some time writing a command-line tool called face mask [1], which can easily put on masks for people in pictures, and the direction and size of masks are adapted to face~

## use

### Installation`face-mask`

Make sure Python version is 3.6 and above

``pip install face-mask``

### Use`face-mask`

Directly specify the image path to put on the mask for the portrait in the picture, and a new image will be generated (additional`-with-mask`Suffix:

``face-mask /path/to/face/picture``

By specifying`--show`You can also use the default picture viewer to open a newly generated picture:

``face-mask /path/to/face/picture --show``

### effect

Put a mask on a person

## realization

### thinking

What should we do to achieve the above effect? Think of it this way:

• The first is to recognize the nose_ Bridge and chin

• The left point of the face (Chin) is determined by the contour of the face_ left_ Point, Chin_ bottom_ Point and chin_ right_ point）

• The height and center line of mask size are determined by nose and face bottom point

• Divide the mask into two parts

• Adjust the size of the left mask to the distance from the left point of the face to the center line

• Adjust the size of the right mask to the distance from the right point of the face to the center line

• Rotate the new mask at the angle of the center line relative to the Y axis

• Put the new mask in the proper position in the original picture

For face recognition, you can use face_ Recognition [2] library.

For image processing, we can use the pillow [3] library for processing.

### code

With ideas, implementation is a relatively easy thing. However, it may take some time to get familiar with the library and transform the images.

#### Face recognition

With the help of`face_recognition`Library can easily identify the portrait, and finally get the`face_landmarks`It’s a list, each of them`face_landmark`It means that a person is like data.

`face_landmark`Is a dictionary where the key represents the portrait feature and the value represents the list of points for that feature. For example:

• Key`nose_bridge`The bridge of the nose
• Key`chin`It means cheek

We need to base on each`face_landmark`Put a mask on the corresponding head portrait.

#### Get the feature points of nose and cheek

Through the above code, we obtain:

• A point representing the bridge of the upper nose`nose_point`
• Left point of face`chin_left_point`
• Right point of face`chin_right_point`
• It means the bottom of the face`chin_bottom_point`

#### Split, zoom and merge masks

The above code mainly includes the following contents:

• Divide the mask into two parts
• Adjust the size of the left mask. The width is the distance from the left point of the face to the center line * the width coefficient is 1.2
• Adjust the size of the right mask. The width is the distance from the right point of the face to the center line * the width coefficient is 1.2

`get_distance_from_point_to_line`Used to get a point to a line distance, the specific implementation can see the source code.

`width_ratio`Is the width factor, which is used to properly enlarge the mask. The reason is that we calculate the width of the mask according to the width of the cheek, but the mask stays on the ear, and the actual width should be wider.

#### Rotate the mask and place it in the proper position of the original picture

The above code mainly includes the following contents:

• Rotate the new mask at the angle of the center line relative to the Y axis
• Calculate the coordinates where the mask should be placed
• Put the new mask under the calculated coordinates of the original figure

Finally, the new image will be saved to the local path, and the code will not be displayed.

## summary

We use`face_recognition`The library can easily identify the portrait, and then calculate the size, direction and position of the exit mask according to the width of the cheek and the position of the bridge of the nose, and finally generate the picture of wearing the mask. The whole process is not complicated, but we should be very careful in coordinate calculation. In this way, we have created a short and concise “automatic mask on” program! Many little partners in the process of learning Python often do not want to learn because there is no information or guidance, so I specially prepared a large number of pdf books, video tutorials, are free to everyone! No matter you are zero foundation or have foundation, you can get the corresponding study gift bag! Jiaqun 592539176, including Python software tools and the latest introduction to the actual combat tutorial in 2019!