【问题标题】:Create an text file with numpy array of facial lanmark detection.?使用面部标志检测的numpy数组创建一个文本文件。?
【发布时间】: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

所以,在这里,我需要检测面部标志并直接读取它,以便它可以用于面部交换。或者,如果无法完成,我需要检测面部标志并立即将其写入不带括号的文本文件,这样我就不必两次读取和写入文本文件并删除括号。

【问题讨论】:

    标签: python numpy opencv dlib


    【解决方案1】:

    有一个名为 imutils 的包用于处理 dlib 面部标志。运行pip install imutils 进行安装。这是一个简单的方法

    from imutils import face_utils
    
    shape = predictor(img1, d)
    shape = face_utils.shape_to_np(shape)
    
    # access the x-coordinate point 20 
    x_20 = shape[20][0]
    
    # access the y-coordinate point 54 
    y_54 = shape[54][1]
    

    【讨论】:

      【解决方案2】:

      您确实需要以文本格式编写 numpy 矩阵数据并稍后删除括号。相反,numpy 已经提供了 np.save()np.load() 方法用于序列化和反序列化目的。

      我会在这里提供一个示例,另外最好将你的读写函数封装在不同的方法中,这样当你改变读/写逻辑时,你就不需要扫描整个代码。

      创建随机面部特征点:

      facial_points = np.zeros((68, 2), dtype=np.uint8)
      
      # Fill in some absurd values:
      for i in xrange(68):
          facial_points[i] = np.asarray([i, i%10])
      

      读写数据的实用方法:

      def serialize_feature_points(feature_points, file_path):
          np.save(file_path, feature_points)
      
      
      def deserialize_feature_points(file_path):
          return np.load(file_path)
      

      是时候采取一些行动了:

      serialize_feature_points(facial_points, "feature_points1.npy")
      print deserialize_feature_points("feature_points1.npy")
      
      [[ 0  0]
       [ 1  1]
       [ 2  2]
       .... 
       [65  5]
       [66  6]
       [67  7]]
      

      【讨论】:

        猜你喜欢
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-24
        • 1970-01-01
        • 1970-01-01
        • 2016-10-27
        • 2021-04-26
        相关资源
        最近更新 更多