-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_IAM.py
60 lines (49 loc) · 1.95 KB
/
clean_IAM.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
import os
import sys
from PIL import Image
import pandas as pd
import numpy as np
import json
import hw_utils
def main():
# Configuration file path, passed by arguments or default "./config.json".
if len(sys.argv) == 1:
print("Execution without arguments, config file by default: ./config.json")
config_file=str('./config.json')
elif len(sys.argv) == 2:
print("Execution with arguments, config file:" +str(sys.argv[1]))
config_file = str(sys.argv[1])
else:
print()
print("ERROR")
print("Wrong number of arguments. Execute:")
print(">> python3 clean_IAM.py [path_config_file]")
exit(1)
# We load the configuration file
try:
data = json.load(open(config_file))
except FileNotFoundError:
print()
print("ERROR")
print("No such config file : " + config_file)
exit(1)
# If the destination directory does not exist, it is created.
if not os.path.exists(str(data["general"]["processed_data_path"])):
os.mkdir(str(data["general"]["processed_data_path"]))
# list with all the files in the directory.
lstDir = os.walk(str(data["general"]["raw_data_path"]))
#The names of the suitable images are read from the csv.
df = pd.read_csv(str(data["general"]["csv_path"]), sep=",",index_col="index")
df = df.loc[:, ['image']]
lstIm = df.as_matrix()
# We go through the directory where the IAM images are stored.
for root, dirs, files in lstDir:
for file in files:
(name, ext) = os.path.splitext(file)
# We check if the file is in the apt image array.
if name in lstIm:
#Function "scale_invert" is executed
hw_utils.scale_invert(str(root)+str("/")+str(name+ext),
str(data["general"]["processed_data_path"])+str(name+ext),int(data["general"]["height"]),int(data["general"]["width"]))
if __name__ == "__main__":
main()