cool hit counter Adding Christmas hats to avatars with Python_Intefrankly

Adding Christmas hats to avatars with Python


With Christmas around the corner, people have been @officials to add a Santa hat to their avatar. It is necessary for us to write a program to do this.

Tools used

OpenCV

dlib

processes

I. Preparation of material

First we need to prepare a Christmas hat material, preferably in PNG format, because with PNG we can use the Alpha channel directly as a mask. The Christmas hats we used are shown below.

We can get the alpha channel of the Christmas hat image by channel separation. The code is as follows.

r,g,b,a = cv2.split(hat_img)rgb_hat = cv2.merge((r,g,b))cv2.imwrite("hat_alpha.jpg",a)

To be able to work with the rgb channel of the avatar image, we combined the rgb three channels into one rgb colored hat map. The image of the Alpha channel is shown below.

II. Face Detection and Face Keypoint Detection

We used this image below as our test image.

In the following we use dlib's positive face detector for face detection and extract five key points of the face using the model provided by dlib. The code is as follows.

# dlibFace Keypoint Detector predictor_path = "shape_predictor_5_face_landmarks.dat" predictor = dlib.shape_predictor(predictor_path) # dlib Face detector detector = dlib.get_frontal_face_detector() # full-face detection dets = detector(img, 1) # If a face is detected if len(dets)>0: for d in dets: x,y,w,h = d.left(),d.top(), d.right()-d.left(), d.bottom()-d.top() # x,y,w,h = faceRect cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2,8,0) # Key point detection,5 Key Points shape = predictor(img, d) for point in shape.parts(): cv2.circle(img,(point.x,point.y),3,color=(0,255,0)) cv2.imshow("image",img) cv2.waitKey()

This part works as follows.

Three, adjust the size of the hat

We pick the points at the corners of the two eyes and find the center as the reference coordinate for placing the hat in the x-direction, and the y-direction is represented by the y-coordinate of the upper line of the face frame. Then we adjust the size of the hat based on the size of the face obtained from face detection to make the hat the right size.

# Select the point at the corner of the left and right eye point1 = shape.part(0) point2 = shape.part(2) # Find the center of the two points eyes_center = ((point1.x+point2.x)//2,(point1.y+point2.y)//2) # cv2.circle(img,eyes_center,3,color=(0,255,0)) # cv2.imshow("image",img) # cv2.waitKey() # Adjust hat size to face size factor = 1.5 resized_hat_h = int(round(rgb_hat.shape[0]*w/rgb_hat.shape[1]*factor)) resized_hat_w = int(round(rgb_hat.shape[1]*w/rgb_hat.shape[1]*factor)) if resized_hat_h > y: resized_hat_h = y-1 # Adjust hat size to face size resized_hat = cv2.resize(rgb_hat,(resized_hat_w,resized_hat_h)) four、 Extracting caps and areas where caps need to be added

Go to the Alpha channel as mask as described before. And beg to differ. One of these two masks is used to take the hat area out of the hat map, and one is used to empty the area of the figure map that needs to be filled with hats. You will see later.

# Use alpha channel as mask mask = cv2.resize(a,(resized_hat_w,resized_hat_h)) mask_inv = cv2.bitwise_not(mask)

Remove the area from the original image where the cap needs to be added, here we use a bitwise operation.

# Offset of the hat relative to the upper line of the face frame dh = 0 dw = 0 # original imageROI # bg_roi = img[y+dh-resized_hat_h:y+dh, x+dw:x+dw+resized_hat_w] bg_roi = img[y+dh-resized_hat_h:y+dh,(eyes_center[0]-resized_hat_w//3):(eyes_center[0]+resized_hat_w//3*2)] # original imageROI Extract the area where the hat is placed in bg_roi = bg_roi.astype(float) mask_inv = cv2.merge((mask_inv,mask_inv,mask_inv)) alpha = mask_inv.astype(float)/255 # Make sure they are the same size before multiplying them together( May be inconsistent due to rounding) alpha = cv2.resize(alpha,(bg_roi.shape[1],bg_roi.shape[0])) # print("alpha size: ",alpha.shape) # print("bg_roi size: ",bg_roi.shape) bg = cv2.multiply(alpha, bg_roi) bg = bg.astype('uint8')

The background area (bg) for this is shown below. As you can see, just the area that needs to be filled with hats is missing.

Then we extract the hat area.

# Extract hat region hat = cv2.bitwise_and(resized_hat,resized_hat,mask = mask)

The hat area obtained from the extraction is shown below. The hat area is exactly complementary to the previous background area.

V. Adding Christmas hats

Finally we add the two regions together. Put it back into the original image and you have the Christmas hat image we want. The important thing to note here is to resize before adding to ensure that the two are the same size, as they may not match due to rounding.

# Make sure they are the same size before adding them together( May be inconsistent due to rounding) hat = cv2.resize(hat,(bg_roi.shape[1],bg_roi.shape[0])) # bothROI Regional summation add_hat = cv2.add(bg,hat) # cv2.imshow("add_hat",add_hat) # Put the area where the hat was added back into the original image img[y+dh-resized_hat_h:y+dh,(eyes_center[0]-resized_hat_w//3):(eyes_center[0]+resized_hat_w//3*2)] = add_hat

The final result we get is shown below.


Recommended>>
1、New solution to sell inventory no cost to exchange good goods Download I love to exchange APP
2、Artificial intelligence cannot replace phrenology
3、babbling translators Please stop hawking artificial intelligence
4、What kind of code can be called flirty code
5、TokenBook区块链生态规划建设之TBMall商业模式说明会诚邀您来参加

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号