-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (86 loc) · 3.03 KB
/
Copy pathmain.py
File metadata and controls
106 lines (86 loc) · 3.03 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# system imports
import re
import json
# 3rd party imports
import webbrowser
# local imports
from ai import AIClient
from voice import speak, listen_for_command
from solution_map import create_map
from image import take_screenshot, encode_bytes
from audio import play_sound
aiClient = AIClient()
def main_loop():
imgs = []
while True:
(res, command) = listen_for_command()
if res != 0:
continue
if "take photo" in command:
play_sound("wakeup.mp3")
img_bytes = take_screenshot()
imgs.append(encode_bytes(img_bytes))
play_sound("stop.mp3")
speak(f"Saved photo...")
elif "location" in command:
if not imgs:
speak("No screenshots to analyze.")
continue
play_sound("wakeup.mp3")
# Send to location identification service
result = aiClient.call_location_api(imgs)
print(result)
if result:
location_str, lat, lon = parse_location_data(extract_json_from_markdown(result))
if location_str:
speak(f"Most probable location is {location_str}.")
if lat and lon:
speak("Opening the map.")
filepath = "solution_map.html"
create_map(filepath, lat, lon)
webbrowser.open_new_tab(filepath)
else:
speak("No location found in the image.")
imgs = []
if "exit" in command or "quit" in command:
speak("Goodbye!")
break
def extract_json_from_markdown(markdown_str):
"""
Extracts JSON string from a Markdown code block.
Args:
markdown_str (str): String containing Markdown code block with JSON.
Returns:
str: Cleaned JSON string without Markdown delimiters.
"""
# Use regex to extract content between ```json and ```
json_pattern = re.compile(r"```json\s*(\{.*?\})\s*```", re.DOTALL | re.IGNORECASE)
match = json_pattern.search(markdown_str)
if match:
return match.group(1)
else:
# If no code block found, assume the entire string is JSON
return markdown_str.strip()
def parse_location_data(json_str):
"""
Parses the JSON string to extract location and coordinates.
Args:
json_str (str): JSON-formatted string containing location data.
Returns:
tuple: (location (str), latitude (float), longitude (float))
"""
try:
# Parse the JSON string into a Python dictionary
data = json.loads(json_str)
# Extract the location
location = data.get('location')
# Extract coordinates
coordinates = data.get('coordinates', {})
latitude = coordinates.get('lat')
longitude = coordinates.get('lon')
return location, latitude, longitude
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
return None, None, None
if __name__ == "__main__":
main_loop()