-
Notifications
You must be signed in to change notification settings - Fork 3
/
pipeline_new.py
119 lines (84 loc) · 3.06 KB
/
pipeline_new.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
import cv2
import numpy as np
from Image_Preprocessing import Preprocess
from Classification import classifier
import os
import matplotlib.pyplot as plt
from HarrCascade import haar_cascade
from Evaluation import BoundingBox
from Evaluation import DetectionOutput
DIRECTORY = 'Data/test'
CASCADE = 'HarrCascade/data/cascade.xml'
def load_images_from_folder(folder):
images = {}
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename),0) # Reads the images from folder
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Converts images to gray scale
if img is not None:
images[os.path.join(folder,filename)] = img
return images
#while True:
images = load_images_from_folder(DIRECTORY)
labels = []
print('{:<30}- {:<15}'.format('File', 'Prediction'))
# Classify each image in directory and display file name and prediction
for file_name in images.keys():
proc = images[file_name]
proc = Preprocess.image_resize(proc, width = 250)
proc = Preprocess.process(proc)
# proc = Preprocess.crop(proc, 8, 128)
#
fig = plt.figure(figsize = [10,10])
fig.add_subplot(131)
plt.imshow(images[file_name], cmap='gray', interpolation='none')
plt.title("Before Preprocessing" )
# fig.add_subplot(132)
# plt.imshow(proc1,cmap='gray', interpolation = 'none')
fig.add_subplot(133)
plt.imshow(proc, cmap='gray', interpolation='none')
#
#
proc2 = proc.copy()
proc3 = proc.copy()
symbols, s2 = haar_cascade.haar_cascade(proc, CASCADE)
boundingBoxes = []
for (x,y,w,h) in symbols:
# cv2.rectangle(proc2,(x,y),(x+w,y+h),(255,255,0),2)
crop_img = proc[y:y+h, x:x+w]
crop_img = Preprocess.resizeAndPad(crop_img, (28, 28))
pred = classifier.predict_single_image(crop_img)
print('{:<30}{:<15}'.format(file_name, pred))
box = BoundingBox.BoundingBox(x,y, x+w, y+h, classifier.get_symbol(pred))
boundingBoxes.append(box)
cv2.imwrite("test_fives_haar.jpg", proc)
for (x,y,w,h) in symbols:
cv2.rectangle(proc2,(x,y),(x+w,y+h),(255,255,0),2)
for (x,y,w,h) in s2:
cv2.rectangle(proc3,(x,y),(x+w,y+h),(255,255,0),2)
fig.add_subplot(132)
plt.imshow(proc3, cmap='gray', interpolation='none')
fig.add_subplot(133)
plt.imshow(proc2, cmap='gray', interpolation='none')
detectionOutput = DetectionOutput.DetectionOutput(boundingBoxes)
string = detectionOutput.combineAll().display()
print(string)
ans = ""
try:
ans = string + " = " + str(eval(string))
except:
ans = string + " = ???"
print("***** FINAL ANSWER *****")
try:
print(string + " = " + str(eval(string)))
except:
print(string + " = ???")
print("***** DONE *****")
plt.title(ans)
plt.show()
cv2.imwrite("test_fives_haar.jpg", proc)
print('Done')
# pred = classifier.predict_single_image(proc)
# print('{:<30}{:<15}'.format(file_name, pred))
# path = file_name
# os.remove(os.path.abspath(path))
# labels.append(pred)