Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| from tensorflow.keras.applications import MobileNetV2 | |
| from tensorflow.keras.layers import Dense, BatchNormalization, Dropout | |
| from tensorflow.keras.models import Sequential | |
| # ===================== | |
| # MODEL ARCHITECTURE | |
| # ===================== | |
| base_model = MobileNetV2( | |
| weights=None, | |
| include_top=False, | |
| input_shape=(224, 224, 3), | |
| pooling="avg" | |
| ) | |
| model = Sequential([ | |
| base_model, | |
| BatchNormalization(), | |
| Dropout(0.5), | |
| Dense(256, activation="relu"), | |
| Dropout(0.3), | |
| Dense(7, activation="softmax") | |
| ]) | |
| # Load trained weights | |
| model.load_weights("model.weights.h5") | |
| # ===================== | |
| # CLASS NAMES | |
| # ===================== | |
| class_names = [ | |
| "broken_benches", | |
| "fallen_trees", | |
| "garbage", | |
| "leaky_pipes", | |
| "open_manhole", | |
| "potholes", | |
| "streetlight" | |
| ] | |
| # ===================== | |
| # PREDICTION FUNCTION | |
| # ===================== | |
| def predict_image(image): | |
| if image is None: | |
| return "No image uploaded", "0%" | |
| image = image.convert("RGB") | |
| image = image.resize((224, 224)) | |
| img = np.array(image, dtype=np.float32) / 255.0 | |
| img = np.expand_dims(img, axis=0) | |
| prediction = model.predict(img, verbose=0) | |
| predicted_class = class_names[np.argmax(prediction)] | |
| confidence = float(np.max(prediction)) | |
| return predicted_class, f"{confidence:.2%}" | |
| # ===================== | |
| # UI | |
| # ===================== | |
| description = """ | |
| # ποΈ Community Issue Classification System | |
| This AI-powered system automatically identifies common civic infrastructure issues from images. | |
| ### Detectable Categories | |
| - πͺ Broken Benches | |
| - π³ Fallen Trees | |
| - ποΈ Garbage | |
| - π° Leaky Pipes | |
| - β οΈ Open Manholes | |
| - π³οΈ Potholes | |
| - π‘ Streetlight Issues | |
| ### Model Information | |
| - Model: MobileNetV2 Fine-Tuned Classifier | |
| - Classes: 7 | |
| - Input Size: 224 Γ 224 RGB Images | |
| - Developer: Pauras More | |
| Upload an image below to classify a civic issue. | |
| """ | |
| demo = gr.Interface( | |
| fn=predict_image, | |
| inputs=gr.Image(type="pil", label="Upload Image"), | |
| outputs=[ | |
| gr.Textbox(label="Predicted Class"), | |
| gr.Textbox(label="Confidence") | |
| ], | |
| title="Community Issue Classifier", | |
| description=description, | |
| flagging_mode="never" | |
| ) | |
| demo.launch() |