Codes

Save 68 landmarks to TXT

Python Codes:

import cv2
import dlib
import numpy as np
import imutils
import os

def get_points(img_path):
    img = cv2.imread(img_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    detector = dlib.get_frontal_face_detector()
    detector_path = "D:/Anaconda3/Anaconda/Lib/site-packages/dlib/shape_predictor_68_face_landmarks.dat"
    predictor = dlib.shape_predictor(detector_path)
    dets = detector(gray, 1)
    pts = []
    for face in dets:
        shape = predictor(img, face)  # 68 landmarks on human face
        for pt in shape.parts():
            pt_pos = (pt.x, pt.y)
            pts.append(pt_pos)
    return pts

def write_txt(points_list, txt_path, count):
    with open(txt_path + "/" + "keypoints.txt", "a") as f:
        f.write("#" + str(count) + "\n")
        for points_tuple in points_list:
            f.write(str(points_tuple[0])+"," + str(points_tuple[1]) + "\n")

def main():
    path = "C:/Users/lenovo/Desktop/euromale"
    os.chdir(path)
    files = os.listdir(".")
    count = 1
    while count <= len(files):
        img_path = path + "/" + str(count) + ".jpg"
        write_txt(get_points(img_path), path, count)
        count = count + 1

if __name__ == "__main__":
    main()

Save Eyebrows / Eyes Landmarks to TXT

Python Codes:

import cv2
import dlib
import os

def get_points(img_path):
    img = cv2.imread(img_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    detector = dlib.get_frontal_face_detector()
    detector_path = "D:/Anaconda3/Anaconda/Lib/site-packages/dlib/shape_predictor_68_face_landmarks.dat"
    predictor = dlib.shape_predictor(detector_path)
    dets = detector(gray, 1)
    pts = []
    for face in dets:
        shape = predictor(img, face)  # 68 landmarks on human face
        for pt in shape.parts():
            pt_pos = (pt.x, pt.y)
            pts.append(pt_pos)
    return pts

def write_txt(points_list, txt_path, count):
    with open(txt_path + "/" + "keypoints_eyes-eyebrow.txt", "a") as f:
        f.write("#" + str(count) + "\n")
        f.write("left eyebrow (17-21): " + "\n")
        for points_tuple in points_list[17:22]:
            f.write(str(points_tuple[0])+"," + str(points_tuple[1]) + "\n")
        f.write("right eyebrow (22-26): " + "\n")
        for points_tuple in points_list[22:27]:
            f.write(str(points_tuple[0])+"," + str(points_tuple[1]) + "\n")
        f.write("left eye (36-41): " + "\n")
        for points_tuple in points_list[36:42]:
            f.write(str(points_tuple[0])+"," + str(points_tuple[1]) + "\n")
        f.write("right eye (42-47): " + "\n")
        for points_tuple in points_list[42:48]:
            f.write(str(points_tuple[0])+"," + str(points_tuple[1]) + "\n")  

def main():
    path = "C:/Users/lenovo/Desktop/euromale"
    os.chdir(path)
    files = os.listdir(".")
    count = 1
    while count <= len(files):
        img_path = path + "/" + str(count) + ".jpg"
        write_txt(get_points(img_path), path, count)
        count = count + 1

if __name__ == "__main__":
    main()

Save Eyebrows – Eyes Ratio to TXT

Python Codes:

import linecache

def get_line_content(txt_path, line_num):
    return linecache.getline(txt_path, line_num + 1).strip()

def get_eye_length(count, txt_path, position):
    with open(txt_path, "r") as f:
        point_left = get_line_content(txt_path, 27*(count-1)+position)
        point_right = get_line_content(txt_path, 27*(count-1)+position+3)
        point_left_x, point_left_y = int(point_left.split(",")[0]), int(point_left.split(",")[1])
        point_right_x, point_right_y = int(point_right.split(",")[0]), int(point_right.split(",")[1])
    return ((point_left_x-point_right_x)**2 + (point_left_y-point_right_y)**2)**0.5

def get_eyebrow_length(count, txt_path, position):
    with open(txt_path, "r") as f:
        point_left = get_line_content(txt_path, 27*(count-1)+position)
        point_right = get_line_content(txt_path, 27*(count-1)+position+4)
        point_left_x, point_left_y = int(point_left.split(",")[0]), int(point_left.split(",")[1])
        point_right_x, point_right_y = int(point_right.split(",")[0]), int(point_right.split(",")[1])
    return ((point_left_x-point_right_x)**2 + (point_left_y-point_right_y)**2)**0.5

# left eye position: 14; right eye position: 21; 
# left eyebrow position: 2; right eyebrow position: 8 

def write_txt(count, txt_path):
    with open("C:/Users/lenovo/Desktop/" + "eye-eyebrow ratio.txt", "a") as f:
        f.write(get_line_content(txt_path, 27*(count-1)) + "\n")
        f.write("left ratio\n")
        f.write(str(get_eyebrow_length(count, txt_path, 2)/get_eye_length(count, txt_path, 14)) + "\n")
        f.write("right ratio\n")
        f.write(str(get_eyebrow_length(count, txt_path, 8)/get_eye_length(count, txt_path, 21)) + "\n")

def main():
    txt_path = "C:/Users/lenovo/Desktop/keypoints_eyes-eyebrow.txt"
    count = 1
    file_length = len(open(txt_path).readlines())
    while count <= file_length/27:
        write_txt(count, txt_path)
        count = count + 1

if __name__ == "__main__":
    main()
Kathleen Li

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注