Spaces:
Build error
Build error
Upload 5 files
Browse files- README.md +0 -12
- app.py +130 -0
- models/best.pt +3 -0
- requirements.txt +7 -0
- utils/detection.py +0 -0
README.md
CHANGED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Road Damage Detection
|
| 3 |
-
emoji: 🚀
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: gray
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 6.2.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Load model
|
| 10 |
+
model = YOLO('models/best.pt')
|
| 11 |
+
|
| 12 |
+
# Class mapping sesuai dengan mobile app
|
| 13 |
+
CLASS_NAMES = {
|
| 14 |
+
0: 'amblas',
|
| 15 |
+
1: 'bergelombang',
|
| 16 |
+
2: 'berlubang',
|
| 17 |
+
3: 'retak_buaya'
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def detect_road_damage(image, confidence_threshold=0.5):
|
| 21 |
+
"""
|
| 22 |
+
Deteksi kerusakan jalan dari gambar
|
| 23 |
+
"""
|
| 24 |
+
try:
|
| 25 |
+
# Convert PIL to numpy array
|
| 26 |
+
if isinstance(image, Image.Image):
|
| 27 |
+
image = np.array(image)
|
| 28 |
+
|
| 29 |
+
# Run inference
|
| 30 |
+
results = model(image, conf=confidence_threshold)
|
| 31 |
+
|
| 32 |
+
detections = []
|
| 33 |
+
annotated_image = image.copy()
|
| 34 |
+
|
| 35 |
+
for result in results:
|
| 36 |
+
boxes = result.boxes
|
| 37 |
+
if boxes is not None:
|
| 38 |
+
for box in boxes:
|
| 39 |
+
# Extract detection info
|
| 40 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 41 |
+
confidence = float(box.conf[0])
|
| 42 |
+
class_id = int(box.cls[0])
|
| 43 |
+
class_name = CLASS_NAMES.get(class_id, 'unknown')
|
| 44 |
+
|
| 45 |
+
# Calculate dimensions (estimasi)
|
| 46 |
+
width_pixels = x2 - x1
|
| 47 |
+
height_pixels = y2 - y1
|
| 48 |
+
|
| 49 |
+
# Estimasi ukuran dalam cm (asumsi 1 pixel = 0.1 cm)
|
| 50 |
+
width_cm = width_pixels * 0.1
|
| 51 |
+
depth_cm = height_pixels * 0.1
|
| 52 |
+
|
| 53 |
+
detection = {
|
| 54 |
+
'class': class_name,
|
| 55 |
+
'confidence': confidence,
|
| 56 |
+
'bbox': [float(x1), float(y1), float(x2), float(y2)],
|
| 57 |
+
'width_cm': width_cm,
|
| 58 |
+
'depth_cm': depth_cm
|
| 59 |
+
}
|
| 60 |
+
detections.append(detection)
|
| 61 |
+
|
| 62 |
+
# Draw bounding box
|
| 63 |
+
cv2.rectangle(annotated_image,
|
| 64 |
+
(int(x1), int(y1)), (int(x2), int(y2)),
|
| 65 |
+
(0, 255, 0), 2)
|
| 66 |
+
|
| 67 |
+
# Add label
|
| 68 |
+
label = f"{class_name}: {confidence:.2f}"
|
| 69 |
+
cv2.putText(annotated_image, label,
|
| 70 |
+
(int(x1), int(y1-10)),
|
| 71 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 72 |
+
|
| 73 |
+
# Prepare response
|
| 74 |
+
response = {
|
| 75 |
+
'detections': detections,
|
| 76 |
+
'count': len(detections),
|
| 77 |
+
'status': 'success'
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
return annotated_image, json.dumps(response, indent=2)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
error_response = {
|
| 84 |
+
'error': str(e),
|
| 85 |
+
'status': 'error'
|
| 86 |
+
}
|
| 87 |
+
return image, json.dumps(error_response, indent=2)
|
| 88 |
+
|
| 89 |
+
# Gradio interface
|
| 90 |
+
with gr.Blocks(title="VGTec Road Damage Detector") as demo:
|
| 91 |
+
gr.Markdown("# 🛣️ VGTec Road Damage Detection API")
|
| 92 |
+
gr.Markdown("Upload gambar jalan untuk mendeteksi kerusakan (amblas, bergelombang, berlubang, retak_buaya)")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
input_image = gr.Image(type="pil", label="Upload Gambar Jalan")
|
| 97 |
+
confidence_slider = gr.Slider(
|
| 98 |
+
minimum=0.1,
|
| 99 |
+
maximum=1.0,
|
| 100 |
+
value=0.5,
|
| 101 |
+
step=0.1,
|
| 102 |
+
label="Confidence Threshold"
|
| 103 |
+
)
|
| 104 |
+
detect_btn = gr.Button("🔍 Deteksi Kerusakan", variant="primary")
|
| 105 |
+
|
| 106 |
+
with gr.Column():
|
| 107 |
+
output_image = gr.Image(label="Hasil Deteksi")
|
| 108 |
+
output_json = gr.Code(label="JSON Response", language="json")
|
| 109 |
+
|
| 110 |
+
# Event handler
|
| 111 |
+
detect_btn.click(
|
| 112 |
+
fn=detect_road_damage,
|
| 113 |
+
inputs=[input_image, confidence_slider],
|
| 114 |
+
outputs=[output_image, output_json]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Examples
|
| 118 |
+
gr.Examples(
|
| 119 |
+
examples=[
|
| 120 |
+
["examples/berlubang.jpg", 0.5],
|
| 121 |
+
["examples/retak_buaya.jpg", 0.6],
|
| 122 |
+
],
|
| 123 |
+
inputs=[input_image, confidence_slider],
|
| 124 |
+
outputs=[output_image, output_json],
|
| 125 |
+
fn=detect_road_damage,
|
| 126 |
+
cache_examples=True
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|
models/best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:854825ce8d6a0c0179e51f863c4627761f6d95a729c6cf5b646236bad96236e2
|
| 3 |
+
size 6254762
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics==8.0.196
|
| 2 |
+
gradio==4.44.0
|
| 3 |
+
torch==2.1.0
|
| 4 |
+
torchvision==0.16.0
|
| 5 |
+
opencv-python==4.8.1.78
|
| 6 |
+
Pillow==10.0.1
|
| 7 |
+
numpy==1.24.3
|
utils/detection.py
ADDED
|
File without changes
|