Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to put a mask on the profile picture of a female colleague

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use Python to put a mask on the profile picture of a female colleague. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

The sudden arrival of novel coronavirus made people all over the country panic during the Spring Festival, and it has been agreed that they must wear masks when they go out.

The picture is from Pexels

During the prevention and control of the epidemic, all we have to do is to follow the government's order: "go out less, wear masks, and wash your hands frequently." to be a good home is to contribute to the fight against the epidemic.

Seeing that some friends asked designers to help put masks on their avatars, as technicians, I thought that there must be more people with such demands, so it would be better to develop a simple program to achieve this requirement, which can also be regarded as helping the designer reduce her workload.

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

Use

① install face-mask

Pip install face-mask

Make sure the Python version is 3.6 or above.

② uses face-mask

Directly specify the image path to put on a mask for the portrait in the picture, and generate a new image (with an additional-with-mask suffix):

Face-mask / path/to/face/picture

You can also open a newly generated picture using the default picture viewer by specifying the-- show option:

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

③ effect

Put masks on anime characters:

Realize

Train of thought

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

The first is to identify a person's nose (nose_bridge) and face outline (chin).

The left point of the face (chin_left_point), the base point of the face (chin_bottom_point) and the right point of the face (chin_right_point) are determined by the face contour.

The height and centerline of the mask size are determined by the nose and the base of the face.

Divide the mask into two parts, adjust the width of the left mask to the distance from the left point to the centerline, adjust the width of the right mask to the distance from the right point to the centerline, and merge the left and right masks into a new mask.

Rotate the new mask at the angle of the centerline relative to the y-axis.

Put the new mask in place in the original picture.

About face recognition, we can use face_recognition [2] library for face recognition. For image processing, you can use the Pillow [3] library for processing.

Code

After having an idea, it is relatively easy to achieve. However, familiarity with the library and image transformation calculation may take some time.

For detailed code, please refer to the following link, which only explains the core steps below:

Https://github.com/Prodesire/face-mask

Face recognition:

Import face_recognition face_image_np = face_recognition.load_image_file ('/ path/to/face/picture') face_landmarks = face_recognition.face_landmarks (face_image_np)

Portraits can be easily identified with the help of the face_recognition library, and the resulting face_landmarks is a list in which each face_landmark represents a portrait data.

Face_landmark is a dictionary in which keys represent portrait features and values represent a list of points for that feature. For example:

The key nose_bridge indicates the bridge of the nose

The key chin indicates cheek

We need to put masks on the corresponding avatars according to each face_landmark.

Get the feature points of the nose and cheeks:

Import numpy as np nose_bridge = face_landmark ['nose_bridge'] nose_point = nose_ bridgeLen (nose_bridge) * 1 / / 4] nose_v = np.array (nose_point) chin = face_landmark [' chin'] chin_len = len (chin) chin_bottom_point = chinchin _ len / / 2] chin_bottom_v = np.array (chin_bottom_point) chin_left_point = chinchin _ len / / 8] chin_right_ Point = chinchin _ len * 7 / / 8

Through the above code, we have obtained:

A point that represents the bridge of the upper nose: nose_point

Indicates the left point of the face: chin_left_point

Indicates the right point of the face: chin_right_point

Indicates the base of the face: chin_bottom_point

Split, scale, and merge masks:

From PIL import Image _ face_img = Image.fromarray (face_image_np) _ mask_img = Image.open ('/ path/to/mask/picture') # split mask and resize width = _ mask_img.width height = _ mask_img.height width_ratio = 1.2 new_height = int (np.linalg.norm (nose_v-chin_bottom_v)) # left mask_left_img = _ mask_img.crop ((0,0, width / / 2) Height)) mask_left_width = get_distance_from_point_to_line (chin_left_point, nose_point, chin_bottom_point) mask_left_width = int (mask_left_width * width_ratio) mask_left_img = mask_left_img.resize ((mask_left_width, new_height)) # right mask_right_img = _ mask_img.crop ((width / / 2,0, width) Height)) mask_right_width = get_distance_from_point_to_line (chin_right_point, nose_point, chin_bottom_point) mask_right_width = int (mask_right_width * width_ratio) mask_right_img = mask_right_img.resize ((mask_right_width, new_height)) # merge mask size = (mask_left_img.width + mask_right_img.width, new_height) mask_img = Image.new ('RGBA' Size) mask_img.paste (mask_left_img, (0,0), mask_left_img) mask_img.paste (mask_right_img, (mask_left_img.width, 0), mask_right_img)

The above code mainly does the following:

Divide the mask into two parts on the left and right.

Resize the left mask to the width of the distance from the left point of the face to the centerline * width factor 1.2.

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

Merge the left and right masks into a new mask.

Get_distance_from_point_to_line is used to obtain the distance from a point to a line, as shown in the source code.

Width_ratio is the width factor that is used to enlarge the mask appropriately. 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 true width should be wider.

Rotate the mask and place it in the original position:

# rotate mask angle = np.arctan2 (chin_bottom_point [1]-nose_point [1], chin_bottom_point [0]-nose_point [0]) rotated_mask_img = mask_img.rotate (angle Expand=True) # calculate mask location center_x = (nose_point [0] + chin_bottom_point [0]) / / 2 center_y = (nose_point [1] + chin_bottom_point [1]) / / 2 offset = mask_img.width / / 2-mask_left_img.width radian = angle * np.pi / 180 box_x = center_x + int (offset * np.cos (radian))-rotated_mask_img.width / / 2 box_y = center _ y + int (offset * np.sin (radian))-rotated_mask_img.height / / 2 # add mask_ face_img.paste (mask_img (box_x, box_y), mask_img)

The above code mainly does the following:

Rotate the new mask at the angle of the centerline relative to the y-axis.

Calculate the coordinates in which the mask should be placed.

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

Finally, the new picture is saved to the local path, and the code is no longer displayed.

With the help of the 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 a picture of wearing the mask.

The whole process is not complicated, but we have to be extra careful in coordinate calculation, so we have created a short "automatic mask" program!

After reading the above, do you have any further understanding of how to use Python to put a mask on the profile picture of a female colleague? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 221

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report