<jupyter_start><jupyter_text>Visualization of Center of the Eyes, Nose and Mouth  0. Import Packages<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
from tensorflow.keras.models import load_model
import os
import matplotlib.patches as patches
import random
from math import dist<jupyter_output><empty_output><jupyter_text>1. Make image dataset<jupyter_code>emotions = os.listdir('test')
emotions = np.array(emotions)
print(emotions)
for emo in emotions:
    globals()['x_test_{}'.format(emo)] = []
    globals()['name_{}'.format(emo)] = os.listdir('test/' + emo)
    for i in range(len(globals()['name_{}'.format(emo)])):
        img = Image.open('test/'+ emo + '/' + globals()['name_{}'.format(emo)][i]).convert("L")
        img_resize = img.resize((96,96))  # resize
        img_resize = np.array(img_resize)
        img_resize = img_resize/255  # normalize
        globals()['x_test_{}'.format(emo)].append(img_resize)

    globals()['x_test_{}'.format(emo)] = np.array(globals()['x_test_{}'.format(emo)])
np.shape(name_angry)
plt.rcParams["figure.figsize"] = (3,3)
ind = 111
plt.imshow(x_test_angry[ind,:,:], cmap = 'gray')
plt.axis('off')
plt.show()
print("Shape of image dataset :", x_test_angry.shape)<jupyter_output><empty_output><jupyter_text>2. Make landmark dataset<jupyter_code>for emo in emotions:
    globals()['y_test_{}'.format(emo)] = []
    name = globals()['name_{}'.format(emo)]
    for i in range(len(name)):
        name = globals()['name_{}'.format(emo)]
        pred = np.loadtxt('land_mark/' + emo + '/' + name[i][:-4] + '.txt')
        globals()['y_test_{}'.format(emo)].append(pred)

    globals()['y_test_{}'.format(emo)] = np.array(globals()['y_test_{}'.format(emo)])
print("Shape of pred :", y_test_angry.shape)
plt.imshow(x_test_angry[ind,:,:], cmap='gray')
plt.scatter(y_test_angry[ind,:][0:-1:2], y_test_angry[ind,:][1::2], c ='r')  # divide x-coordinate and y-coordinate
plt.axis("off")
plt.show()
print("Shape of prediction dataset :", y_test_angry.shape)<jupyter_output><empty_output><jupyter_text>3. Center points<jupyter_code>keypts = pd.read_csv("facial_keypoints.csv", header = None)
location = keypts.to_numpy()
print(location)
print(location.shape)
center_inds = np.array([0, 2, -10, -2])
center_pts_x = location[:, center_inds]
center_pts_y = location[:, center_inds + 1]

print(center_pts_x, "\n", center_pts_y)
plt.rcParams["figure.figsize"] = (16, 40)

for i in range(len(emotions)):
    index = 22
    plt.subplot(1, 7, i+1)
    
    plt.imshow(globals()['x_test_{}'.format(emotions[i])][index,:,:], cmap='gray')
    plt.scatter(globals()['y_test_{}'.format(emotions[i])][index,:][center_inds], 
                globals()['y_test_{}'.format(emotions[i])][index,:][center_inds+1], c ='r')  
    plt.title(emotions[i])
    plt.axis("off")
plt.show()    
center_pts_x = location[:, center_inds]
center_pts_y = location[:, center_inds + 1]

print(center_pts_x, "\n", center_pts_y)
center_inds<jupyter_output>[['left_eye_center_x' 'right_eye_center_x' 'nose_tip_x'
  'mouth_center_bottom_lip_x']] 
 [['left_eye_center_y' 'right_eye_center_y' 'nose_tip_y'
  'mouth_center_bottom_lip_y']]<jupyter_text>4. Distances of center points<jupyter_code>def dist_pts(x_pts, y_pts):
    
    left_eye = (x_pts[0], y_pts[0])
    right_eye = (x_pts[1], y_pts[1])
    nose = (x_pts[2], y_pts[2])
    mouth = (x_pts[3], y_pts[3])
    
    left_eye_nose = dist(left_eye, nose)
    right_eye_nose = dist(right_eye, nose)
    nose_mouth = dist(mouth, nose)
    
    return left_eye_nose, right_eye_nose, nose_mouth
distance = []
for i in range(len(emotions)):

    name = globals()['name_{}'.format(emotions[i])]
    sum1 = 0
    sum2 = 0
    for j in range(len(name)):
        x = np.zeros(4)
        for k in range(4):
            x[k] = globals()['y_test_{}'.format(emotions[i])][j, center_inds[k]]
            y[k] = globals()['y_test_{}'.format(emotions[i])][j, center_inds[k] + 1]
        leye_nose, reye_nose, nose_mouth = dist_pts(x, y)
        sum1 += leye_nose + reye_nose
        sum2 += nose_mouth
    
    mean1 = sum1/(len(name)*2)
    mean2 = sum2/len(name)
    
    distance.append([mean1, mean2])
    
distance = np.array(distance)    
print(distance.shape)
print(emotions)
plt.rcParams["figure.figsize"] = (20,5)
plt.bar(emotions, distance[:,0]/np.max(distance[:,0]))
plt.xticks(fontsize = 15)
plt.title("Distance of eyes ~ nose", fontsize = 17)
plt.show()

print("Max :", emotions[np.argmax(distance[:,0])])
print("Min :", emotions[np.argmin(distance[:,0])])
plt.rcParams["figure.figsize"] = (20,5)
plt.bar(emotions, distance[:,1]/np.max(distance[:,1]))
plt.xticks(fontsize = 15)
plt.title("Distance of nose~mouth", fontsize = 17)
plt.show()

print("Max :", emotions[np.argmax(distance[:,1])])
print("Min :", emotions[np.argmin(distance[:,1])])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18773081657775953, 'fraction_numerical': 0.01764464505539598, 'mean_word_length': 4.930656934306569, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '159400', 'license': 'MIT', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1908, 'n_words': 311, 'path': 'Visualization_of_Face_landmark.ipynb', 'repo_name': 'OH-Seoyoung/Facial_Emotion_Recognition', 'size': '4874'}