def upload_file(): if flask.request.method == 'GET': url = flask.request.args.get("url") img = load_image_url(url) tensor = transform_image(img) length_digit, output_number = get_prediction(tensor) else: img_bytes = flask.request.files['file'].read() tensor = transform_image(img_bytes) length_digit, output_number = get_prediction(tensor) # print(length_digit, output_number) data = {'prediction': output_number, 'Length': str(length_digit)} return flask.jsonify(data)
def testpredict(): if request.method == "POST": file = request.files.get('file') if file is None or file.filename == "": return jsonify({"error": "no file"}) if not allowed_file(file.filename): return jsonify({"error": "format not supported"}) img_bytes = file.read() tensor = transform_image(img_bytes) prediction = get_predictions(tensor) data = { 'prediction': prediction.item(), 'class_name': str(prediction.item()) } return jsonify(data) # try: # img_bytes = file.read() # tensor = transform_image(img_bytes) # prediction = get_predictions(tensor) # data = {'prediction': prediction.item(), 'class_name' :str(prediction.item())} # return jsonify(data) # except: # return jsonify({"error":"error during prediction"}) return jsonify({'result': 1})
def fileUpload(): target = os.path.join(UPLOAD_FOLDER, 'test_docs') if not os.path.isdir(target): os.mkdir(target) logger.info("welcome to upload`") file = request.files['file'] filename = secure_filename(file.filename) destination = "/".join([target, filename]) file.save(destination) #session['uploadFilePath']=destination img = Image.open('./uploads/test_docs/img1.jpeg') tensor = transform_image(img) prediction = get_prediction(tensor) predicted_idx = str(prediction.item()) print(predicted_idx) data = { 'prediction': predicted_idx, 'class_name': class_index_name[predicted_idx] } response = make_response(jsonify(data)) # Add Access-Control-Allow-Origin header to allow cross-site request response.headers[ 'Access-Control-Allow-Origin'] = 'http://ec2-18-217-62-181.us-east-2.compute.amazonaws.com:3000' return response
def predict(): if request.method == "POST": file = request.files.get('file') if file is None or file.filename == "": return jsonify({'error': 'no file'}) if not allowed_file(file.filename): return jsonify({'error': 'format not supported'}) try: img_bytes = file.read() tensor = transform_image(img_bytes) prediction = get_prediction(tensor) data = { 'prediction': prediction.item(), 'class_name': str(prediction.item()) } return jsonify(data) except: return jsonify({'error': 'error during prediction'}) # 1. load image # 2. image -> tensor # 3. prediction # 4. return json return jsonify({'result': 1})
def predict(): if request.method == 'POST': file = request.files.get('file') if file is None or file.filename == "": return jsonify({'error': 'no hay archivo'}) if not allowed_file(file.filename): return jsonify({'error': 'formato no soportado'}) try: img_bytes = file.read() tensor = transform_image(img_bytes) prediction = get_prediction(tensor) data = {'prediction': prediction.item(), 'class_name': str(prediction.item())} return jsonify(data) except: return jsonify({'error': 'error durante la predicción'})
def upload_pred(): if request.method == "POST": image_file = request.values['canvas'] image = base64.b64decode(image_file) im_file = io.BytesIO(image) # convert image to file-like object img = PIL.Image.open(im_file) filename = str(uuid.uuid4()) img.save(UPLOAD_FOLDER + filename + ".png", "PNG") file = open(UPLOAD_FOLDER + filename + '.png', 'rb') #file = open("eight.png", 'rb') img_bytes = file.read() tensor = transform_image(img_bytes) pred = get_prediction(tensor) return render_template('index.html', prediction=str(pred.item())) return render_template('index.html')
def predict(): if request.method == 'POST': file = request.files.get('image') if file is None or file.filename == "": return jsonify({'error': 'no file'}) if not allowed_file(file.filename): return jsonify({'error': 'format not supported'}) try: img_bytes = file.read() image = io.BytesIO(img_bytes) image = Image.open(image) tensor = transform_image(image) prediction = get_prediction(tensor) return render_template('rezultat.html', rez=prediction) except: return jsonify({'error': 'error during prediction'})
def predict(): if request.method == 'POST': print(request.files) file = request.files.read() if file is None or file.filename == "": return jsonify({'error': 'no file'}) if not allowed_file(file.filename): return jsonify({'error': 'format not supported'}) # try: # print(file.filename) print("reachd") img_bytes = file.read() tensor = transform_image(img_bytes) length_digit, output_number = get_prediction(tensor) print(length_digit, output_number) data = {'prediction': output_number, 'Length': str(length_digit)} return flask.render_template("index.html", data=data)
def upload_files(): uploaded_file = request.files['file'] filename = uploaded_file.filename if filename != '': uploaded_file.save(os.path.join(app.config['UPLOAD_PATH'], filename)) #img_bytes = uploaded_file.read() img = Image.open('./uploads/img1.jpeg') tensor = transform_image(img) prediction = get_prediction(tensor) predicted_idx = str(prediction.item()) print(predicted_idx) data = { 'prediction': predicted_idx, 'class_name': class_index_name[predicted_idx] } return render_template('index.html', file=uploaded_file.filename, category=class_index_name[predicted_idx])
def predict(): if request.method == 'POST': if 'file' not in request.files: return redirect(request.url) file = request.files.get('file') if file is None or file.filename == "": error = 'No file uploaded' return render_template('index.html', messages=error) if not allowed_file(file.filename): error = 'Format Not Supported' return render_template('index.html', messages=error) try: img_bytes = file.read() tensor = transform_image(img_bytes) labels = Predict(tensor) return render_template('result.html', labels=labels) except: error = 'Prediction Error' return render_template('index.html', messages=error) return render_template('index.html')
def predict(image): image = Image.fromarray((image[:, :, 0]).astype(np.uint8)) image = image.resize((28, 28)) tensor = transform_image(image) prediction = get_prediction(tensor) return prediction