-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsequence3.py
195 lines (180 loc) · 8.33 KB
/
sequence3.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import numpy as np
import tensorflow as tf
import argparse
def get_idx(numbers, split_idx):
while (numbers[split_idx] !=0 or numbers[split_idx+1] !=0 or numbers[split_idx+2] !=0 or numbers[split_idx+3] !=0
or numbers[split_idx+4] !=0 or numbers[split_idx+5] !=0 or numbers[split_idx+6] != 0 or numbers[split_idx+7] !=0
or numbers[split_idx+8] !=0 or numbers[split_idx+9] !=0 or numbers[split_idx+10] != 0 or numbers[split_idx+11] !=0
or numbers[split_idx+12] !=0 or numbers[split_idx+13] !=0 or numbers[split_idx+14] != 0 or numbers[split_idx+15] !=0
or numbers[split_idx+16] !=0 or numbers[split_idx+17] !=0 or numbers[split_idx+18] != 0 or numbers[split_idx+19] !=0
or numbers[split_idx+20] !=0 or numbers[split_idx+21] !=0 or numbers[split_idx+22] != 0 or numbers[split_idx+23] !=0
or numbers[split_idx+24] !=0 or numbers[split_idx+25] !=0 or numbers[split_idx+26] != 0 or numbers[split_idx+27] !=0
or numbers[split_idx+28] !=0 or numbers[split_idx+29] !=0 or numbers[split_idx+30] != 0 or numbers[split_idx+31] !=0
or numbers[split_idx+32] !=0 or numbers[split_idx+33] !=0 or numbers[split_idx+34] != 0 or numbers[split_idx+35] !=0
or numbers[split_idx+36] !=0 or numbers[split_idx+37] !=0 or numbers[split_idx+38] != 0 or numbers[split_idx+39] !=0
or numbers[split_idx+40] !=0 or numbers[split_idx+41] !=0 or numbers[split_idx+42] != 0 or numbers[split_idx+43] !=0
or numbers[split_idx+44] !=0 or numbers[split_idx+45] !=0 or numbers[split_idx+46] != 0 or numbers[split_idx+47] !=0
or numbers[split_idx+48] !=0 or numbers[split_idx+49] !=0 or numbers[split_idx+50] != 0 or numbers[split_idx+51] !=0
or numbers[split_idx+52] !=0 or numbers[split_idx+53] !=0 or numbers[split_idx+54] != 0 or numbers[split_idx+55] !=0
or numbers[split_idx+56] !=0 or numbers[split_idx+57] !=0 or numbers[split_idx+58] != 0 or numbers[split_idx+59] !=0
or numbers[split_idx+60] !=0 or numbers[split_idx+61] !=0 or numbers[split_idx+62] != 0 or numbers[split_idx+63] !=0
or numbers[split_idx+64] !=0 or numbers[split_idx+65] !=0 or numbers[split_idx+66] != 0 or numbers[split_idx+67] !=0
or numbers[split_idx+68] !=0 or numbers[split_idx+69] !=0 or numbers[split_idx+70] != 0 or numbers[split_idx+71] !=0
or numbers[split_idx+72] !=0 or numbers[split_idx+73] !=0 or numbers[split_idx+74] != 0 or numbers[split_idx+75] !=0
or numbers[split_idx+76] !=0 or numbers[split_idx+77] !=0 or numbers[split_idx+78] != 0 or numbers[split_idx+79] !=0
or numbers[split_idx+80] !=0 or numbers[split_idx+81] !=0 or numbers[split_idx+82] != 0 or numbers[split_idx+83]!=0):
split_idx += 1
return split_idx
def split_list3(numbers):
while numbers[0] == 0:
numbers = numbers[1:]
split_idx = get_idx(numbers,0)
number2 = numbers[split_idx:]
number1 = numbers[:split_idx]
while number2[0] == 0:
number2 = number2[1:]
split_idx1 = get_idx(number2,0)
number3 = number2[split_idx1:]
number2 = number2[:split_idx1]
while number3[0] ==0:
number3 = number3[1:]
return number1, number2, number3
def load_data3(dirname):
listfile = os.listdir(dirname)
X1 = []
X2 = []
X3 = []
Y = []
for file in listfile:
wordname = file
textlist = os.listdir(dirname + wordname)
###################### Xu ly txt file #######################
for text in textlist:
if "DS_" in text:
continue
textname = dirname + wordname + "/" + text
numbers = []
with open(textname, mode='r') as t:
numbers = [float(num) for num in t.read().split()]
number1, number2, number3 = split_list3(numbers)
print("Do dai file txt tu thu nhat: " + str(len(number1)))
print("Do dai file txt tu thu hai: " + str(len(number2)))
print("Do dai file txt tu thu hai: " + str(len(number3)))
print("===================================")
for i in range(len(number1), 4200):
number1.extend([0.000])
for i in range(len(number2), 4200):
number2.extend([0.000])
for i in range(len(number3), 4200):
number3.extend([0.000])
landmark_frame1 = []
row1 = 0
for i in range(0, 35):
landmark_frame1.extend(number1[row1:row1 + 84])
row1 += 84
landmark_frame1 = np.array(landmark_frame1)
landmark_frame1 = landmark_frame1.reshape(-1, 84)
landmark_frame2 = []
row2 = 0
for i in range(0, 35):
landmark_frame2.extend(number2[row2:row2 + 84])
row2 += 84
landmark_frame2 = np.array(landmark_frame2)
landmark_frame2 = landmark_frame2.reshape(-1, 84)
landmark_frame3 = []
row3 = 0
for i in range(0, 35):
landmark_frame3.extend(number3[row3:row3 + 84])
row3 += 84
landmark_frame3 = np.array(landmark_frame3)
landmark_frame3 = landmark_frame3.reshape(-1, 84)
X1.append(np.array(landmark_frame1))
X2.append(np.array(landmark_frame2))
X3.append(np.array(landmark_frame3))
Y.append(wordname)
##################################################################
x1_train = np.array(X1)
x2_train = np.array(X2)
x3_train = np.array(X3)
Y = np.array(Y)
print(Y)
return x1_train, x2_train, x3_train, Y
def load_label():
listfile = ['Cách ly', 'Cảm ơn', 'CoronaCovid19', 'Ho', 'Khẩu trang', 'Lây lan', 'Mọi người', 'Rửa tay', 'Sốt',
'Xà phòng']
label = {} # khởi tạo 1 dict
count = 1
for l in listfile:
if "_" in l:
continue
label[l] = count
count += 1
return label
def main(dirname):
listfile=os.listdir(dirname)
for file in listfile:
if "_" in file:
continue
wordname=file
textlist=os.listdir(dirname+wordname)
for text in textlist:
if "DS_" in text:
continue
textname=dirname+wordname+"/"+text
numbers=[]
with open(textname, mode = 'r') as t:
numbers = [float(num) for num in t.read().split()]
print("Do dai file txt ban dau: " + str(len(numbers)))
while numbers[0] == 0:
numbers = numbers[1:]
print("Do dai file txt luc sau: " + str(len(numbers)))
y = len(numbers)
x1_test, x2_test, x3_test, Y=load_data3(dirname)
#Load model & labels
new_model = tf.keras.models.load_model('model.h5')
labels=load_label()
print(labels)
#Predict
y1hat = new_model.predict(x1_test)
y2hat = new_model.predict(x2_test)
y3hat = new_model.predict(x3_test)
predictions1 = np.array([np.argmax(pred) for pred in y1hat])
predictions2 = np.array([np.argmax(pred) for pred in y2hat])
predictions3 = np.array([np.argmax(pred) for pred in y3hat])
print("pre1, pre2 & pre3")
print(predictions1)
print(predictions2)
print(predictions3)
rev_labels = dict(zip(list(labels.values()), list(labels.keys())))
print("rev_labels:")
print(rev_labels)
txtpath=dirname+"sequence.txt"
s=0
with open(txtpath, "w") as f:
for i in range(len(predictions1)):
f.write("true_label: ")
f.write(Y[s])
f.write(" === ")
f.write("Predict sequence: ")
for j in range(len(predictions1)):
if j==i:
f.write(rev_labels[predictions1[j]])
f.write(" ")
for k in range(len(predictions2)):
if k==i:
f.write(rev_labels[predictions2[k]])
f.write(" ")
for m in range(len(predictions3)):
if m==i:
f.write(rev_labels[predictions3[m]])
f.write(" ")
f.write("\n")
s+=1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Predict Sign language with Mediapipe')
parser.add_argument("--dirname",help=" ")
args=parser.parse_args()
dirname=args.dirname
main(dirname)