-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataLoaderClass.py
76 lines (54 loc) · 2.19 KB
/
dataLoaderClass.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
from base import *
from matplotlib import pyplot as plt
from filters.spatialFilters import spatialFilterClass
from filters.frequencyFilters import frequencyFilterClass
class dataLoader:
def __init__(self, cfg):
self.cfg = cfg
self.spatial = cfg["spatial"]
self.frequency = cfg["frequency"]
self.objS = spatialFilterClass(cfg)
self.objF = frequencyFilterClass(cfg)
def loadImage(self):
if self.cfg["type"] == "image":
imagePath = Image.open(self.cfg["path"]) #Loads Image
imgResize = imagePath.resize((self.cfg["height"],self.cfg["width"])) #Resizes Image
self.img = np.array(imgResize) #Converts to numpy array
self.imgPadded = np.pad(self.img,((1,1),(1,1),(0,0)),'constant')
else:
file = open(self.cfg["path"], "r")
matrix = []
for row in file:
matrix.append([int(x) for x in row.split()])
self.img = np.array(matrix)
self.imgPadded = np.pad(self.img, [(1, 1), (1, 1)], mode='constant')
def save(self,ans):
save_path = "outputs/result"
if self.cfg["type"] == "image":
plt.imshow(ans)
plt.savefig(save_path +".jpg")
else:
f = open(save_path, "w")
f.write(str(ans))
f.close()
def execute(self):
self.loadImage()
if self.spatial:
for i in self.spatial:
if self.spatial[i] is True:
try:
method = getattr(self.objS,i)
ans = method(self.imgPadded)
except AttributeError:
ans = None
if self.frequency:
for i in self.frequency:
if self.frequency[i] is True:
ans = self.objF.start(self.img,i)
if ans is None:
print("Incorrect Domain / Filter not found ")
else:
if self.cfg["save"]:
self.save(ans)
else:
display(self.cfg["type"], ans)