In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to use Python to make a Christmas hat for your avatar. The content is very detailed. Interested friends can refer to it for reference. I hope it can help you.
At this time of year, WeChat friends 'avatars will start to change into "Christmas" skin. The most common is to add a Christmas hat.
As a programmer, is there a way? have
Tools used:
OpenCV(after all, our main content is OpenCV...)
dlib(dlib's face detection works better than OpenCV, and dlib has keypoint detection that OpenCV doesn't.)
Languages used:
Python, but it can be changed to C++ version.
Material preparation
First we need to prepare a Christmas hat material, format *** PNG, because PNG then we can directly use Alpha channel as a mask.
The Christmas hat we use is as follows:
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)
In order to be able to calculate with rgb channel avatar image, we combine rgb three channels into an rgb color hat image.
The Alpha channel image is shown below:
Face detection and keypoint detection
We used the following image as our test image:
Below we use dlib's face detector for face detection, and use the model provided by dlib to extract five key points of the face.
The code is as follows:
# dlib Face Keypoint Detector predictor_path = "shape_predictor_5_face_landmarks.dat" predictor = dlib.shape_predictor(predictor_path) # dlib front face detector detector = dlib.get_frontal_face_detector() #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) #Keypoint detection, 5 keypoints 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 of the effect is as follows:
Resize the hat
We select two corners of the eye, find the center as the x direction of the hat reference coordinates, y direction coordinates with the face frame line y coordinates.
Then we adjust the hat size according to the size of the face detected, so that the hat size is appropriate.
#Select the corner of left and right eye point1 = shape.part(0) point2 = shape.part(2) Find the center of 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 according 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 according to face size resized_hat = cv2.resize(rgb_hat,(resized_hat_w,resized_hat_h))
Extract the hat and the area where the hat needs to be added
Remove the Alpha channel as a mask and negate it as described above. One of these masks is used to take out the hat area in the hat map, and the other is used to empty out the area that needs to be filled in the hat in the figure map.
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)
Take the area where you want to add the hat from the original image. Here we are using bitwise operations.
#hat offset relative to face box line dh = 0 dw = 0 #Original ROI # 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)] #Extract the hat area from the ROI of the original image 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 (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')
This is the background area (bg) shown below. You can see that the area where the hat needs to be filled is missing.
Then we extract the hat region with the following code:
#Extract hat area hat = cv2.bitwise_and(resized_hat,resized_hat,mask = mask)
The extracted hat area is shown below. The hat region complements the previous background region.
Add a Christmas hat
*** Put it back into the original picture, and you can get the Christmas hat picture we want.
Note here that resize before adding to ensure that the two sizes are the same, because it may be inconsistent due to rounding reasons.
#Make sure they are the same size before adding (may be inconsistent due to rounding) hat = cv2.resize(hat,(bg_roi.shape[1],bg_roi.shape[0])) #Add two ROI regions add_hat = cv2.add(bg,hat) # cv2.imshow("add_hat",add_hat) #Return the area where the hat was added to 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 result we get is shown below:
Merry Christmas to all!
About how to use Python to make a Christmas hat for the avatar to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.