-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathck_landmark.py
117 lines (88 loc) · 3.09 KB
/
ck_landmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pandas as pd
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
import shutil
data_path = '/media/ice/OS/Datasets/ck/extended-cohn-kanade-images/'
cropped_path = '/media/ice/OS/Datasets/ck/cropped_ck/'
annotation_path = '/media/ice/OS/Datasets/ck/Emotion_labels/'
def create_dir(data_path):
for root, file, images in os.walk(data_path):
if len(root) > 54 and len(root) <= 58:
root = root.replace('extended-cohn-kanade-images', 'cropped_ck')
# print(root)
os.mkdir(root)
for root, file, images in os.walk(data_path):
if len(root) > 58:
root = root.replace('extended-cohn-kanade-images', 'cropped_ck')
os.mkdir(root)
def read_data(data_path):
# getting the data path
folders = []
for root, file, images in os.walk(data_path):
if len(root) > 58:
for image in images:
file_path = root + "/" + image
folders += [file_path]
return folders
def crop_data(data_path):
# create directory in cropped folder
folders = read_data(data_path)
# create_dir(data_path)
for item in folders:
# landmarks
landmark_data = item.replace('.png', '_landmarks.txt')
landmark_data = landmark_data.replace('extended-cohn-kanade-images', 'Landmarks')
landmarks = pd.read_table(landmark_data, header=None, sep=" ", names=['X', 'Y'])
landmarks = landmarks.as_matrix()
landmarks = landmarks.astype(int)
left_most = min(landmarks[:, 0])
right_most = max(landmarks[:, 0])
up_most = max(landmarks[:, 1])
down_most = min(landmarks[:, 1])
image = cv2.imread(item)
image = image[down_most-20:up_most, left_most:right_most]
# saving/visualizing cropped image
target_path = item.replace('extended-cohn-kanade-images', 'cropped_ck')
cv2.imwrite(target_path, image)
print(target_path)
# implt = plt.imshow(image)
# plt.show()
def annotation_cross_check(cropped_path, annotation_path, remove_empty_annotation, remove_missing_labels):
existing_annotation = []
missing_annotation = []
no_annotation = []
existing_vid = []
for root, file, data in os.walk(annotation_path):
if len(root) > 46:
if len(data) > 0:
data = data[0]
data = data.replace("_emotion.txt", "")
elif len(data) == 0:
root = root.replace("Emotion_labels", "cropped_ck")
missing_annotation += [root]
for root, file, data in os.walk(cropped_path):
if len(root) > 41:
for item in data:
item = item.replace('.png', '')
for item in missing_annotation:
if item == root:
shutil.rmtree(item)
# remove folders that are without annotations
if remove_empty_annotation == True:
for item in missing_annotation:
item = item.replace("cropped_ck", "Emotion_labels")
shutil.rmtree(item)
for root, file, data in os.walk(annotation_path):
if len(root) > 46:
root = root.replace("Emotion_labels", "cropped_ck")
existing_annotation += [root]
for root, file, data in os.walk(cropped_path):
if len(root) > 41:
existing_vid += [root]
missing = list(set(existing_vid) - set(existing_annotation))
if remove_missing_labels == True:
for item in missing:
shutil.rmtree(item)
annotation_cross_check(cropped_path, annotation_path, False, True)