Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,56 @@
|
|
1 |
-
import os
|
2 |
-
import pickle
|
3 |
-
import numpy as np
|
4 |
-
import tensorflow as tf
|
5 |
-
from flask import Flask, render_template, request
|
6 |
-
from PIL import Image
|
7 |
-
import keras
|
8 |
-
from keras.applications.vgg16 import preprocess_input
|
9 |
-
from tensorflow.keras.applications.vgg16 import preprocess_input
|
10 |
-
import gradio as gr
|
11 |
-
|
12 |
-
def model(img):
|
13 |
-
model_dir = 'cataract'
|
14 |
-
class_names = ['Normal', 'Cataract'] # Cataract class labels
|
15 |
-
channel='RGB'
|
16 |
-
selected_model = 'cataract'
|
17 |
-
|
18 |
-
architecture_path = os.path.join('models',model_dir, f'model_architecture_{selected_model}.pkl')
|
19 |
-
weights_path = os.path.join('models',model_dir, f'model_weights_{selected_model}.pkl')
|
20 |
-
|
21 |
-
with open(architecture_path, 'rb') as f:
|
22 |
-
loaded_model_architecture = pickle.load(f)
|
23 |
-
|
24 |
-
with open(weights_path, 'rb') as f:
|
25 |
-
loaded_model_weights = pickle.load(f)
|
26 |
-
|
27 |
-
# Create the model using the loaded architecture
|
28 |
-
loaded_model = tf.keras.models.model_from_json(loaded_model_architecture)
|
29 |
-
|
30 |
-
# Set the loaded weights to the model
|
31 |
-
loaded_model.set_weights(loaded_model_weights)
|
32 |
-
|
33 |
-
# Load and preprocess the image
|
34 |
-
try:
|
35 |
-
image = Image.open(img).convert(channel).resize((256, 256))
|
36 |
-
except:
|
37 |
-
print("ERROR")
|
38 |
-
return "ERROR"
|
39 |
-
x = np.array(image)
|
40 |
-
x = np.expand_dims(x, axis=0)
|
41 |
-
x = preprocess_input(x)
|
42 |
-
|
43 |
-
# Make predictions
|
44 |
-
predictions = loaded_model.predict(x)
|
45 |
-
print(predictions)
|
46 |
-
|
47 |
-
predicted_class_index = 1 if (predictions[0]>0.5) else 0
|
48 |
-
print(predicted_class_index)
|
49 |
-
|
50 |
-
predicted_class_label = class_names[predicted_class_index]
|
51 |
-
|
52 |
-
return predicted_class_label
|
53 |
-
|
54 |
-
cataract_app = gr.Interface(model,gr.Image())
|
55 |
-
|
56 |
cataract_app.launch(share=True)
|
|
|
1 |
+
import os
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
from flask import Flask, render_template, request
|
6 |
+
from PIL import Image
|
7 |
+
import keras
|
8 |
+
from keras.applications.vgg16 import preprocess_input
|
9 |
+
from tensorflow.keras.applications.vgg16 import preprocess_input
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
def model(img):
|
13 |
+
model_dir = 'cataract'
|
14 |
+
class_names = ['Normal', 'Cataract'] # Cataract class labels
|
15 |
+
channel='RGB'
|
16 |
+
selected_model = 'cataract'
|
17 |
+
|
18 |
+
architecture_path = os.path.join('models',model_dir, f'model_architecture_{selected_model}.pkl')
|
19 |
+
weights_path = os.path.join('models',model_dir, f'model_weights_{selected_model}.pkl')
|
20 |
+
|
21 |
+
with open(architecture_path, 'rb') as f:
|
22 |
+
loaded_model_architecture = pickle.load(f)
|
23 |
+
|
24 |
+
with open(weights_path, 'rb') as f:
|
25 |
+
loaded_model_weights = pickle.load(f)
|
26 |
+
|
27 |
+
# Create the model using the loaded architecture
|
28 |
+
loaded_model = tf.keras.models.model_from_json(loaded_model_architecture)
|
29 |
+
|
30 |
+
# Set the loaded weights to the model
|
31 |
+
loaded_model.set_weights(loaded_model_weights)
|
32 |
+
|
33 |
+
# Load and preprocess the image
|
34 |
+
try:
|
35 |
+
image = Image.open(img).convert(channel).resize((256, 256))
|
36 |
+
except:
|
37 |
+
print("ERROR")
|
38 |
+
return "ERROR"
|
39 |
+
x = np.array(image)
|
40 |
+
x = np.expand_dims(x, axis=0)
|
41 |
+
x = preprocess_input(x)
|
42 |
+
|
43 |
+
# Make predictions
|
44 |
+
predictions = loaded_model.predict(x)
|
45 |
+
print(predictions)
|
46 |
+
|
47 |
+
predicted_class_index = 1 if (predictions[0]>0.5) else 0
|
48 |
+
print(predicted_class_index)
|
49 |
+
|
50 |
+
predicted_class_label = class_names[predicted_class_index]
|
51 |
+
|
52 |
+
return predicted_class_label
|
53 |
+
|
54 |
+
cataract_app = gr.Interface(model,gr.Image(),["text"])
|
55 |
+
|
56 |
cataract_app.launch(share=True)
|