Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
model = tf.keras.models.load_model('densenet.h5')
|
| 2 |
+
model.load_weights("pretrained_model.h5")
|
| 3 |
+
class_names = ['Cardiomegaly', 'Emphysema', 'Effusion', 'Hernia', 'Infiltration', 'Mass', 'Nodule', 'Atelectasis', 'Pneumothorax', 'Pleural_Thickening', 'Pneumonia', 'Fibrosis', 'Edema', 'Consolidation']
|
| 4 |
+
def custom_decode_predictions(predictions, class_labels):
|
| 5 |
+
|
| 6 |
+
decoded_predictions = []
|
| 7 |
+
for pred in predictions:
|
| 8 |
+
# Get indices of top predicted classes
|
| 9 |
+
top_indices = pred.argsort()[-3:][::-1] # Change 5 to the number of top classes you want to retrieve
|
| 10 |
+
# Decode each top predicted class
|
| 11 |
+
decoded_pred = [(class_labels[i], pred[i]) for i in top_indices]
|
| 12 |
+
decoded_predictions.append(decoded_pred)
|
| 13 |
+
return decoded_predictions
|
| 14 |
+
|
| 15 |
+
def classify_image(img):
|
| 16 |
+
img_array = image.img_to_array(img)
|
| 17 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 18 |
+
img_array = preprocess_input(img_array)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
predictions1 = model.predict(img_array)
|
| 22 |
+
decoded_predictions = custom_decode_predictions(predictions1, class_names)
|
| 23 |
+
return decoded_predictions
|
| 24 |
+
|
| 25 |
+
# Gradio interface
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=classify_image,
|
| 28 |
+
inputs="image",
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="Image Classification",
|
| 31 |
+
description="Classify images using your pre-trained model."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the interface
|
| 35 |
+
iface.launch(inline = False)
|