Spaces:
Sleeping
Sleeping
Bastien Dechamps
commited on
Commit
·
dfbe385
1
Parent(s):
95a008f
[ADD] Random Guessr
Browse files- .gitignore +164 -0
- README.md +2 -11
- app.py +60 -0
- geoguessr_bot/__init__.py +0 -0
- geoguessr_bot/guessr/__init__.py +2 -0
- geoguessr_bot/guessr/abstract_guessr.py +51 -0
- geoguessr_bot/guessr/random_guessr.py +19 -0
- geoguessr_bot/interfaces.py +9 -0
- requirements.txt +0 -0
.gitignore
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
.idea/
|
161 |
+
|
162 |
+
# Perso
|
163 |
+
data/
|
164 |
+
models/
|
README.md
CHANGED
@@ -1,12 +1,3 @@
|
|
1 |
-
|
2 |
-
title: Geoguessr Bot
|
3 |
-
emoji: 👀
|
4 |
-
colorFrom: pink
|
5 |
-
colorTo: indigo
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.28.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
|
12 |
-
|
|
|
1 |
+
# Geoguessr bot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
A bot playing Geoguessr using Computer Vision models. WIP... :wrench:
|
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
|
5 |
+
from geoguessr_bot.guessr import RandomGuessr, AbstractGuessr
|
6 |
+
|
7 |
+
ALL_GUESSR_CLASS = {
|
8 |
+
"random": RandomGuessr
|
9 |
+
}
|
10 |
+
|
11 |
+
ALL_GUESSR_ARGS = {
|
12 |
+
"random": {}
|
13 |
+
}
|
14 |
+
|
15 |
+
# For instantiating guessrs only when needed
|
16 |
+
ALL_GUESSR = {}
|
17 |
+
|
18 |
+
|
19 |
+
def create_map(guessr: str) -> go.Figure:
|
20 |
+
"""Create an interactive map
|
21 |
+
"""
|
22 |
+
# Instantiate guessr if not already done
|
23 |
+
if guessr not in ALL_GUESSR:
|
24 |
+
ALL_GUESSR[guessr] = ALL_GUESSR_CLASS[guessr](**ALL_GUESSR_ARGS[guessr])
|
25 |
+
return AbstractGuessr.create_map()
|
26 |
+
|
27 |
+
|
28 |
+
def guess(guessr: str, uploaded_image) -> go.Figure:
|
29 |
+
"""Guess a coordinate from an image uploaded in the Gradio interface
|
30 |
+
"""
|
31 |
+
# Instantiate guessr if not already done
|
32 |
+
if guessr not in ALL_GUESSR:
|
33 |
+
ALL_GUESSR[guessr] = ALL_GUESSR_CLASS[guessr](**ALL_GUESSR_ARGS[guessr])
|
34 |
+
# Convert image to numpy array
|
35 |
+
uploaded_image = np.array(uploaded_image)
|
36 |
+
# Guess coordinate
|
37 |
+
guess_coordinate = ALL_GUESSR[guessr].guess(uploaded_image)
|
38 |
+
# Create map
|
39 |
+
fig = ALL_GUESSR[guessr].create_map(guess_coordinate)
|
40 |
+
return fig
|
41 |
+
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
# Create & launch Gradio interface
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
with gr.Row():
|
47 |
+
with gr.Column():
|
48 |
+
guessr_dropdown = gr.Dropdown(
|
49 |
+
list(ALL_GUESSR_CLASS.keys()),
|
50 |
+
value="random",
|
51 |
+
label="Guessr type",
|
52 |
+
info="More Guessr types will be added soon!"
|
53 |
+
)
|
54 |
+
image = gr.Image(shape=(800, 800))
|
55 |
+
button = gr.Button(text="Guess")
|
56 |
+
interactive_map = gr.Plot()
|
57 |
+
demo.load(create_map, [guessr_dropdown], interactive_map)
|
58 |
+
button.click(guess, [guessr_dropdown, image], interactive_map)
|
59 |
+
# Launch demo 🚀
|
60 |
+
demo.launch()
|
geoguessr_bot/__init__.py
ADDED
File without changes
|
geoguessr_bot/guessr/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
from .abstract_guessr import AbstractGuessr
|
2 |
+
from .random_guessr import RandomGuessr
|
geoguessr_bot/guessr/abstract_guessr.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from abc import abstractmethod
|
2 |
+
from dataclasses import dataclass
|
3 |
+
from typing import Optional
|
4 |
+
|
5 |
+
from PIL import Image
|
6 |
+
import plotly.graph_objects as go
|
7 |
+
|
8 |
+
from geoguessr_bot.interfaces import Coordinate
|
9 |
+
|
10 |
+
|
11 |
+
@dataclass
|
12 |
+
class AbstractGuessr:
|
13 |
+
"""Abstract class for a guessr
|
14 |
+
"""
|
15 |
+
@abstractmethod
|
16 |
+
def guess(self, image: Image) -> Coordinate:
|
17 |
+
raise NotImplementedError
|
18 |
+
|
19 |
+
def guess_from_path(self, image_path: str) -> Coordinate:
|
20 |
+
image = Image.open(image_path)
|
21 |
+
return self.guess(image)
|
22 |
+
|
23 |
+
@staticmethod
|
24 |
+
def create_map(guess_coordinate: Optional[Coordinate] = None, **kwargs) -> go.Figure:
|
25 |
+
"""Create an interactive map showing a coordinate
|
26 |
+
"""
|
27 |
+
fig = go.Figure(go.Scattermapbox(
|
28 |
+
customdata=[guess_coordinate.__str__()] if guess_coordinate is not None else None,
|
29 |
+
lat=[guess_coordinate.latitude] if guess_coordinate is not None else None,
|
30 |
+
lon=[guess_coordinate.longitude] if guess_coordinate is not None else None,
|
31 |
+
mode="markers",
|
32 |
+
marker=go.scattermapbox.Marker(
|
33 |
+
size=6,
|
34 |
+
),
|
35 |
+
hoverinfo="text",
|
36 |
+
hovertemplate='coord: %{customdata[0]}'
|
37 |
+
))
|
38 |
+
fig.update_layout(
|
39 |
+
mapbox_style="open-street-map",
|
40 |
+
hovermode='closest',
|
41 |
+
mapbox=dict(
|
42 |
+
bearing=0,
|
43 |
+
center=go.layout.mapbox.Center(
|
44 |
+
lat=guess_coordinate.latitude if guess_coordinate is not None else 0,
|
45 |
+
lon=guess_coordinate.longitude if guess_coordinate is not None else 0
|
46 |
+
),
|
47 |
+
pitch=0,
|
48 |
+
zoom=3 if guess_coordinate is not None else 0
|
49 |
+
),
|
50 |
+
)
|
51 |
+
return fig
|
geoguessr_bot/guessr/random_guessr.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
from .abstract_guessr import AbstractGuessr
|
5 |
+
from ..interfaces import Coordinate
|
6 |
+
|
7 |
+
|
8 |
+
class RandomGuessr(AbstractGuessr):
|
9 |
+
"""Guesses a random coordinate
|
10 |
+
"""
|
11 |
+
@staticmethod
|
12 |
+
def _random_coordinate() -> Coordinate:
|
13 |
+
return Coordinate(
|
14 |
+
latitude=np.random.uniform(-90, 90),
|
15 |
+
longitude=np.random.uniform(-180, 180)
|
16 |
+
)
|
17 |
+
|
18 |
+
def guess(self, image: Image) -> Coordinate:
|
19 |
+
return self._random_coordinate()
|
geoguessr_bot/interfaces.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic.main import BaseModel
|
2 |
+
|
3 |
+
|
4 |
+
class Coordinate(BaseModel):
|
5 |
+
latitude: float
|
6 |
+
longitude: float
|
7 |
+
|
8 |
+
def __str__(self):
|
9 |
+
return f"({round(self.latitude, 6)}, {round(self.longitude, 6)})"
|
requirements.txt
ADDED
File without changes
|