Spaces:
Build error
Build error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
-
from tensorflow.keras.models import load_model
|
| 5 |
-
|
| 6 |
-
# Define the class names
|
| 7 |
-
class_names = ['AnnualCrop', 'Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake']
|
| 8 |
-
|
| 9 |
-
# Function to load the selected model
|
| 10 |
-
def load_selected_model(model_name):
|
| 11 |
-
if model_name == "ResNet50_Model.keras":
|
| 12 |
-
return load_model('ResNet50_Model.keras')
|
| 13 |
-
elif model_name == "CNN_model.h5":
|
| 14 |
-
return load_model('CNN_model.h5')
|
| 15 |
-
else:
|
| 16 |
-
raise ValueError("Invalid model name")
|
| 17 |
-
|
| 18 |
-
# Function to predict the image
|
| 19 |
-
def predict_image(image, model_name):
|
| 20 |
-
model = load_selected_model(model_name)
|
| 21 |
-
img = cv2.resize(image, (64, 64))
|
| 22 |
-
img = img / 255.0
|
| 23 |
-
img = np.expand_dims(img, axis=0)
|
| 24 |
-
|
| 25 |
-
predictions = model.predict(img)[0]
|
| 26 |
-
|
| 27 |
-
predicted_class_index = np.argmax(predictions)
|
| 28 |
-
predicted_class_name = class_names[predicted_class_index]
|
| 29 |
-
|
| 30 |
-
confidence = predictions[predicted_class_index]
|
| 31 |
-
|
| 32 |
-
# Get the top 3 predictions
|
| 33 |
-
top_indices = predictions.argsort()[-3:][::-1]
|
| 34 |
-
top_classes = [class_names[i] for i in top_indices]
|
| 35 |
-
top_confidences = [predictions[i] for i in top_indices]
|
| 36 |
-
|
| 37 |
-
# Format the output
|
| 38 |
-
result = "Top Predictions:\n"
|
| 39 |
-
for i in range(3):
|
| 40 |
-
result += f"{top_classes[i]} (Confidence: {top_confidences[i]:.2f})\n"
|
| 41 |
-
|
| 42 |
-
return result
|
| 43 |
-
|
| 44 |
-
with gr.Blocks() as demo:
|
| 45 |
-
gr.Markdown("# Area Prediction Angle")
|
| 46 |
-
|
| 47 |
-
with gr.Row():
|
| 48 |
-
with gr.Column():
|
| 49 |
-
model_selector = gr.Dropdown(choices=["ResNet50_Model.keras", "CNN_model.h5"], label="Select Model")
|
| 50 |
-
image_input = gr.Image(type="numpy", label="Upload Image")
|
| 51 |
-
predict_button = gr.Button("Predict")
|
| 52 |
-
|
| 53 |
-
image_output = gr.Image(type="numpy", label="Uploaded Image")
|
| 54 |
-
|
| 55 |
-
prediction_output = gr.Textbox(label="Prediction")
|
| 56 |
-
|
| 57 |
-
predict_button.click(fn=predict_image, inputs=[image_input, model_selector], outputs=prediction_output)
|
| 58 |
-
image_input.change(fn=lambda x: x, inputs=image_input, outputs=image_output)
|
| 59 |
-
|
| 60 |
-
demo.launch()
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|