import gradio as gr from PIL import Image import json from datetime import datetime import os # Import the detector from detector import RoadsideDetector # Initialize the detector with the model print("🚀 Yol Kenarı Temizlik Uygulaması Başlatılıyor...") detector = RoadsideDetector('yol_kenari_tespit_best.pt') # Store detection history detection_history = [] def process_image(image): """ Process uploaded image and detect illegal dumping """ if image is None: return None, "❌ Lütfen bir resim yükleyin!", "İstatistik yok" try: # Process the image print("🔍 Resim işleniyor...") results = detector.process_image(image) # Add to history detection_history.append({ 'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M"), 'severity': results['severity']['level'], 'count': results['severity']['count'], 'types': list(results['severity']['types'].keys()) }) # Create statistics stats = f"📊 **Oturum İstatistikleri:**\n\n" stats += f"İşlenen resimler: {len(detection_history)}\n" if detection_history: high_count = sum(1 for d in detection_history if d['severity'] == 'HIGH') medium_count = sum(1 for d in detection_history if d['severity'] == 'MEDIUM') low_count = sum(1 for d in detection_history if d['severity'] == 'LOW') total_items = sum(d['count'] for d in detection_history) stats += f"🔴 Yüksek öncelik: {high_count}\n" stats += f"🟠 Orta öncelik: {medium_count}\n" stats += f"🟡 Düşük öncelik: {low_count}\n" stats += f"Toplam tespit: {total_items} öğe" return results['image'], results['summary'], stats except Exception as e: error_msg = f"❌ Hata: {str(e)}" print(error_msg) return image, error_msg, "Hata oluştu" def create_interface(): """ Create the Gradio interface """ # Custom CSS for better mobile display custom_css = """ .container { max-width: 100% !important; } .image-container { max-height: 500px !important; } @media (max-width: 768px) { .gradio-container { padding: 10px !important; } } """ with gr.Blocks(title="🚮 Yol Kenarı Temizlik", css=custom_css) as interface: # Header gr.Markdown(""" # 🚮 Yol Kenarı Temizlik ### 📱 Temiz Şehirler İçin Yapay Zeka **Nasıl Kullanılır:** 1. 📷 çöp, çöp kutusu ve taşan çöp fotoğrafı yükleyin 2. 🔍 AI çöp, çöp kutusu ve taşan çöpleri tespit eder 3. 📊 Sonuçları ve önem derecesini görün --- """) # Main interface with gr.Row(): with gr.Column(): # Input image input_image = gr.Image( type="pil", label="Resim Yükle veya Fotoğraf Çek", sources=["upload", "camera", "clipboard"] ) # Submit button submit_btn = gr.Button( "🔍 çöp, çöp kutusu ve taşan çöpleri Tespit Et", variant="primary", size="lg" ) # Example images (optional - you can remove if you don't have examples) gr.Examples( examples=[ ["examples/ornek1.jpg"], ["examples/ornek2.jpg"] ], inputs=input_image, label="Örnek Resimler" ) with gr.Column(): # Output image with detections output_image = gr.Image( label="Tespit Sonuçları", type="pil" ) # Summary text summary = gr.Textbox( label="Tespit Özeti", lines=8, max_lines=10 ) # Statistics stats = gr.Markdown("📊 İstatistikler burada görünecek") # Info section with gr.Accordion("ℹ️ Hakkında", open=False): gr.Markdown(""" ### 🎯 Ne Tespit Ediyoruz: - 🔴 **Çöp**: Mobilyalar, büyük atıklar - 🟠 **Çöp Kutusu**: Çöp konteynerleri - 🟡 **Taşan Çöp**: Çöp torbaları, dağınık atıklar ### 📊 Önem Dereceleri: - **YÜKSEK** (🔴): 5+ öğe - Acil temizlik gerekli - **ORTA** (🟠): 2-4 öğe - Temizlik planlanmalı - **DÜŞÜK** (🟡): 1 öğe - Küçük sorun ### 🏆 Doğruluk: Bu model 100 görüntü ile eğitildi ve ~%70 doğruluk sağlıyor. --- Temiz şehirler için ❤️ ile yapıldı | Versiyon 1.0 """) # Connect button to function submit_btn.click( process_image, inputs=[input_image], outputs=[output_image, summary, stats] ) return interface # Launch the app if __name__ == "__main__": print("="*50) print("🚀 Yol Kenarı Temizlik - Hazır!") print("="*50) # Create interface and launch app = create_interface() app.launch( share=False, server_name="0.0.0.0", server_port=7860 )