mustafa2ak commited on
Commit
ce51d13
·
verified ·
1 Parent(s): db08ca7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -0
app.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import json
4
+ from datetime import datetime
5
+ import os
6
+
7
+ # Import the detector
8
+ from detector import RoadsideDetector
9
+
10
+ # Initialize the detector with the model
11
+ print("🚀 Yol Kenarı Temizlik Uygulaması Başlatılıyor...")
12
+ detector = RoadsideDetector('yol_kenari_tespit_best.pt')
13
+
14
+ # Store detection history
15
+ detection_history = []
16
+
17
+ def process_image(image):
18
+ """
19
+ Process uploaded image and detect illegal dumping
20
+ """
21
+ if image is None:
22
+ return None, "❌ Lütfen bir resim yükleyin!", "İstatistik yok"
23
+
24
+ try:
25
+ # Process the image
26
+ print("🔍 Resim işleniyor...")
27
+ results = detector.process_image(image)
28
+
29
+ # Add to history
30
+ detection_history.append({
31
+ 'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M"),
32
+ 'severity': results['severity']['level'],
33
+ 'count': results['severity']['count'],
34
+ 'types': list(results['severity']['types'].keys())
35
+ })
36
+
37
+ # Create statistics
38
+ stats = f"📊 **Oturum İstatistikleri:**\n\n"
39
+ stats += f"İşlenen resimler: {len(detection_history)}\n"
40
+
41
+ if detection_history:
42
+ high_count = sum(1 for d in detection_history if d['severity'] == 'HIGH')
43
+ medium_count = sum(1 for d in detection_history if d['severity'] == 'MEDIUM')
44
+ low_count = sum(1 for d in detection_history if d['severity'] == 'LOW')
45
+ total_items = sum(d['count'] for d in detection_history)
46
+
47
+ stats += f"🔴 Yüksek öncelik: {high_count}\n"
48
+ stats += f"🟠 Orta öncelik: {medium_count}\n"
49
+ stats += f"🟡 Düşük öncelik: {low_count}\n"
50
+ stats += f"Toplam tespit: {total_items} öğe"
51
+
52
+ return results['image'], results['summary'], stats
53
+
54
+ except Exception as e:
55
+ error_msg = f"❌ Hata: {str(e)}"
56
+ print(error_msg)
57
+ return image, error_msg, "Hata oluştu"
58
+
59
+ def create_interface():
60
+ """
61
+ Create the Gradio interface
62
+ """
63
+ # Custom CSS for better mobile display
64
+ custom_css = """
65
+ .container { max-width: 100% !important; }
66
+ .image-container { max-height: 500px !important; }
67
+ @media (max-width: 768px) {
68
+ .gradio-container { padding: 10px !important; }
69
+ }
70
+ """
71
+
72
+ with gr.Blocks(title="🚮 Yol Kenarı Temizlik", css=custom_css) as interface:
73
+
74
+ # Header
75
+ gr.Markdown("""
76
+ # 🚮 Yol Kenarı Temizlik
77
+
78
+ ### 📱 Temiz Şehirler İçin Yapay Zeka
79
+
80
+ **Nasıl Kullanılır:**
81
+ 1. 📷 çöp, çöp kutusu ve taşan çöp fotoğrafı yükleyin
82
+ 2. 🔍 AI çöp, çöp kutusu ve taşan çöpleri tespit eder
83
+ 3. 📊 Sonuçları ve önem derecesini görün
84
+
85
+ ---
86
+ """)
87
+
88
+ # Main interface
89
+ with gr.Row():
90
+ with gr.Column():
91
+ # Input image
92
+ input_image = gr.Image(
93
+ type="pil",
94
+ label="Resim Yükle veya Fotoğraf Çek",
95
+ sources=["upload", "camera", "clipboard"]
96
+ )
97
+
98
+ # Submit button
99
+ submit_btn = gr.Button(
100
+ "🔍 çöp, çöp kutusu ve taşan çöpleri Tespit Et",
101
+ variant="primary",
102
+ size="lg"
103
+ )
104
+
105
+ # Example images (optional - you can remove if you don't have examples)
106
+ gr.Examples(
107
+ examples=[
108
+ ["examples/ornek1.jpg"],
109
+ ["examples/ornek2.jpg"]
110
+ ],
111
+ inputs=input_image,
112
+ label="Örnek Resimler"
113
+ )
114
+
115
+ with gr.Column():
116
+ # Output image with detections
117
+ output_image = gr.Image(
118
+ label="Tespit Sonuçları",
119
+ type="pil"
120
+ )
121
+
122
+ # Summary text
123
+ summary = gr.Textbox(
124
+ label="Tespit Özeti",
125
+ lines=8,
126
+ max_lines=10
127
+ )
128
+
129
+ # Statistics
130
+ stats = gr.Markdown("📊 İstatistikler burada görünecek")
131
+
132
+ # Info section
133
+ with gr.Accordion("ℹ️ Hakkında", open=False):
134
+ gr.Markdown("""
135
+ ### 🎯 Ne Tespit Ediyoruz:
136
+ - 🔴 **Çöp**: Mobilyalar, büyük atıklar
137
+ - 🟠 **Çöp Kutusu**: Çöp konteynerleri
138
+ - 🟡 **Taşan Çöp**: Çöp torbaları, dağınık atıklar
139
+
140
+ ### 📊 Önem Dereceleri:
141
+ - **YÜKSEK** (🔴): 5+ öğe - Acil temizlik gerekli
142
+ - **ORTA** (🟠): 2-4 öğe - Temizlik planlanmalı
143
+ - **DÜŞÜK** (🟡): 1 öğe - Küçük sorun
144
+
145
+ ### 🏆 Doğruluk:
146
+ Bu model 100 görüntü ile eğitildi ve ~%70 doğruluk sağlıyor.
147
+
148
+ ---
149
+ Temiz şehirler için ❤️ ile yapıldı | Versiyon 1.0
150
+ """)
151
+
152
+ # Connect button to function
153
+ submit_btn.click(
154
+ process_image,
155
+ inputs=[input_image],
156
+ outputs=[output_image, summary, stats]
157
+ )
158
+
159
+ return interface
160
+
161
+ # Launch the app
162
+ if __name__ == "__main__":
163
+ print("="*50)
164
+ print("🚀 Yol Kenarı Temizlik - Hazır!")
165
+ print("="*50)
166
+
167
+ # Create interface and launch
168
+ app = create_interface()
169
+ app.launch(
170
+ share=False,
171
+ server_name="0.0.0.0",
172
+ server_port=7860
173
+ )