LazyBoss commited on
Commit
9e36bec
·
verified ·
1 Parent(s): 9de6422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -32
app.py CHANGED
@@ -1,45 +1,36 @@
1
- from flask import Flask, request, jsonify, send_file
 
 
 
2
  from PIL import Image, ImageOps
3
  import numpy as np
4
- import tensorflow as tf
5
  import io
6
 
7
- app = Flask(__name__)
8
 
9
- # Load the TensorFlow model
10
  model = tf.keras.models.load_model('my_model2.h5')
11
 
12
- def import_and_predict(image_data):
13
- image = ImageOps.fit(image_data, (100, 100), Image.ANTIALIAS)
 
 
 
 
 
14
  image = image.convert('RGB')
15
  image = np.asarray(image)
16
- image = (image.astype(np.float32) / 255.0)
17
  img_reshape = image[np.newaxis, ...]
18
- prediction = model.predict(img_reshape)
19
- return prediction
20
 
21
- @app.route('/')
22
- def index():
23
- return "Welcome to the Glaucoma Detector API!"
24
 
25
- @app.route('/predict', methods=['POST'])
26
- def predict():
27
- if 'file' not in request.files:
28
- return jsonify({'error': 'No file part'}), 400
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
- if __name__ == '__main__':
45
- app.run(port=5000, debug=True) # You can specify any port you prefer
 
 
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!"}