-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_dataset.py
64 lines (58 loc) · 1.68 KB
/
form_dataset.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
import services.api
from os.path import isfile, join
import os
import time
import pandas as pd
# PARAMS
SLEEP_TIME = 1.2
CLASSES = {"alert": 0, "non_vigilant": 1, "tired": 2}
DIR = "non_vigilant" # TRAINING CLASS
MODE = "test"
OUTFILE = f"./dataset/{DIR}_{MODE}.csv"
# FINAL RESULT
res = {"anger": [],
"contempt": [],
"disgust": [],
"fear": [],
"happiness": [],
"neutral": [],
"sadness": [],
"surprise": [],
"class": []}
PATH = f"./dataset/{MODE}/{DIR}/"
files = [PATH + f for f in os.listdir(PATH) if isfile(join(PATH, f))]
api_lim_reached = False
all_files_read = False
file_ptr = 0
while not (api_lim_reached or all_files_read):
while file_ptr < len(files):
img = open(files[file_ptr], 'rb')
print(f"FILE SIZE for {files[file_ptr]}", os.stat(files[file_ptr]).st_size)
try:
face = services.api.face_detect(img, True)
for feature in face['emotion']:
try:
res[feature].append(face['emotion'][feature])
except Exception:
pass
res["class"].append(CLASSES[DIR])
except Exception as e:
print('Exception', e)
if str(e) == "API FAIL":
file_ptr += 1
continue
elif str(e) == "API LIMIT":
print("SLEEP START")
api_lim_reached = True
break
file_ptr += 1
if api_lim_reached:
time.sleep(SLEEP_TIME)
print("SLEEP COMPLETE")
api_lim_reached = False
else:
all_files_read = True
df = pd.DataFrame.from_dict(res)
print("CLASS", DIR)
print(df.head())
df.to_csv(OUTFILE, index=False)