Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +51 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from ultralyticsplus import YOLO
|
| 5 |
+
|
| 6 |
+
# Load YOLO model
|
| 7 |
+
model = YOLO("keremberke/yolov8n-pothole-segmentation")
|
| 8 |
+
model.overrides['conf'] = 0.25
|
| 9 |
+
|
| 10 |
+
# Constants
|
| 11 |
+
SCALE = 0.005 # m² per pixel²
|
| 12 |
+
COST_PER_M2 = 1000 # ₹ per m²
|
| 13 |
+
|
| 14 |
+
def estimate_cost_from_image(image):
|
| 15 |
+
img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 16 |
+
h, w = img_bgr.shape[:2]
|
| 17 |
+
|
| 18 |
+
# Save temporary image
|
| 19 |
+
temp_path = "temp.jpg"
|
| 20 |
+
cv2.imwrite(temp_path, img_bgr)
|
| 21 |
+
|
| 22 |
+
result = model.predict(source=temp_path, verbose=False)[0]
|
| 23 |
+
mask = result.masks.data.cpu().numpy()
|
| 24 |
+
total_area_px = mask.sum()
|
| 25 |
+
area_m2 = total_area_px * SCALE
|
| 26 |
+
estimated_cost = area_m2 * COST_PER_M2
|
| 27 |
+
|
| 28 |
+
# Overlay visualization
|
| 29 |
+
overlay = img_bgr.copy()
|
| 30 |
+
combined = np.max(mask, axis=0).astype(bool)
|
| 31 |
+
overlay[combined] = [0, 0, 255]
|
| 32 |
+
final = cv2.addWeighted(img_bgr, 0.7, overlay, 0.3, 0)
|
| 33 |
+
final_rgb = cv2.cvtColor(final, cv2.COLOR_BGR2RGB)
|
| 34 |
+
|
| 35 |
+
return final_rgb, f"{area_m2:.2f} m²", f"₹{estimated_cost:.0f}"
|
| 36 |
+
|
| 37 |
+
# Gradio Interface
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=estimate_cost_from_image,
|
| 40 |
+
inputs=gr.Image(type="numpy", label="Upload Pothole Image"),
|
| 41 |
+
outputs=[
|
| 42 |
+
gr.Image(type="numpy", label="Overlay"),
|
| 43 |
+
gr.Text(label="Estimated Area"),
|
| 44 |
+
gr.Text(label="Repair Cost"),
|
| 45 |
+
],
|
| 46 |
+
title="Pothole Repair Estimator",
|
| 47 |
+
description="Upload a pothole image to estimate repair cost using YOLOv8 segmentation model."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralyticsplus
|
| 2 |
+
ultralytics
|
| 3 |
+
opencv-python
|
| 4 |
+
gradio
|
| 5 |
+
numpy
|