-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
43 lines (36 loc) · 1.25 KB
/
app.py
File metadata and controls
43 lines (36 loc) · 1.25 KB
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
38
39
40
41
42
43
from flask_cors import CORS
from flask import Flask, request, jsonify
from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image
import io
app = Flask(__name__)
CORS(app)
# Load the pre-trained model
model = load_model('models/model.h5')
# Define the image size
width = 150
height = 150
# Define a function to preprocess the image
def preprocess_image(image):
# Resize the image to the required input shape of your model
resized_image = image.resize((width, height))
# Convert image to numpy array
img_array = np.asarray(resized_image)
# Normalize pixel values
img_array = img_array / 255.0
# Expand dimensions to match the shape expected by the model
img_array = np.expand_dims(img_array, axis=0)
return img_array
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
image = Image.open(io.BytesIO(file.read()))
processed_image = preprocess_image(image)
prediction = model.predict(processed_image)
# Example: Assuming the model returns class probabilities
return jsonify({'predictions': prediction.tolist()})
if __name__ == '__main__':
app.run(debug=True)