File size: 999 Bytes
1467f2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, render_template, jsonify
from flask_cors import CORS
import keras
import numpy as np
from keras.preprocessing import image
import io

app = Flask(__name__)
CORS(app)

model = keras.models.load_model('Cats_vs_Dogs.model')

@app.route('/')
def index():
    return render_template('index.html', prediction=None)

@app.route('/predict', methods=['POST'])
def predict():
    imagefile = request.files['imagefile']

    # Read the image file into memory
    img_stream = imagefile.read()

    # Convert the image to grayscale and resize
    img = image.load_img(io.BytesIO(img_stream), color_mode='grayscale', target_size=(60, 60))
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array /= 255.0

    prediction = model.predict(img_array)
    predicted_class = "Dog" if prediction[0][1] > prediction[0][0] else "Cat"

    return jsonify({'prediction': predicted_class})

if __name__ == '__main__':
    app.run(debug=True)