Spaces:
Sleeping
Sleeping
import gradio as gr | |
from ultralytics import YOLO | |
from gtts import gTTS | |
import os | |
from PIL import Image as PILImage | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import cv2 | |
# Load model YOLOv11 | |
model = YOLO('last.pt') | |
# Fungsi untuk menjelaskan tanda lalu lintas | |
def explain_signs(labels): | |
explanations = { | |
"Stop": "Tanda berhenti. Pengemudi harus berhenti sebelum melanjutkan.", | |
"Yield": "Tanda memberi jalan. Pengemudi harus memberi jalan kepada kendaraan lain.", | |
"Speed limit": "Tanda batas kecepatan. Pengemudi tidak boleh melebihi batas kecepatan yang ditentukan.", | |
# Tambahkan penjelasan untuk tanda lalu lintas lainnya... | |
} | |
explained_text = [] | |
for label in labels: | |
explanation = explanations.get(label, f"Tanda {label} tidak memiliki penjelasan.") | |
explained_text.append(explanation) | |
return " ".join(explained_text) | |
def detect_traffic_signs(image_path): | |
results = model.predict(source=image_path, conf=0.25) | |
detected_labels = [] | |
original_image = cv2.imread(image_path) | |
original_image_rgb = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) | |
for result in results: | |
for box in result.boxes: | |
x1, y1, x2, y2 = box.xyxy[0] | |
label = result.names[int(box.cls)] | |
confidence = box.conf[0] | |
color = (0, 255, 0) | |
cv2.rectangle(original_image_rgb, (int(x1), int(y1)), (int(x2), int(y2)), color, 2) | |
text = f"{label}: {confidence:.2f}" | |
cv2.putText(original_image_rgb, text, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) | |
detected_labels.append(label) | |
output_image_path = "output_image_with_boxes.jpg" | |
plt.imsave(output_image_path, original_image_rgb) | |
output_text = ", ".join(detected_labels) if detected_labels else "Tidak ada tanda lalu lintas yang terdeteksi." | |
explanation = explain_signs(detected_labels) if detected_labels else "Tidak ada penjelasan yang tersedia." | |
tts = gTTS(explanation, lang='id') | |
tts_file = "output.mp3" | |
tts.save(tts_file) | |
return output_text, output_image_path, tts_file | |
# Fungsi untuk mengambil contoh gambar dari folder output, dengan maksimum 5 gambar | |
def get_examples(output_folder): | |
examples = [os.path.join(output_folder, filename) for filename in os.listdir(output_folder) if filename.endswith('.jpg')] | |
return examples[:5] # Mengambil maksimal 5 gambar | |
# Mengatur folder output | |
output_folder = "output_images" | |
os.makedirs(output_folder, exist_ok=True) # Membuat folder jika belum ada | |
# Mengatur contoh gambar yang diunggah | |
examples = get_examples(output_folder) | |
# Mengatur antarmuka Gradio | |
iface = gr.Interface( | |
fn=detect_traffic_signs, | |
inputs=gr.Image(type="filepath", label="Unggah Gambar Tanda Lalu Lintas"), | |
outputs=[ | |
gr.Textbox(label="Hasil Deteksi", interactive=False, placeholder="Hasil akan ditampilkan di sini...", elem_id="result_textbox"), | |
gr.Image(label="Gambar dengan Bounding Box"), | |
gr.Audio(label="Output Suara", type="filepath") | |
], | |
examples=examples, | |
title="Deteksi Tanda Lalu Lintas untuk Mobil Self-Driving", | |
description="Unggah gambar untuk mendeteksi tanda lalu lintas. Model YOLOv11 digunakan untuk mendeteksi tanda lalu lintas dan output suara akan menjelaskan tanda yang terdeteksi. Silakan tekan tombol play untuk mendengar output suara.", | |
theme="compact", | |
css=""" | |
.gradio-container { | |
background: linear-gradient(135deg, #1DB954, #BB86FC, #3700B3); | |
color: #ffffff; | |
border-radius: 10px; | |
padding: 20px; | |
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5); | |
} | |
h1, h2, h3 { | |
color: #000000; | |
} | |
#result_textbox { | |
background-color: rgba(255, 255, 255, 0.1); | |
color: #ffffff; | |
border-radius: 5px; | |
} | |
""" | |
) | |
# Menjalankan antarmuka | |
iface.launch() | |