File size: 1,955 Bytes
7245fd8 a45bd87 f558ecf 3f7f1f6 55a1c7d a45bd87 55a1c7d a45bd87 55a1c7d a45bd87 3f7f1f6 f558ecf 7245fd8 44b81e8 f558ecf a924631 7245fd8 a924631 f558ecf 7245fd8 f558ecf 7245fd8 f558ecf 7245fd8 44b81e8 f558ecf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import gradio as gr
import numpy as np
import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
from tensorflow.keras.preprocessing import image
from huggingface_hub import from_pretrained_keras
import requests
# URL of the model file (adjust if needed)
model_url = "https://huggingface.co/diabolic6045/indian_cities_image_classification/resolve/main/model.h5"
model_path = "model.h5"
# Download the model if it doesn't exist
if not os.path.exists(model_path):
print("Downloading the model...")
response = requests.get(model_url)
with open(model_path, "wb") as f:
f.write(response.content)
print("Model downloaded.")
from tensorflow.keras.models import load_model
from tensorflow.keras.optimizers import Adam
print("loading model")
# Load the model, ignoring the optimizer argument
model = load_model(model_path, compile=False)
# Recompile the model with a valid optimizer
model.compile(optimizer=Adam(), loss="categorical_crossentropy")
# Define the class labels
class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai']
# Function to preprocess the image and predict the city
def classify_city(img):
# Preprocess the image
img = img.resize((175, 175))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img / 175.0 # Normalize the image
# Make predictions
predictions = model.predict(img)
predicted_class = np.argmax(predictions)
predicted_city = class_labels[predicted_class]
return f"Predicted City: {predicted_city}"
# Gradio Interface
iface = gr.Interface(
fn=classify_city,
inputs=gr.Image(type="pil", label="Upload an image of an Indian city"),
outputs=gr.Textbox(label="Predicted City"),
title="Indian Cities Image Classification",
description="Upload an image of a city in India, and the model will predict which city it is: Ahmedabad, Delhi, Kerala, Kolkata, or Mumbai.",
)
# Launch the Gradio app
iface.launch()
|