-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
86 lines (60 loc) · 2.54 KB
/
example_usage.py
File metadata and controls
86 lines (60 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
GeoMatchAI minimal usage example.
"""
import asyncio
from pathlib import Path
from collections.abc import AsyncGenerator
from PIL import Image
from geomatchai import GeoMatchAI, config
from geomatchai.fetchers import BaseFetcher, MapillaryFetcher
class LocalFolderFetcher(BaseFetcher):
"""Custom fetcher that reads images from local folder."""
def __init__(self, folder: Path):
self.folder = Path(folder)
async def get_images(
self, lat: float, lon: float, num_images: int = 20
) -> AsyncGenerator[Image.Image, None]:
for img_path in self.folder.iterdir():
if img_path.suffix.lower() in {".jpg", ".jpeg", ".png"}:
yield Image.open(img_path).convert("RGB")
async def example_with_mapillary():
"""Example using MapillaryFetcher with config."""
print("Example 1: MapillaryFetcher")
# Configure library settings
# It automatically reads from MAPILLARY_API_KEY env var
# config.set_mapillary_api_key("YOUR_MAPILLARY_API_KEY")
config.set_device("cuda")
config.set_log_level("INFO")
# Create fetcher
fetcher = MapillaryFetcher(
api_token=config.get_mapillary_api_key()
) # Default value will be retrieved from config
# Create verifier (uses config defaults)
verifier = await GeoMatchAI.create(fetcher=fetcher)
# Verify image
with open("examples/input/wawel/wawel1.jpg", "rb") as f:
is_verified, score = await verifier.verify(50.054404, 19.935730, f.read())
print(f"Verified: {is_verified}, Score: {score:.3f}")
async def example_with_custom_fetcher():
"""Example using custom LocalFolderFetcher with config."""
print("Example 2: Custom LocalFolderFetcher")
# Configure library settings
config.set_log_level("INFO")
config.set_device("auto")
# Create custom fetcher
fetcher = LocalFolderFetcher(folder=Path("examples/input/wawel"))
# Create verifier (uses config defaults)
verifier = await GeoMatchAI.create(fetcher=fetcher)
# Verify image
with open("examples/input/wawel/wawel1.jpg", "rb") as f:
is_verified, score = await verifier.verify(50.054404, 19.935730, f.read())
print(f"Verified: {is_verified}, Score: {score:.3f}")
async def main():
# Run custom fetcher example (works without API key)
if (config.get_mapillary_api_key() is None) or (config.get_mapillary_api_key() == ""):
await example_with_custom_fetcher()
# Run Mapillary example (requires API key)
else:
await example_with_mapillary()
if __name__ == "__main__":
asyncio.run(main())