Mary
Mary

Reputation: 91

My flask app is not reading the input images

I have created an Image Classifier Flask App but it doesn't read my input images(none of them). I have tried every solution I found on internet but couldn't solve my problem.

Below is Flask API I'm using:

@app.route('/upload',methods=['GET','POST'])
def upload_analyze():

    if request.method == 'POST':
        # check if a file was passed into the POST request
        if 'image' not in request.files:
            flash('No file was uploaded.')
            return redirect(request.url)


        image_file = request.files['image']
        #print(image_file)
        image_file = str(image_file)
        image = cv2.imread(image_file)

        clt = KMeans(n_clusters = 3)
        dataset = pd.read_csv('bb22.csv') 
        X = dataset.iloc[:, 1: 8].values
        sc = StandardScaler()
        global orig , r
            # load the image, convert it to grayscale, and blur it slightly
        #images = np.array(Image.open(image_file))
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (7, 7), 0)
        ................

It takes an input image of grain and calculates its length,width and tells whether its whole(1)/broken(0)

Upvotes: 0

Views: 802

Answers (1)

Emrah Diril
Emrah Diril

Reputation: 1755

I found this flask file uploading example code online.

I think the problem in your code is the return value from request.files['image'] is an object that has methods like save() and filename(). cv2.imread() needs a file as an input so str(image_file) doesn't work.

Check out the example in the link above to see how the return value is used to save the file:

f = request.files['file']
filename = secure_filename(f.filename)
f.save(filename)
image = cv2.imread(filename)

Upvotes: 1

Related Questions