Pau22's picture
Upload 4 files
9331fda verified
Raw
History Blame Contribute Delete
2.42 kB
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()