-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
164 lines (136 loc) · 4.95 KB
/
app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import click
import os
import donwloaddata as dl
import cv2 as cv
import imghdr
import numpy as np
from PIL import Image, ImageDraw
from models import model1
from models import model3
from utils import LABELS
modelos = {"model1": model1, "model2": 0, "model3": model3}
LABELS = LABELS()
# download command
##################
@click.group()
def data():
pass
@data.command()
@click.option('-p', required=True, type=click.Choice(['descarga']), help='Descarga los datos necesarios para los modelos')
def download(p):
"""Format: download -p descarga """
if p == p:
dl.activate()
else:
click.echo('parametro no valido')
# train command
###############
@click.group()
def training():
pass
@training.command()
@click.option('-m', required=True, type=click.Choice(['model1', 'model2', 'model3']))
@click.option('-d', default=os.path.join('images', 'train'), type=click.STRING, help='Directory with training data')
def train( m, d ):
"""Format: train -m modelo -d path/to/folder """
lenet = modelos[m] == model3
clase = [c for c in os.listdir(d) if os.path.isdir(os.path.join(d, c))]
xdata = []
ydata = []
for ID in clase:
class_folder = os.path.join(d, ID)
for file in os.listdir(class_folder):
if os.path.basename(os.path.join(class_folder, file)): # images only
imagePath = os.path.join(os.path.abspath('.'), d, ID, file)
image = cv.imread(imagePath, 0) # se carga la imagen en escala de grises
size = (32, 32) if lenet else (28, 28)
resized = cv.resize(image, size) # cv2 drops channel dimension
xdata.append(resized[..., None] if lenet else resized.flatten())
ydata.append(int(ID))
print(len(ydata))
if m == 'model1':
model = model1.LogRskLearn()
model.train(xdata, ydata)
elif m == 'model2':
pass
else:
model = model3.LeNet5()
model.train(xdata, ydata)
# test command
##############
@click.group()
def testing():
pass
@testing.command()
@click.option('-m', required=True, type=click.Choice(['model1', 'model2', 'model3']))
@click.option('-d', required=True)
def test( m, d ):
"""Format: test -m modelo -d path/to/folder """
lenet = modelos[m] == model3
clase = [c for c in os.listdir(d) if os.path.isdir(os.path.join(d, c))]
xdata = []
ydata = []
for ID in clase:
class_folder = os.path.join(d, ID)
for file in os.listdir(class_folder):
if os.path.basename(os.path.join(class_folder, file)): # images only
imagePath = os.path.join(os.path.abspath('.'), d, ID, file)
image = cv.imread(imagePath, 0) # # se carga la imagen en escala de grises
size = (32, 32) if lenet else (28, 28)
resized = cv.resize(image, size) # cv2 drops channel dimension
xdata.append(resized[..., None] if lenet else resized.flatten())
ydata.append(int(ID))
if m == 'model1':
model = model1.LogRskLearn()
model.accuracy(xdata, ydata)
elif m == 'model2':
pass
else:
model = model3.LeNet5()
model.accuracy(xdata, ydata)
# infer command
##############
@click.group()
def infering():
pass
@infering.command()
@click.option('-m', required=True, type=click.Choice(['model1', 'model2', 'model3']))
@click.option('-d', required=True, help='Directorio con las imagenes de entramiento')
def infer( m, d ):
"""Format: infer -m modelo -d path/to/folder """
global predictions
lenet = modelos[m] == model3
xdata = []
files = []
data_dir = os.path.join(d)
print("Nuevas imagenes de : ", data_dir)
for file in os.listdir(d):
imagePath = os.path.join(os.path.abspath('.'), data_dir, file)
if os.path.basename(imagePath) and imghdr.what(imagePath):
image = cv.imread(imagePath, 0) # # se carga la imagen en escala de grises
size = (32, 32) if lenet else (28, 28)
resized = cv.resize(image, size)
xdata.append(resized[..., None] if lenet else resized.flatten())
files.append(imagePath)
xdata = np.array(xdata)
if m == 'model1':
model = model1.LogRskLearn()
predictions = model.predict(xdata)
elif m == 'model2':
pass
else:
model = model3.LeNet5()
predictions = model.predict(xdata)
click.echo("Predicciones :")
for i in range(len(predictions)):
class_label = LABELS[str(predictions[i])]
click.echo('%s' %class_label)
img = Image.open(files[i])# se abre la imagen del archivo i
img = img.resize((200, 200))# se reajusta el tamano
draw = ImageDraw.Draw(img)
draw.line((0, 0) + (200, 0), fill=255, width=20)
draw.text((0, 0), class_label, fill=(0, 0, 0))
img.show()
start = click.CommandCollection(sources=[data, training, testing, infering])
if __name__ == '__main__':
start()