I am trying to run a simple machine learning code and calling it using flask but it giving the error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application. and the console log says `
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/rishav/Desktop/test/app.py", line 47, in upload_file
noise = speckle_noise(image)
File "/Users/rishav/Desktop/test/app.py", line 22, in speckle_noise
row, col = image.shape
ValueError: too many values to unpack (expected 2)
`.
My Code is :
import os
import sys # ONLY for Loggind in console
import time
from flask import Flask, request
import pickle
import pandas as pd
import base64
import cv2
import numpy as np
from io import BytesIO
UPLOAD_FOLDER = './upload'
app = Flask(__name__, template_folder='templates')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
base64_string = ''
def speckle_noise(image):
row, col = image.shape
gauss = np.random.randn(row, col)
gauss = gauss.reshape(row, col)
noisy = image + image * gauss
return noisy
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file1' not in request.files:
return 'there is no file1 in form!'
file1 = request.files['file1']
path = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
file1.save(path)
with open(path, "rb") as img_file:
base64_string = base64.b64encode(img_file.read())
print(base64_string, file=sys.stdout)
image_bytes = base64.b64decode(base64_string)
image_array = np.frombuffer(image_bytes, dtype=np.uint8)
image = cv2.imdecode(image_array, flags=cv2.IMREAD_COLOR)
noise = speckle_noise(image)
_, image_arr = cv2.imencode('.jpg', noise)
image_bytes = image_arr.tobytes()
base64_return = base64.b64encode(image_bytes)
return base64_return
return 'ok'
return '''
<h1>Upload new File</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<input type="submit">
</form>
'''
if __name__ == '__main__':
app.run()
Any help would be highly appreciated!