Introduction:

The current global epidemic situation is grim and complex

The risk of the spread of the epidemic has increased

Remind everybody

Even after vaccination

Always wear a mask, too

Enhance personal Protection

​​

                                          

The new pneumonia outbreak, which began spreading in late 2019, affects people’s hearts.

As an individual, what we can do is to stay at home as little as possible.

Everyone started wearing masks even when they went out, so there were different types and sizes of masks involved.

So I spent some time writing a little program called the adjustable mask. It makes it easy to put a mask over a portrait in a picture, and the orientation and size of the mask is better for the face

The body of the

Installation environment

(1) Python3 can be installed directly;

pip install face-mask
Copy the code

(2) Directly specify the image path to be placed on the portrait mask in the image, will generate a new image (appended with **-with-mask** suffix) :

face-mask /path/to/face/picture
Copy the code

(3) Specify the **–show** option, and you can also use the default image viewer to open the newly generated image:

face-mask /path/to/face/picture --show
Copy the code

Effect:

Put a mask on the fairy sister.

​​

Put masks on multiple people.

Put masks on anime characters.

thinking

To achieve the above impact, what should we do? Think of it this way:

  • The first step is to identify the nose (bridge) and the face (chin)

  • The left, bottom and right points of the face are determined by the contour of the face

  • Determine the height and center line of the mask dimensions from the nose and bottom of the face

  • Divide the mask in half

  • Resize the left mask. Width is the distance from the left point of the face to the center line

  • Resize the right mask. Width is the distance from the right point of the face to the center line

  • Merge the left and right masks into the new mask

  • Rotate the new mask at the rotation Angle of the center line relative to the Y-axis

  • Put the new mask on top of the original

For face recognition, you can use the face_recognition library.

For image processing, you can use the Pillow library.

Face recognition

​​

With the help of the Face_recognition library it is easy to identify the portraits, and finally face_landmarks this is a list of each person face_landmark which means that a person is like data.

Face_landmark is a dictionary where the key represents a vertical feature and the value represents a list of points for that feature. Such as:

  • Key nose_bridge

  • Key chin

We need face_landmark to put on a mask with the corresponding face.

Get the nose and cheek feature points.

​​

With the code above, we get:

  • Nose_point represents the point above the bridge of the nose

  • Represents the left point chin_left_point

  • The right point chin_right_point

  • Represents the bottom chin_bottom_point of the plane

Split, scale, and merge masks

​​

The above code is mainly as follows:

  • Divide the mask in half

  • Resize the left mask. Width is the distance from the point on the left side of the face to the centerline * width factor 1.2

  • Resize the right mask. Width is the distance from the right point of the face to the centerline * width factor 1.2

  • Merge the left and right masks into the new mask

Get_distance_from_point_to_line Calculates the distance from a point to a line. The implementation can be seen in the source code.

Width_ratio width factor, used to properly expand the mask. The reason is that we calculate the width of the mask based on the width of the cheek, but the mask is on the ear, which should be wider.

Rotate the mask and place it in position

​​

The above code is mainly as follows:

  • Rotate the new mask at the rotation Angle of the center line relative to the Y-axis

  • Calculate the coordinates where the mask should be placed

  • Place the new mask under the calculated coordinates of the original image

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

conclusion

With the help of face_recognition library, we can easily identify the portrait, then calculate the size, direction and position of the 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.

Many people do not want to learn Python because there is no material or no one to guide them….

Source base private letter xiaobian: 06 can, including Python software tools and 2021 latest entry to the actual combat tutorial!