ziffir's picture
Update app.py
1b98f74 verified
raw
history blame
4.92 kB
# app.py - Hafif Jeo-Referanslama Demo
import gradio as gr
import requests
from PIL import Image
import json
import tempfile
import folium
import os
class LightweightGeoDemo:
def __init__(self):
self.dataset_info = {
'name': 'allenai/s2-naip',
'description': 'Sentinel-2 ve NAIP görüntü çiftleri'
}
def predict_coordinates(self, image):
"""Basit koordinat tahmini (demo amaçlı)"""
try:
# Demo için rastgele koordinatlar
import random
lat = random.uniform(36.0, 42.0) # Türkiye enlemleri
lon = random.uniform(26.0, 45.0) # Türkiye boylamları
# Görüntü boyutuna göre basit güven skoru
if hasattr(image, 'size'):
confidence = min(0.8, image.size[0] / 1000 * 0.1)
else:
confidence = 0.5
result = {
'latitude': round(lat, 4),
'longitude': round(lon, 4),
'confidence': round(confidence, 2),
'location': 'Türkiye',
'method': 'Demo Tahmini'
}
return result
except Exception as e:
return {'error': str(e)}
def create_simple_map(self, lat, lon):
"""Basit harita oluştur"""
try:
# Folium haritası
m = folium.Map(location=[lat, lon], zoom_start=10)
# Marker ekle
folium.Marker(
[lat, lon],
popup=f'Tahmin: {lat}, {lon}',
tooltip='Tahmin Edilen Konum'
).add_to(m)
# Geçici dosyaya kaydet
with tempfile.NamedTemporaryFile(suffix='.html', delete=False) as tmp:
m.save(tmp.name)
return tmp.name
except Exception as e:
return None
def create_demo_interface():
demo = LightweightGeoDemo()
with gr.Blocks(title="🌍 Jeo-Referanslama Demo", theme=gr.themes.Soft()) as interface:
gr.Markdown("""
# 🌍 Jeo-Referanslama Demo
**Hafif AI destekli koordinat tahmini sistemi**
Bu demo, S2-NAIP dataseti için geliştirilmiş jeo-referanslama sisteminin basit bir versiyonudur.
""")
with gr.Row():
with gr.Column():
image_input = gr.Image(
type="filepath",
label="Uydu Görüntüsü Yükle",
height=300
)
predict_btn = gr.Button("📍 Koordinatları Tahmin Et", variant="primary")
with gr.Column():
output_json = gr.JSON(label="Tahmin Sonuçları")
output_map = gr.HTML(label="Harita Görünümü")
# Örnek görüntüler
gr.Markdown("### 📸 Örnek Görüntüler")
with gr.Row():
gr.Examples(
examples=[
["https://raw.githubusercontent.com/usc-isi-i2/datasets/master/s2-NAIP/sample_images/sentinel_sample1.jpg"],
["https://raw.githubusercontent.com/usc-isi-i2/datasets/master/s2-NAIP/sample_images/naip_sample1.jpg"]
],
inputs=image_input,
label="Örnek Görüntüler"
)
# Dataset bilgisi
with gr.Accordion("ℹ️ Dataset Bilgisi", open=False):
gr.Markdown(f"""
**Kullanılan Dataset:** {demo.dataset_info['name']}
**Açıklama:** {demo.dataset_info['description']}
**Özellikler:**
- Sentinel-2 (10m çözünürlük) görüntüleri
- NAIP (1m çözünürlük) görüntüleri
- Koordinat metadata'sı
- ABD genelinde çeşitli lokasyonlar
**Not:** Bu demo basit tahminler yapar. Tam model eğitimi gereklidir.
""")
# Tahmin butonu işlevi
def process_prediction(image):
result = demo.predict_coordinates(image)
if 'error' not in result:
map_path = demo.create_simple_map(result['latitude'], result['longitude'])
if map_path:
with open(map_path, 'r') as f:
map_html = f.read()
os.unlink(map_path) # Temizlik
return result, map_html
return result, "<p>Harita oluşturulamadı</p>"
predict_btn.click(
fn=process_prediction,
inputs=image_input,
outputs=[output_json, output_map]
)
return interface
if __name__ == "__main__":
demo = create_demo_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)