Codes

Plot Ratio Histograms

Python Codes:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import linecache

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

# left_ratio list: num = 2; right_ratio list: num = 4
def get_data_list(txt_path, num):
    ratio = []
    count = 0
    file_length = len(open(txt_path).readlines())
    while count <= file_length - 1:
        ratio.append(float(get_line_content(txt_path, count+num)))
        count = count + 5
    return ratio

def normfun(x,mu,sigma):
    pdf = np.exp(-((x-mu)**2)/(2*sigma**2))/(sigma*np.sqrt(2*np.pi))
    return pdf

def draw_hist(left_data, right_data):
    left_bins = np.linspace(min(left_data),max(left_data),80)
    plt.hist(left_data, left_bins, normed=1, facecolor='blue', alpha=0.5)
    # normal distribution
    left_mu =np.mean(left_data)
    left_sigma =np.std(left_data) 
    left_x = np.arange(min(left_data),max(left_data),0.001) 
    left_y = normfun(left_x, left_mu, left_sigma)
    plt.plot(left_x,left_y)
    plt.title(r'Eyebrow-Eye Histogram : $\mu=1.9790$,$\sigma=0.1888$')

    right_bins = np.linspace(min(right_data),max(right_data),80)
    plt.hist(right_data, right_bins, normed=1, facecolor='red', alpha=0.5)
    # normal distribution
    right_mu =np.mean(right_data)
    right_sigma =np.std(right_data) 
    right_x = np.arange(min(right_data),max(right_data),0.001) 
    right_y = normfun(right_x, right_mu, right_sigma)
    plt.plot(right_x,right_y)

    print("left:", left_mu, left_sigma)
    plt.show()

def main():
    txt_path = "D:/Lab/Jiaying_Li/keypoints/eye-eyebrow ratio/euro_male_eye-eyebrow ratio.txt"
    left_ratio = get_data_list(txt_path, 2)
    right_ratio = get_data_list(txt_path, 4)
    draw_hist(left_ratio, right_ratio)

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

发表回复

您的电子邮箱地址不会被公开。