Upload 3 files
Browse files- app.py +81 -0
- model.pt +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load the YOLO model - YOLOv11m for pothole, road damage, and garbage detection
|
| 7 |
+
try:
|
| 8 |
+
model = YOLO("model.pt")
|
| 9 |
+
except Exception as e:
|
| 10 |
+
print(f"Error loading model: {e}")
|
| 11 |
+
model = None
|
| 12 |
+
|
| 13 |
+
def predict(image, conf_threshold):
|
| 14 |
+
try:
|
| 15 |
+
if image is None or model is None:
|
| 16 |
+
return None, "Model not loaded or invalid image."
|
| 17 |
+
|
| 18 |
+
# Run inference
|
| 19 |
+
results = model(image, imgsz=768, conf=conf_threshold)
|
| 20 |
+
result = results[0]
|
| 21 |
+
|
| 22 |
+
# Plotting the detections on the image returns a BGR numpy array
|
| 23 |
+
annotated_image = result.plot()
|
| 24 |
+
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 25 |
+
|
| 26 |
+
# Detection overview text
|
| 27 |
+
boxes = result.boxes
|
| 28 |
+
class_names = result.names
|
| 29 |
+
|
| 30 |
+
if len(boxes) == 0:
|
| 31 |
+
detection_summary = "No civic issues detected in this image."
|
| 32 |
+
else:
|
| 33 |
+
# Count detections safely
|
| 34 |
+
detection_counts = {}
|
| 35 |
+
for box in boxes:
|
| 36 |
+
# box.cls is usually a tensor. Safe conversion to integer:
|
| 37 |
+
cls_id = int(box.cls.item() if hasattr(box.cls, "item") else box.cls[0])
|
| 38 |
+
cls_name = class_names.get(cls_id, f"Class {cls_id}")
|
| 39 |
+
detection_counts[cls_name] = detection_counts.get(cls_name, 0) + 1
|
| 40 |
+
|
| 41 |
+
summary_lines = ["**Detections:**"]
|
| 42 |
+
for cls_name, count in detection_counts.items():
|
| 43 |
+
summary_lines.append(f"- {count} {cls_name}(s)")
|
| 44 |
+
|
| 45 |
+
detection_summary = "\n".join(summary_lines)
|
| 46 |
+
|
| 47 |
+
return Image.fromarray(annotated_image_rgb), detection_summary
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
import traceback
|
| 51 |
+
error_msg = f"ERROR during prediction: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 52 |
+
return None, error_msg
|
| 53 |
+
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
with gr.Blocks(title="PotholeNet-YOLO11m-v1 π") as interface:
|
| 56 |
+
gr.Markdown("# π PotholeNet-YOLO11m-v1")
|
| 57 |
+
gr.Markdown("**Aamchi City AI Civic System** β Real-time pothole, road damage, and garbage detection for Indian urban roads.")
|
| 58 |
+
gr.Markdown("Upload an image of a road to detect infrastructure issues. The model was trained on 23,000+ street-level images.")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
input_image = gr.Image(type="pil", label="Upload Street Image")
|
| 63 |
+
conf_slider = gr.Slider(minimum=0.01, maximum=1.0, value=0.25, step=0.01, label="Confidence Threshold")
|
| 64 |
+
submit_btn = gr.Button("Detect Civic Issues", variant="primary")
|
| 65 |
+
|
| 66 |
+
with gr.Column():
|
| 67 |
+
output_image = gr.Image(type="pil", label="Detection Results")
|
| 68 |
+
detection_text = gr.Textbox(label="Detection Summary", interactive=False, lines=4)
|
| 69 |
+
|
| 70 |
+
submit_btn.click(
|
| 71 |
+
fn=predict,
|
| 72 |
+
inputs=[input_image, conf_slider],
|
| 73 |
+
outputs=[output_image, detection_text]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
gr.Markdown("### Intended Use")
|
| 77 |
+
gr.Markdown("Real-time pothole detection, Automated civic issue reporting, Infrastructure health monitoring.")
|
| 78 |
+
gr.Markdown("**Developer:** Vansh Momaya")
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|
model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f380cd373f61f2bc71f7fcc1b0ec072194dc2cd933fd05bc1ae5ad136a333b78
|
| 3 |
+
size 40540780
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
gradio==4.44.1
|
| 3 |
+
pillow
|
| 4 |
+
opencv-python-headless
|