parent
dcc253707b
commit
7219727dc4
@ -1,48 +0,0 @@
|
|||||||
#coding = utf-8
|
|
||||||
|
|
||||||
import cv2
|
|
||||||
import dlib
|
|
||||||
|
|
||||||
Base = 32
|
|
||||||
img = 'ym.jpg'
|
|
||||||
hat = 'ghat.png'
|
|
||||||
Model = 'shape_predictor_5_face_landmarks.dat'
|
|
||||||
|
|
||||||
image = cv2.imread(img)
|
|
||||||
greenhat= cv2.imread(hat,-1)
|
|
||||||
detector = dlib.get_frontal_face_detector()
|
|
||||||
predictor = dlib.shape_predictor(Model)
|
|
||||||
dets = detector(image,1)
|
|
||||||
|
|
||||||
|
|
||||||
for face in dets:
|
|
||||||
left,right,top,bot=face.left(),face.right(),face.top(),face.bottom()
|
|
||||||
shape = predictor(image,face)
|
|
||||||
point1 = shape.part(0)
|
|
||||||
point2 = shape.part(2)
|
|
||||||
# Midx Midy 眼睛的中间点
|
|
||||||
Midx = (point1.x+point2.x)//2
|
|
||||||
Midy = (point1.y+point2.y)//2
|
|
||||||
# cv2.circle(image,(Midx,Midy),3,(0,255,0))
|
|
||||||
width = int((right-left)*2.25)
|
|
||||||
height = int(width*greenhat.shape[0]/greenhat.shape[1])
|
|
||||||
# 将要加上去的绿帽的形状
|
|
||||||
greenhat = cv2.resize(greenhat,(width,height),cv2.INTER_CUBIC)
|
|
||||||
# cv2.imshow("greenhat",greenhat)
|
|
||||||
# cv2.waitKey(0)
|
|
||||||
Addx = width//2
|
|
||||||
# 有一个值得注意的就是不能数组越界
|
|
||||||
r,g,b,alpha = cv2.split(greenhat)
|
|
||||||
for y in range(Midy-height,Midy):
|
|
||||||
if y+Base < 0 or y+Base >= image.shape[0]: continue
|
|
||||||
for x in range(Midx-Addx,Midx-Addx+width):
|
|
||||||
if x < 0 or x >= image.shape[1] : continue
|
|
||||||
tmpx,tmpy = x-Midx+Addx,y-Midy+height
|
|
||||||
if alpha[tmpy][tmpx] >= 100:
|
|
||||||
image[y+Base][x] = (r[tmpy][tmpx],g[tmpy][tmpx],b[tmpy][tmpx])
|
|
||||||
|
|
||||||
|
|
||||||
cv2.imshow("output",image)
|
|
||||||
cv2.imwrite("output.jpg",image)
|
|
||||||
cv2.waitKey(0)
|
|
||||||
cv2.destroyAllWindows()
|
|
Before Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 132 KiB |
@ -1 +0,0 @@
|
|||||||
自行下载
|
|
Before Width: | Height: | Size: 66 KiB |
Loading…
Reference in new issue