前几天看抖音视频,发现有一个叫【嘴哥】的抖音号,把人的嘴巴放在了猫咪脸上,诙谐可爱,于是就在网上搜索了一下如何实现,本来以为要用到复杂的ps技术,没想到手边的python就可以做。
这里感谢原帖,内附了作者的源码,非常感谢!
没有学习过人脸识别的教程,自己的conda上面也没有装cv2和dlib,所以在安装这两个模块的时候,花费了不少时间,也走了不少弯路。这里准备在原帖的基础上加一些小白说明,帮助想实现的朋友,少走弯路。
这里需要提两点:
1、猫脸视频和人脸视频最好选取尺寸相当的,不要一个横板一个竖版;
2、猫脸视频和人脸视频最好选择目标对象动作比较少的,比如扭头、侧脸、低头等,这些动作越少越好。
这个是我通过python代码实现的,把提莫小姐姐的嘴巴装在了网红猫咪阿里多多酱脸上:
https://zhuanlan.zhihu.com/p/70471077/edit
话不多说,开始教程:
第一步:安装cv2和dlib
这两个库安装真的是意外地花费了我很长时间。
首先直接在cmd中pip install cv2的结果是这样的:
这里我们通过安装opencv解决:
pip install --upgrade setuptools
pip install opencv-python
关于dlib,直接pip安装也是会报错的,这里需要提前安装下:
boost和CMake
不过在安装CMake的过程中,也失败了很多次,网上搜索了很多方法,最后发现这个配置vs的可用,这里贴一个链接:
配置vs和安装CMake
当安装完所有的包之后,就可以运行代码了,代码原po主已经放了github地址,这里只做几点说明:
import cv2
import numpy as np
import dlib
#人脸检测
detector = dlib.get_frontal_face_detector()
#人脸landmark
PREDICTOR_PATH = "D:\\dlib\\shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(PREDICTOR_PATH)
#猫脸检测
cat_path = "D:\\anaconda\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalcatface.xml"
facecascade = cv2.CascadeClassifier(cat_path)
ret = facecascade.load(cat_path)
#猫脸landmark,这个没有搜到源码,暂时不做
old_cat_face_loc = np.array([-1,-1])
def human_mouth_paste_to_cat(human,cat):
global old_cat_face_loc
human_gray = cv2.cvtColor(human,cv2.COLOR_BGR2GRAY)
points_keys = []
#人脸检测
rects = detector(human_gray,1)
#人脸landmark检测
landmarks = np.matrix([[p.x,p.y] for p in predictor(human_gray,rects[0]).parts()])
landmarks = np.array(landmarks)
#mouth的landmark
mouth_landmark = landmarks[48:,:]
#扩个边
border=8
mouth = human[np.min(mouth_landmark[:,1])-border:np.max(mouth_landmark[:,1]+border),np.min(mouth_landmark[:,0])-border:np.max(mouth_landmark[:,0])+border,:]
mouth_landmark[:,0] -= (np.min(mouth_landmark[:,0])-border)
mouth_landmark[:,1] -= (np.min(mouth_landmark[:,1])-border)
#制作用于泊松融合的mask
mask=np.zeros((mouth.shape[0],mouth.shape[1],3)).astype(np.float32)
for i in range(mouth_landmark.shape[0]):#先画线
cv2.line(mask,(mouth_landmark[i,0],mouth_landmark[i,1]),(mouth_landmark[(i+1)%mouth_landmark.shape[0],0],mouth_landmark[(i+1)%mouth_landmark.shape[0],1]),(255,255,255),10)
mask_tmp=mask.copy()
for i in range(6,mask.shape[0]-6):#将线内部的范围都算作mask=255
for j in range(6,mask.shape[1]-6):
if(np.max(mask_tmp[:i,:j,:])==0 or np.max(mask_tmp[i:,:j,:])==0 or np.max(mask_tmp[:i,j:,:])==0 or np.max(mask_tmp[i:,j:,:])==0):
mask[i,j,:]=0
else:
mask[i,j,:]=255
#猫脸检测
width, height, channels = cat.shape
cat_gray = cv2.cvtColor(cat,cv2.COLOR_BGR2GRAY)
cat_face_loc= facecascade.detectMultiScale(cat_gray,scaleFactor = 1.1,minNeighbors=3,minSize=(100,100),flags=cv2.CASCADE_SCALE_IMAGE)
cat_face_loc = cat_face_loc[0]
if(old_cat_face_loc[0] != -1):#因为猫脸检测抖动太厉害,所以此处用历史坐标缓冲一下
cat_face_loc = 0.9*old_cat_face_loc + 0.1*cat_face_loc
old_cat_face_loc = cat_face_loc
center = (int(cat_face_loc[0]+cat_face_loc[2]/2), int(cat_face_loc[1]+cat_face_loc[3]*0.8))#0.8为手动设定的猫嘴位置,因为没找到猫脸landmark
normal_clone = cv2.seamlessClone(mouth, cat, mask.astype(mouth.dtype), center, cv2.NORMAL_CLONE)
#mixed_clone = cv2.seamlessClone(mouth, cat, mask.astype(mouth.dtype), center, cv2.MIXED_CLONE)
#cv2.imwrite("opencv-normal-clone-example.jpg", normal_clone)
#cv2.imwrite("opencv-mixed-clone-example.jpg", mixed_clone)
return normal_clone
#human = cv2.imread('bigmouth.jpg')
#cat = cv2.imread('cat.jpg')
#cat_with_human_mouth = human_mouth_paste_to_cat(human,cat)
human_video_cap = cv2.VideoCapture("human_video.mp4")
cat_video_cap =cv2.VideoCapture("cat_video2.mp4")
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
video_writer = cv2.VideoWriter('cat_with_humanmouth.avi', fourcc, 29.43, (720,1280))
index=0
while(True):
print(index)
index+=1
if(index>400):
break
human_ret,human_frame = human_video_cap.read()
human_frame = cv2.resize(human_frame,dsize=None,fx=2,fy=2)
cat_ret,cat_frame = cat_video_cap.read()
if(human_ret == True and cat_ret == True):
cat_with_human_mouth = human_mouth_paste_to_cat(human_frame,cat_frame)
video_writer.write(cat_with_human_mouth.astype(np.uint8))
video_writer.release()
以上。感谢阅读。