-
Notifications
You must be signed in to change notification settings - Fork 28
/
gvision.py
277 lines (242 loc) · 12.3 KB
/
gvision.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import io
import cv2
import streamlit as st
from google.cloud import vision
from google.cloud.vision_v1 import types
from google.oauth2 import service_account
from streamlit_folium import folium_static
import folium
from PIL import Image
import xyzservices.providers as xyz
import json
# Set page style
st.set_page_config(page_title='GVision', page_icon='📷', layout='wide')
# Set page logo
logo_path = "gvision.png"
logo = Image.open(logo_path)
st.image(logo, width=320)
# Add a button to display the readme.md file in a popup
if st.sidebar.checkbox('README'):
with open('readme.md', 'r', encoding='UTF-8') as f:
readme = f.read()
st.info(readme)
# Set sidebar title and description
st.sidebar.title('ℹ️ About')
st.sidebar.info('GVision is a reverse image search app that use Google Cloud Vision API to detect landmarks and web entities from images, helping you gather valuable information quickly and easily.')
st.sidebar.markdown('----')
# Add a button to upload a config file
config_slot = st.empty()
config_file = config_slot.file_uploader('Upload a config file', type=['json'])
# Load the credentials from the config file
if config_file is not None:
content = config_file.read()
try:
credentials = service_account.Credentials.from_service_account_info(json.loads(content))
client = vision.ImageAnnotatorClient(credentials=credentials)
config_slot.empty()
# Add examples of supported image formats, sizes, and resolutions
st.sidebar.subheader('🖼️ Supported image formats:')
st.sidebar.markdown("""
- JPG
- JPEG
- PNG
""")
st.sidebar.markdown('----')
# Add free tier data
st.sidebar.subheader('⚠️ Free: first 1000 units/month')
st.sidebar.markdown('----')
# Provide a link or reference to the Google Cloud Vision API documentation or pricing
st.sidebar.subheader('📘 Resources:')
st.sidebar.markdown("""
- [Cloud Vision API Documentation](https://cloud.google.com/vision/docs)
- [Cloud Vision API Pricing](https://cloud.google.com/vision/pricing)
----
""")
# Add a button to reset the app to its default state or to clear the uploaded image and results
st.sidebar.button('Reset app')
# Upload image
uploaded_file = st.file_uploader('Choose an image', type=['jpg', 'jpeg', 'png'], accept_multiple_files=False)
def create_folium_map(landmarks):
providers = xyz.flatten()
selection = ['OpenTopoMap',
'Stamen.Toner',
'Stamen.Terrain',
'Stamen.TerrainBackground',
'Stamen.Watercolor',
'CartoDB.Positron',
'CartoDB.Voyager',
'WaymarkedTrails.hiking',
'WaymarkedTrails.cycling',
'WaymarkedTrails.mtb',
'WaymarkedTrails.slopes',
'WaymarkedTrails.riding',
'WaymarkedTrails.skating'
]
# Create a map centered on the first detected location using Folium
m = folium.Map(
location=[landmarks[0].locations[0].lat_lng.latitude, landmarks[0].locations[0].lat_lng.longitude],
zoom_start=15)
for landmark in landmarks:
# Add a marker to the existing map for each detected location
tooltip = landmark.description
folium.Marker(
location=[landmark.locations[0].lat_lng.latitude, landmark.locations[0].lat_lng.longitude],
tooltip=tooltip).add_to(m)
for tiles_name in selection:
tiles = providers[tiles_name]
folium.TileLayer(
tiles=tiles.build_url(),
attr=tiles.html_attribution,
name=tiles.name,
).add_to(m)
folium.LayerControl().add_to(m)
return m
if uploaded_file is not None:
with st.spinner('Analyzing the image...'):
# Read the image file
content = uploaded_file.read()
# Perform landmark detection on the image
image = types.Image(content=content)
response = client.landmark_detection(image=image)
# Extract the detected landmarks and their geolocation
landmarks = response.landmark_annotations
# Show the uploaded image and map side-by-side
st.write('-------------------')
st.subheader('📤 Uploaded image and detected location:')
col1, col2 = st.columns(2)
with col1:
image = Image.open(io.BytesIO(content))
st.image(image, use_column_width=True, caption='')
if landmarks:
with col2:
# Create a map centered on the first detected location using Folium
providers = xyz.flatten()
selection = [
'OpenTopoMap',
'Stamen.Toner',
'Stamen.Terrain',
'Stamen.TerrainBackground',
'Stamen.Watercolor',
'CartoDB.Positron',
'CartoDB.Voyager',
'WaymarkedTrails.hiking',
'WaymarkedTrails.cycling',
'WaymarkedTrails.mtb',
'WaymarkedTrails.slopes',
'WaymarkedTrails.riding',
'WaymarkedTrails.skating',
'OpenRailwayMap'
]
m = folium.Map(
location=[landmarks[0].locations[0].lat_lng.latitude, landmarks[0].locations[0].lat_lng.longitude],
zoom_start=15)
for landmark in landmarks:
# Add a marker to the existing map for each detected location
tooltip = landmark.description
folium.Marker(
location=[landmark.locations[0].lat_lng.latitude, landmark.locations[0].lat_lng.longitude],
tooltip=tooltip).add_to(m)
for tiles_name in selection:
tiles = providers[tiles_name]
folium.TileLayer(
tiles=tiles.build_url(),
attr=tiles.html_attribution,
name=tiles.name,
).add_to(m)
folium.LayerControl().add_to(m)
folium_static(m)
st.write('-------------------')
st.subheader('📍 Location information:')
for landmark in landmarks:
st.write('- **Coordinates**: ' + str(landmark.locations[0].lat_lng.latitude) + ', ' + str(
landmark.locations[0].lat_lng.longitude))
st.write('- **Location**: ' + landmark.description)
st.write('')
st.write('-------------------')
else:
st.write('❌ No landmarks detected.')
st.write('-------------------')
#Perform Logo detection
image= types.Image(content=content)
response= client.logo_detection(image=image)
#Extract logos detected
logos_detected = response.logo_annotations
#Print the detected Logo entities in Image
if logos_detected:
st.subheader('👓 Logos Detected:')
for logo in logos_detected:
st.markdown(f'''- {logo.description}''')
else:
st.write('❌ No Logos Detected.')
st.write('-------------------')
#Perform Objects detection
image = types.Image(content=content)
response=client.object_localization(image=image)
object_annotations = response.localized_object_annotations
# Extract Objects if Detected
if object_annotations:
st.subheader('🧳 Objects Detected:')
annotated_image = cv2.imread(uploaded_file.name)
for object_found in object_annotations:
vertices = [(int(vertex.x * annotated_image.shape[1]), int(vertex.y * annotated_image.shape[0]))
for vertex in object_found.bounding_poly.normalized_vertices]
for i in range(len(vertices)):
cv2.line(annotated_image, vertices[i], vertices[(i + 1) % len(vertices)], color=(0, 255, 0),
thickness=2)
cv2.putText(annotated_image,
f"{object_found.name} ({round(object_found.score * 100, 1)}% Confidence)",
(vertices[0][0], vertices[0][1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
st.image(annotated_image, channels="RGB")
else:
st.write('❌ No Objects Detected.')
st.write('-------------------')
# Perform web detection on the image
image = types.Image(content=content)
response = client.web_detection(image=image)
# Extract the detected web entities and pages
web_entities = response.web_detection.web_entities
pages_with_matching_images = response.web_detection.pages_with_matching_images
visually_similar_images = response.web_detection.visually_similar_images
# Print the detected web entities and pages
if web_entities or pages_with_matching_images or visually_similar_images:
st.subheader('🌐 Detected web entities:')
entity_rows = [entity.description for entity in web_entities if entity.description]
if entity_rows:
st.write(entity_rows)
else:
st.write('❌ No web entities detected.')
st.write('-------------------')
st.subheader('🔗 Pages with matching images:')
page_rows = [page.url for page in pages_with_matching_images]
if page_rows:
st.write(page_rows)
else:
st.write('❌ No pages with matching images found.')
st.write('-------------------')
st.subheader('🖼️ Visually similar images:')
similar_images = [image for image in visually_similar_images if image.url]
num_images = len(similar_images)
if num_images > 0:
cols = st.columns(3)
for i, image in enumerate(similar_images):
if i % 3 == 0:
cols = st.columns(3)
with cols[i % 3]:
st.image(image.url, use_column_width=True, caption=image.url)
else:
st.write('❌ No visually similar images found.')
#ChatGPT:
else:
st.write('❌ No web entities detected.')
else:
st.write('📁 Please upload an image.')
config_slot.empty()
except json.JSONDecodeError as e:
st.error("Invalid JSON syntax in config file: {}".format(e))
except Exception as e:
st.error("Error while loading config file: {}".format(e))
config_slot.empty()
else:
st.warning('Please upload a config file.')