-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathsamm_utilitis.py
176 lines (127 loc) · 4.26 KB
/
samm_utilitis.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy as np
import sys
import math
import operator
import csv
import glob,os
import xlrd
import cv2
import pandas as pd
import os
import glob
from itertools import groupby
from sklearn.svm import SVC
from collections import Counter
from sklearn.metrics import confusion_matrix
import scipy.io as sio
from keras.models import Sequential
from keras.layers import LSTM, Dense, TimeDistributed
from keras.utils import np_utils
from keras import metrics
from keras import backend as K
from keras.models import model_from_json
import keras
from labelling import collectinglabel
from reordering import readinput
from evaluationmatrix import fpr
def Read_Input_Images_SAMM_CASME(inputDir, filteredSamples, ignoredSamples, dB, resizedFlag, table, workplace, spatial_size, channel, objective_flag):
# r=224; w=224
r=w=spatial_size
SubperdB=[]
# cross-checking parameter
subperdb_id = []
for sub in sorted([infile for infile in os.listdir(inputDir)]):
VidperSub=[]
vid_id = np.empty([0])
for vid in sorted([inrfile for inrfile in os.listdir(inputDir+sub)]):
path=inputDir + sub + '/'+ vid + '/'
# print(len(filteredSamples))
# print(filteredSamples)
# print("bohaha")
# filtered samples are samples needed
if path not in filteredSamples:
# print(path)
continue
# print(dB)
# print(path)
imgList=readinput(path)
numFrame=len(imgList)
# print(numFrame)
if resizedFlag ==1:
col=w
row=r
else:
img=cv2.imread(imgList[0])
[row,col,_l]=img.shape
## ##read the label for each input video
# print(sub[3:])
collectinglabel(table, sub, vid, workplace+'Classification/', dB, objective_flag)
for var in range(numFrame):
img=cv2.imread(imgList[var])
[_,_,dim]=img.shape
if channel == 1:
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
if resizedFlag ==1:
#in resize function, [col,row]
img=cv2.resize(img,(col,row))
if var==0:
FrameperVid=img.flatten()
else:
FrameperVid=np.vstack((FrameperVid,img.flatten()))
vid_id = np.append(vid_id, imgList[var]) # <--cross-check
VidperSub.append(FrameperVid)
subperdb_id.append(vid_id)# <--cross-check
# print(subperdb_id)
# if len(VidperSub) > 0:
# print(len(VidperSub))
SubperdB.append(VidperSub)
# return SubperdB, vid_id, subperdb_id
return SubperdB
def get_subfolders_num_crossdb(path, IgnoredSamples_index, sub_items, table, list_samples):
files = folders = 0
# print(path)
folders_array = np.empty([0])
subject_array = np.empty([0])
videos_array = np.empty([0])
for root, dirnames, filenames in os.walk(path):
discard_objective_flag = 0
files += len(filenames)
folders += len(dirnames)
# this line bypasses np.delete in 163
if len(dirnames) > 0 and len(subject_array) == 0:
subject_array = np.append(subject_array, dirnames)
elif len(dirnames) > 0:
folders_array = np.append(folders_array, len(dirnames))
videos_array = np.append(videos_array, (dirnames))
####### discard objective classses with 6 and 7 #######
item_array = []
for item in list_samples:
item = item.split('/', 1)[0]
if item in sub_items:
sub_items = np.delete(sub_items, 0)
else:
item_array += [item]
folders_array = [len(list(group)) for key, group in groupby(item_array)]
folders_array = [int(i) for i in folders_array]
# process list_samples, modify into proper path for later use
help_list_samples = []
for item in list_samples:
item = path + item + "/"
help_list_samples += [item]
list_samples = help_list_samples
# print(list_samples)
return folders_array, list_samples
def loading_samm_labels(root_db_path, dB, objective_flag):
label_filename = 'SAMM_Micro_FACS_Codes_v2.xlsx'
label_path = root_db_path + dB + "/" + label_filename
label_file = pd.read_excel(label_path, converters={'Subject': lambda x: str(x)})
# remove class 6, 7
if objective_flag:
label_file = label_file.ix[label_file['Objective Classes'] < 6]
# print(len(label_file)) # 68 samples
subject = label_file[['Subject']]
filename = label_file[['Filename']]
label = label_file[['Estimated Emotion']]
objective_classes = label_file[['Objective Classes']]
# print(label)
return subject, filename, label, objective_classes