Time：2020-1-18

With the coming of Christmas, I want to add a Christmas hat to my head. If it’s not a picture, add a Santa Claus.

Add a Christmas hat to the avatar in Python. I’ve read the articles from the great God in 2017: https://xuanlan.zhihu.com/p/32283641

Main process
Material preparation
Face detection and face key point detection

The face detector of Dlib is used for face detection, and five key points of face are extracted by the model provided by Dlib

``````#Dlib face key point detector
predictor\_path = "shape\_predictor\_5\_face\_landmarks.dat"
predictor = dlib.shape\_predictor(predictor\_path)

#Dlib 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)

#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()``````

## Adjust cap size, with cap

Select the points of two corners of the eye, and find the center as the reference coordinate of the X direction of the hat. The coordinate of the Y direction is represented by the Y coordinate of the line on the face frame. Then we adjust the size of the hat according to the size of the face detected to make the hat size appropriate.

``````#Select the corner of left and right eyes
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 based on 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 based on face size
resized\_hat = cv2.resize(rgb\_hat,(resized\_hat\_w,resized\_hat\_h))``````

Of course, some students’ heads are not characters or can’t be accurately identified. All of them are marked. (that is, add a small icon at the bottom right).

Small icons avoid monotony. Select one randomly from the icons:

The location of icons can also be adjusted according to hobbies

The code is as follows:

``````#Watermark image
num = random.randint(1, 5)
logo = Image.open("img\_icon/santa\_" + str(num) + ".png")

img = Image.open(imgPath)

print(img.size, logo.size)

layer = Image.new("RGBA", img.size, (255, 255, 255, 0))
layer.paste(logo, (img.size\[0\] - logo.size\[0\], img.size\[1\]-logo.size\[1\]))

Mule cover
img\_after = Image.composite(layer, img, layer)
# img\_after.show()
img\_after.save(outImgePath)``````

give the result as follows

Code acquisition: pay attention to the public address, reply: 20191224 or Christmas.

## Windows server side programming – Chapter 2 device IO and inter thread communication – 8 – the architecture of ports around I / O

Architecture of ports around I / O When the service application is initialized, I / O completion ports are created through functions such as createnewcompletionport. The application also needs to create a thread pool to handle client requests. Now the question is: how many threads should there be in the thread pool? This question is […]