-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataProcessor.py
112 lines (91 loc) · 3.69 KB
/
dataProcessor.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
import csv
# from statistics import median
def save_ear(ear_list, mar_list, filename, person):
# if not os.path.exists(f"{filename}.csv"):
# with open(f"{filename}.csv", mode="w") as train_file:
# file_write = csv.writer(
# train_file, delimiter=",", quoting=csv.QUOTE_MINIMAL
# )
# file_write.writerow(ear_list)
# file_write.writerow(mar_list)
# else:
with open(
f"{filename}_mear_PA{person}.csv", mode="a"
) as file: # change P3 in the file name with PA1, PA2 and so on for different person's video
file_write = csv.writer(file, delimiter=",", quoting=csv.QUOTE_MINIMAL)
file_write.writerow(ear_list)
file_write.writerow(mar_list)
# with open(f"{filename}_mar.csv", mode="a") as file:
# file_write = csv.writer(file, delimiter=",", quoting=csv.QUOTE_MINIMAL)
# file_write.writerow(mar_list)
def processingCSV(filename):
cleanedMAR = []
cleanedEAR = []
if filename == "Drowsy":
status = 1
else:
status = 0
with open(f"{filename}_mear.csv", mode="r") as file:
csv_read = csv.reader(file, delimiter=",")
count = 0
for row in csv_read:
# print(count)
if row != []:
mearList = []
for item in row:
mearList.append(float(item))
# print(mearList)
# if count % 2 == 0:
# medMEar.append(status)
# # print(medMEar)
# finalCSV(filename, medMEar)
# medMEar.clear()
# taking min/max value from 5 data
for x in range(0, len(mearList) + 1, 5):
if len(mearList) - x >= 5:
minimum = round(
min(
mearList[x],
mearList[x + 1],
mearList[x + 2],
mearList[x + 3],
mearList[x + 4],
),
2,
)
maximum = round(
max(
mearList[x],
mearList[x + 1],
mearList[x + 2],
mearList[x + 3],
mearList[x + 4],
),
2,
)
if status == 1 and count % 2 == 0:
cleanedEAR.append(minimum)
elif status == 0 and count % 2 == 1:
cleanedMAR.append(minimum)
elif status == 1 and count % 2 == 1:
cleanedMAR.append(maximum)
elif status == 0 and count % 2 == 0:
cleanedEAR.append(maximum)
count += 1
mearList.clear()
if count % 2 == 0:
print(cleanedEAR)
print(cleanedMAR)
cleanCSV(filename, cleanedEAR, cleanedMAR)
cleanedEAR.clear()
cleanedMAR.clear()
def cleanCSV(filename, cleanedEAR, cleanedMAR):
with open(f"{filename}_mear_cleaned.csv", mode="a") as file:
file_write = csv.writer(file, delimiter=",", quoting=csv.QUOTE_MINIMAL)
file_write.writerow(cleanedEAR)
file_write.writerow(cleanedMAR)
def main():
processingCSV("Drowsy")
processingCSV("NotDrowsy")
if __name__ == "__main__":
main()