diff --git a/greenhat/PyPy.py b/greenhat/PyPy.py new file mode 100644 index 0000000..a1c597f --- /dev/null +++ b/greenhat/PyPy.py @@ -0,0 +1,48 @@ +#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() diff --git a/greenhat/ghat.png b/greenhat/ghat.png new file mode 100644 index 0000000..dd012a4 Binary files /dev/null and b/greenhat/ghat.png differ diff --git a/greenhat/output.jpg b/greenhat/output.jpg new file mode 100644 index 0000000..68c91e0 Binary files /dev/null and b/greenhat/output.jpg differ diff --git a/greenhat/shape_predictor_5_face_landmarks.txt b/greenhat/shape_predictor_5_face_landmarks.txt new file mode 100644 index 0000000..46bbaca --- /dev/null +++ b/greenhat/shape_predictor_5_face_landmarks.txt @@ -0,0 +1 @@ +自行下载 diff --git a/greenhat/ym.jpg b/greenhat/ym.jpg new file mode 100644 index 0000000..0d728d0 Binary files /dev/null and b/greenhat/ym.jpg differ