def upload_file(): if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename): filename = file.filename file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) cnn.test() return redirect(url_for('uploaded_file', filename=filename)) return render_template("index.html")
def do_POST(self): response_code = 200 response = "" var_len = int(self.headers.get('Content-Length')) content = self.rfile.read(var_len) payload = json.loads(content) print(payload) if 'train' in self.path: print('train') start_training() elif 'test' in self.path: print('test') response = test(payload['image'], payload['label']) else: response_code = 400 b = io.BytesIO(json.dumps(response).encode('utf-8')) view = b.getbuffer() self.send_response(response_code) self.send_header('Content-Type', 'application/json') self.send_header('Access-Control-Allow-Origin', '*') self.end_headers() self.wfile.write(view)
def result(): if request.method == 'POST': f = request.files['file'] f.save(secure_filename(f.filename)) shutil.move( f.filename, "os.getcwd/static" + f.filename ) # as the file we save will be in directory we need to move to static #make sure that cnn.py is in the same directory created import cnn result = cnn.test(f.filename) print(result) return render_template('result.html', value=result, filename=str("../static/" + f.filename))
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # Train Module # Module to train a CNN model on character data. import numpy as np import cnn import utility input_arguments = utility.parse_input_arguments(module="train") images, labels = utility.load_data(input_arguments.image_path) class_count = len(np.unique(labels)) images, labels = utility.preprocess(images, labels) images, labels = utility.shuffle(images, labels) x_train, x_test, y_train, y_test = utility.split(images, labels, test_size=0.2) cnn = cnn.CNN(x_train.shape[1], x_train.shape[2], x_train.shape[3], class_count) cnn.summary() cnn.train(x_train, y_train, epochs=input_arguments.epochs, batch_size=input_arguments.batch_size, validation_split=input_arguments.validation_split, output_path=input_arguments.output_path) cnn.test(x_test, y_test, output_path=input_arguments.output_path)
from keras.datasets import mnist from conv import Conv2d from maxpool import MaxPooling2d from softmax import Softmax from cnn import training_loop, test (train_images, train_labels), (test_images, test_labels) = mnist.load_data() train_images = train_images[:1000] train_labels = train_labels[:1000] test_images = test_images[:1000] test_labels = test_labels[:1000] conv2d = Conv2d(8, 3) maxpool2d = MaxPooling2d(2) softmax = Softmax(13*13*8, 10) training_loop(3, train_images, train_labels, conv2d, maxpool2d, softmax) test(test_images, test_labels, conv2d, maxpool2d, softmax)