Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,36 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image, ImageOps
|
| 3 |
import numpy as np
|
| 4 |
-
import tensorflow as tf
|
| 5 |
import io
|
| 6 |
|
| 7 |
-
app =
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
model = tf.keras.models.load_model('my_model2.h5')
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
image = image.convert('RGB')
|
| 15 |
image = np.asarray(image)
|
| 16 |
-
image =
|
| 17 |
img_reshape = image[np.newaxis, ...]
|
| 18 |
-
prediction = model.predict(img_reshape)
|
| 19 |
-
return prediction
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return "Welcome to the Glaucoma Detector API!"
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
file = request.files['file']
|
| 30 |
-
if file.filename == '':
|
| 31 |
-
return jsonify({'error': 'No selected file'}), 400
|
| 32 |
-
if file and file.filename.lower().endswith('.jpg'):
|
| 33 |
-
image = Image.open(io.BytesIO(file.read()))
|
| 34 |
-
prediction = import_and_predict(image)
|
| 35 |
-
pred = prediction[0][0]
|
| 36 |
-
if pred > 0.5:
|
| 37 |
-
result = "Your eye is Healthy. Great!"
|
| 38 |
-
else:
|
| 39 |
-
result = "You are affected by Glaucoma. Please consult an ophthalmologist as soon as possible."
|
| 40 |
-
return jsonify({'prediction': result})
|
| 41 |
-
else:
|
| 42 |
-
return jsonify({'error': 'Invalid file format'}), 400
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import Optional
|
| 4 |
+
import tensorflow as tf
|
| 5 |
from PIL import Image, ImageOps
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
import io
|
| 8 |
|
| 9 |
+
app = FastAPI()
|
| 10 |
|
| 11 |
+
# Load your model
|
| 12 |
model = tf.keras.models.load_model('my_model2.h5')
|
| 13 |
|
| 14 |
+
class ImageData(BaseModel):
|
| 15 |
+
file: bytes
|
| 16 |
+
|
| 17 |
+
@app.post("/predict/")
|
| 18 |
+
async def predict(image_data: ImageData):
|
| 19 |
+
image = Image.open(io.BytesIO(image_data.file))
|
| 20 |
+
image = ImageOps.fit(image, (100, 100), Image.ANTIALIAS)
|
| 21 |
image = image.convert('RGB')
|
| 22 |
image = np.asarray(image)
|
| 23 |
+
image = image.astype(np.float32) / 255.0
|
| 24 |
img_reshape = image[np.newaxis, ...]
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
prediction = model.predict(img_reshape)
|
| 27 |
+
pred = prediction[0][0]
|
|
|
|
| 28 |
|
| 29 |
+
result = {
|
| 30 |
+
'prediction': 'Healthy' if pred > 0.5 else 'Affected by Glaucoma'
|
| 31 |
+
}
|
| 32 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def greet():
|
| 36 |
+
return {"Hello": "World!"}
|