ziffir commited on
Commit
13e431f
·
verified ·
1 Parent(s): 0ac9267

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -4
app.py CHANGED
@@ -1,7 +1,94 @@
1
  import gradio as gr
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoImageProcessor, AutoModel
4
+ from PIL import Image
5
+ import numpy as np
6
+ import rasterio
7
+ from rasterio.warp import reproject, Resampling
8
+ import os
9
 
10
+ # MODEL YÜKLE
11
+ processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
12
+ model = AutoModel.from_pretrained("facebook/dinov2-base")
13
+ model.eval()
14
 
15
+ # SENTINEL-2 REFERANS (Örnek: Whiskeytown)
16
+ REF_IMAGE = "whiskeytown_ref.png" # 384x384, 10m/pixel
17
+ REF_TRANSFORM = rasterio.Affine(10.0, 0.0, -125.0, 0.0, -10.0, 40.6) # x, y, lat, lon
18
+ REF_CRS = "EPSG:4326"
19
+
20
+ def georeference(image, location):
21
+ if image is None:
22
+ return None, None, "Lütfen bir görüntü yükleyin."
23
+
24
+ # 1. Görüntüyü hazırla
25
+ img = Image.fromarray(image)
26
+ inputs = processor(images=img, return_tensors="pt")
27
+ with torch.no_grad():
28
+ features = model(**inputs).last_hidden_state[:, 0] # CLS token
29
+
30
+ # 2. Basit eşleştirme (demo için sabit H matrisi)
31
+ # Gerçekte: DINOv2 + homografi tahmini
32
+ H = np.array([
33
+ [1.0, 0.0, 50],
34
+ [0.0, 1.0, 30],
35
+ [0.0, 0.0, 1.0]
36
+ ])
37
+
38
+ # 3. GeoTIFF oluştur
39
+ output_tif = "output_geotiff.tif"
40
+ with rasterio.open(REF_IMAGE) as ref_src:
41
+ profile = ref_src.profile.copy()
42
+ profile.update({
43
+ 'height': image.shape[0],
44
+ 'width': image.shape[1],
45
+ 'count': 3,
46
+ 'dtype': 'uint8',
47
+ 'crs': 'EPSG:3857',
48
+ 'transform': REF_TRANSFORM
49
+ })
50
+
51
+ warped = np.zeros((3, image.shape[0], image.shape[1]), dtype=np.uint8)
52
+ for i in range(3):
53
+ reproject(
54
+ source=image[:, :, i],
55
+ destination=warped[i],
56
+ src_transform=rasterio.Affine(1, 0, 0, 0, 1, 0),
57
+ dst_transform=REF_TRANSFORM,
58
+ resampling=Resampling.cubic
59
+ )
60
+
61
+ with rasterio.open(output_tif, 'w', **profile) as dst:
62
+ dst.write(warped)
63
+
64
+ # 4. .points dosyası (QGIS için)
65
+ points_file = "output.points"
66
+ with open(points_file, 'w') as f:
67
+ f.write("mapX,mapY,pixelX,pixelY,enable\n")
68
+ h, w = image.shape[:2]
69
+ f.write(f"-125.0,40.6,0,0,1\n")
70
+ f.write(f"-124.9,40.6,{w},0,1\n")
71
+ f.write(f"-125.0,40.5,0,{h},1\n")
72
+ f.write(f"-124.9,40.5,{w},{h},1\n")
73
+
74
+ return output_tif, points_file, f"Başarılı! Konum: {location}"
75
+
76
+ # GRADIO ARAYÜZÜ
77
+ with gr.Blocks(title="AI Georeferencer") as demo:
78
+ gr.Markdown("# AI Georeferencer (Bunting Labs Gibi)")
79
+ gr.Markdown("Taranmış harita yükle → konum yaz → GeoTIFF al!")
80
+
81
+ with gr.Row():
82
+ with gr.Column():
83
+ img_input = gr.Image(label="Görüntü Yükle (PNG/JPG/TIFF)", type="numpy")
84
+ location = gr.Textbox(label="Yaklaşık Konum", placeholder="Ör: Whiskeytown, CA")
85
+ btn = gr.Button("Jeoreferansla →")
86
+
87
+ with gr.Column():
88
+ tif_out = gr.File(label="GeoTIFF İndir")
89
+ points_out = gr.File(label=".points Dosyası (QGIS)")
90
+ status = gr.Textbox(label="Sonuç")
91
+
92
+ btn.click(georeference, inputs=[img_input, location], outputs=[tif_out, points_out, status])
93
+
94
+ demo.launch()