Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,3 @@
|
|
1 |
-
# import gradio as gr
|
2 |
-
# import tensorflow as tf
|
3 |
-
# from tensorflow.keras.models import load_model
|
4 |
-
# from tensorflow.keras.preprocessing import image
|
5 |
-
# import numpy as np
|
6 |
-
# import requests
|
7 |
-
# import os
|
8 |
-
# import random
|
9 |
-
# from huggingface_hub import from_pretrained_keras
|
10 |
-
|
11 |
-
# model = from_pretrained_keras("diabolic6045/indian_cities_image_classification")
|
12 |
-
|
13 |
-
# # # Model URL and path
|
14 |
-
# # model_url = "https://huggingface.co/diabolic6045/indian_cities_image_classification/resolve/main/model.h5"
|
15 |
-
# # model_path = "my_model.h5"
|
16 |
-
|
17 |
-
# # # Download the model if it doesn't exist
|
18 |
-
# # if not os.path.exists(model_path):
|
19 |
-
# # print("Downloading the model...")
|
20 |
-
# # response = requests.get(model_url)
|
21 |
-
# # with open(model_path, "wb") as f:
|
22 |
-
# # f.write(response.content)
|
23 |
-
# # print("Model downloaded.")
|
24 |
-
|
25 |
-
# # # Load the model once at the start
|
26 |
-
# # try:
|
27 |
-
# # print("Loading the model...")
|
28 |
-
# # model = load_model(model_path)
|
29 |
-
# # print("Model loaded successfully.")
|
30 |
-
# # except Exception as e:
|
31 |
-
# # print(f"Failed to load model: {str(e)}")
|
32 |
-
# # model = None
|
33 |
-
|
34 |
-
# # Define class labels
|
35 |
-
# class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai']
|
36 |
-
# threshold = 0.5 # Minimum accuracy threshold
|
37 |
-
|
38 |
-
# # Function to process and predict the image
|
39 |
-
# def process_image(img):
|
40 |
-
# if model is None:
|
41 |
-
# return "Error: Model could not be loaded. Please check the logs for more information."
|
42 |
-
|
43 |
-
# # Preprocess the input image
|
44 |
-
# img = image.img_to_array(img)
|
45 |
-
# img = np.expand_dims(img, axis=0)
|
46 |
-
# img = img / 255.0 # Normalize
|
47 |
-
|
48 |
-
# try:
|
49 |
-
# # Make predictions
|
50 |
-
# predictions = model.predict(img)
|
51 |
-
# predicted_class_index = np.argmax(predictions)
|
52 |
-
# predicted_class_label = class_labels[predicted_class_index]
|
53 |
-
# accuracy = predictions[0][predicted_class_index]
|
54 |
-
|
55 |
-
# # Check if accuracy is below the threshold
|
56 |
-
# if all(acc < threshold for acc in predictions[0]):
|
57 |
-
# return "This location is not in our database."
|
58 |
-
# else:
|
59 |
-
# # Random color for text
|
60 |
-
# color = f"#{random.randint(0, 255):02x}{random.randint(0, 255):02x}{random.randint(0, 255):02x}"
|
61 |
-
# output = f"<span style='font-size: 24px; color: {color};'>Predicted city: <strong>{predicted_class_label}</strong></span>"
|
62 |
-
# acc = f"<span style='font-size: 24px; color: {color};'>Confidence: <strong>{accuracy*100:.02f}%</strong></span>"
|
63 |
-
# return output + "<br>" + acc
|
64 |
-
# except Exception as e:
|
65 |
-
# return f"Error during prediction: {str(e)}"
|
66 |
-
|
67 |
-
# # Define the Gradio interface
|
68 |
-
# iface = gr.Interface(
|
69 |
-
# fn=process_image,
|
70 |
-
# inputs=gr.Image(type="pil", label="Upload an image of an Indian city"),
|
71 |
-
# outputs=gr.HTML(label="Prediction"),
|
72 |
-
# title="Indian Cities Image Classification",
|
73 |
-
# description="Upload an image of a city in India, and the model will predict which of the five cities it is: Ahmedabad, Delhi, Kerala, Kolkata, or Mumbai.",
|
74 |
-
# theme=gr.themes.Soft()
|
75 |
-
# )
|
76 |
-
|
77 |
-
# # Launch the Gradio app
|
78 |
-
# iface.launch()
|
79 |
-
|
80 |
import gradio as gr
|
81 |
import numpy as np
|
82 |
import os
|
@@ -112,9 +33,10 @@ class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai']
|
|
112 |
# Function to preprocess the image and predict the city
|
113 |
def classify_city(img):
|
114 |
# Preprocess the image
|
|
|
115 |
img = image.img_to_array(img)
|
116 |
img = np.expand_dims(img, axis=0)
|
117 |
-
img = img /
|
118 |
|
119 |
# Make predictions
|
120 |
predictions = model.predict(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import os
|
|
|
33 |
# Function to preprocess the image and predict the city
|
34 |
def classify_city(img):
|
35 |
# Preprocess the image
|
36 |
+
img = img.resize((175, 175))
|
37 |
img = image.img_to_array(img)
|
38 |
img = np.expand_dims(img, axis=0)
|
39 |
+
img = img / 175.0 # Normalize the image
|
40 |
|
41 |
# Make predictions
|
42 |
predictions = model.predict(img)
|