【发布时间】:2019-05-25 02:45:13
【问题描述】:
我正在尝试制作具有面部标志检测代码的面部交换应用程序。但是,由于我是编程世界的新手,所以我的代码比需要的要长。我知道,有一些简短的方法可以做到这一点,我只是不知道如何。所以。这是我的代码:
predictor_path = "C:\\Users\\G7K4\\Desktop\\FinalFaceSwap\\shape_predictor_68_face_landmarks.dat"
filepath1 = "C:\\Users\\G7K4\\Desktop\\FinalFaceSwap\\Image\\nil.jpg"
image1 = cv2.imread(filepath1)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
dets1 = detector(image1)
for k, d in enumerate(dets1):
shape = predictor(img1, d)
#Detect 68 facial landmark points
vec = np.empty([68, 2], dtype = int)
for b in range(68):
vec[b][0] = shape.part(b).x
vec[b][1] = shape.part(b).y
#write the detected file in text file
with open("Model1.txt","w") as file:
for i in range(len(vec)):
outer=""
outer += str(vec[i])
file.write(outer)
file.write("\n")
#read the text file and remove the brackets
with open("Model1.txt","r") as my_file:
text=my_file.read()
text= text.replace("[","")
text= text.replace("]","")
#again write the file.
with open("Model1.txt","w") as file:
file.write(text)
#function for reading points from text file
def readPoints(path) :
# Create an array of points.
points = [];
# Read points
with open(path) as file :
for line in file :
x, y = line.split()
points.append((int(x), int(y)))
return points
所以,在这里,我需要检测面部标志并直接读取它,以便它可以用于面部交换。或者,如果无法完成,我需要检测面部标志并立即将其写入不带括号的文本文件,这样我就不必两次读取和写入文本文件并删除括号。
【问题讨论】: