-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge.py
209 lines (108 loc) · 3.81 KB
/
edge.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
import random
DATADIR = "Products”
CATEGORIES = ["colgate"]
training data = []
IMG_SIZE = 50
def create_training_data():
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
class_num = CATEGORIES.index (category)
for img in tqdm(os.listdir(path)):
try:
img_array = cv2.imread(os.path.join(path, img), cv2. IMREAD_GRAYSCALE)
new_array = cv2.resize (img_array, (IMG_SIZE, IMG_SIZE))
traning_data. append([new_array, class_num])
except Exception as e:
pass
create_training_data()
random.shuffle(training_data)
X=[]
y=[]
for features, label in training_data:
X.append(features)
y.append (label)
X= np.array(X).reshape(-1,IMG_SIZE, IMG_SIZE, 1)
import pickle
pickle_out = open("X.pickle", "wb")
pickle.dump(X, pickle_out)
pickle_cut.close()
from Flask import Flask, request, jsonify, render_template, send_file
import json
import base64
ihportos
from flask_cors import CORS
from predictor import predict
app = Flask (_name_, template_folder='templates')
CORS(app, resources={r" /api/*": {"origins": "*}})
@app. route('/')
def index () :
return render_template('index .html')
@app.route ('/assets /<path:path>')
def serve_static (path):
return send_file ("assets /"+path)
@app.route (' /api /image_upload',methods=["POST"])
def ImageUpload():
if "image" in (request.files):
file=request.files["image"]
file.save("image.jpeg")
resp=predict()
result = {
'error':False,
'product': resp["category"],
"prediction": str(resp["prediction"])
}
return json.dumps(result)
if _name_ == "_main_":
app.run(host="localhost",port=5000,debug=False)
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keraspreprocessing.image import ImageDateGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
import pickle
NAME = "Prod-CNN"
pickle_in = open("X.pickle", "rb")
X= pickle.load(pickle_in)
pickie_in = open("y.pickle", "rb" )
y = pickle.load(pickle_in)
X = X/255.0
model = Sequential()
model.add(Conv2D(256,(6,3),input_shape=X.shape[1:]))
model.add (Activation( 'relu'))
model.add (MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256,(6,3)))
model.add (Activation( 'relu'))
model.add (MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(dense(1))
model.add(Activation( 'sigmoid'))
tensorboard = Tensorboard(log_dir="logs/{}".format(NAME))
model.compile (loss='binary crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X, y, batch_size=24, epochs=10, validation_split=0.3)
model.save ('product-classifier.model')
import tensorflow as tf
import cv2
from keras.models import load_model
import numpy as np
CATEGORIES =["colgate"]
def prepare(fileprt):
IMG_SIZE = 50
img_array = cv2.imread(filepath)
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,1)
def predict():
model = tf.keras.models.load_model("product-classifier.model")
prediction = model.predict([prepare(r'image.jpeg')])
return{
"prediction":rediction,
"category":CATEGORIES[int(prediction[0][0])]
}