-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_control.py
211 lines (178 loc) · 8.05 KB
/
car_control.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
import logging
from llama_index.core.tools import FunctionTool
from pydantic import BaseModel, Field
from llm_utils import load_and_initialize_llm
from llama_index.core.agent import ReActAgent
import base64
import random
from llama_index.multi_modal_llms.openai import OpenAIMultiModal
from llama_index.core.multi_modal_llms.generic_utils import load_image_urls
from PIL import Image
from llama_index.core.schema import ImageDocument
import requests
import time
from io import BytesIO
import matplotlib.pyplot as plt
import cv2 # OpenCV for capturing images from the camera
# from lib.vision_service import VisionService
# from lib import edge_tts_playback
from lib.edge_tts_playback import playTTS # Import the playTTS function
from lib.car_controller import send_car_action # Import the playTTS function
from lib.vision_service import vision # Import the playTTS function
import config
from llama_index.core.storage.chat_store import SimpleChatStore
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.tools.bing_search import BingSearchToolSpec
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
MOCK_TURN_CAR = False
MOCK_CAPTURE_IMAGE = True
MOCK_DESCRIBE_IMAGES = False
class CarController:
def __init__(self, use_mock=True):
self.USE_MOCK = use_mock
self.llm = load_and_initialize_llm()
self.voice = config.settings.edge_tts_voice_cn
self.turn_car_fn = self.mock_turn_car if MOCK_TURN_CAR else self.turn_car
self.capture_image_fn = self.mock_capture_image if MOCK_CAPTURE_IMAGE else self.capture_image
self.describe_images_fn = self.mock_describe_images if MOCK_DESCRIBE_IMAGES else self.describe_images
self.vision = vision
def playTTS(self, text: str, voice: str) -> None:
"""Play text-to-speech using the specified voice."""
try:
logger.info("Playing TTS for text: %s", text)
playTTS(text, voice)
except Exception as e:
logger.error("Failed to play TTS: %s", e)
def turn_car(self, direction: str, duration: int) -> str:
"""Turn the car in the specified direction("left", "right", "up", "down") for a given duration(ms)."""
if direction not in ["left", "right", "up", "down"]:
logger.error("Invalid direction: %s", direction)
return "Invalid direction. Please specify 'left', 'right', 'up', or 'down'."
logger.info("Turning car %s for %d seconds", direction, duration)
result=send_car_action(direction)
return f"Car turned {direction} for {duration} seconds. Result: {'Success' if result else 'Failure'}"
def mock_turn_car(self, direction: str, duration: int) -> str:
"""Mock turn the car in the specified direction for a given duration."""
logger.info("Mock: Turning car %s for %d seconds", direction, duration)
return f"Mock: Car would turn {direction} for {duration} seconds."
def capture_image(self) -> str:
"""Capture an image from the car's camera."""
cap = cv2.VideoCapture(0) # Assuming the camera is at index 0
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
if not cap.isOpened():
logger.error("Failed to open camera")
return "Failed to open camera"
time.sleep(3)
ret, frame = cap.read()
if not ret:
logger.error("Failed to read frame from camera")
cap.release()
return "Failed to read frame from camera"
if frame is None or frame.size == 0:
logger.error("Captured frame is empty")
cap.release()
return "Captured frame is empty"
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image_path = "captured_image.jpg"
cv2.imwrite(image_path, frame_rgb)
cap.release()
return image_path
def mock_capture_image(self) -> str:
"""Mock capture an image from the car's camera."""
logger.info("Mock: Capturing image from camera")
return "captured_image.jpg"
def describe_images(self, image_path):
"""Describe images using the multi-modal LLM."""
image_document = ImageDocument(image_path=image_path)
try:
response = self.llm.complete(
prompt="Describe the images as an alternative text",
image_documents=[image_document],
)
except Exception as e:
logger.error("Failed to complete LLM request: %s", str(e))
response = "Error: Unable to describe images."
return response
def mock_describe_images(self, image_path):
"""Mock describe images."""
logger.info("Mock: Describing images")
scenes = [
"A sunny beach with palm trees",
"A bustling cityscape at night",
"A serene mountain landscape",
"A dense forest with a flowing river",
"A quiet village covered in snow",
"A vibrant desert with sand dunes",
"A peaceful countryside with rolling hills",
"A futuristic city with flying cars",
"A historical castle on a hill",
"A tropical island with crystal clear water"
]
return f"Mock description of the images: {random.choice(scenes)}."
def display_image(self, image_path):
"""Display an image using matplotlib."""
img = Image.open(image_path)
plt.imshow(img)
plt.show()
# define prompt viewing function
def display_prompt_dict(prompts_dict):
for k, p in prompts_dict.items():
text_md = f"**Prompt Key**: {k}\n**Text:** {p.get_template()}"
logger.info(f"Key: {k}, Prompt: {p}, Markdown: {text_md}")
def car_init(self):
turn_car_tool = FunctionTool.from_defaults(
self.turn_car_fn,
name="TurnCarTool",
description="A tool to turn the car in specified direction for a given duration."
)
capture_image_tool = FunctionTool.from_defaults(
self.capture_image_fn,
name="CaptureImageTool",
description="A tool to capture an image from the car's camera."
)
describe_image_tool = FunctionTool.from_defaults(
self.describe_images_fn,
name="DescribeImageTool",
description="A tool to describe images captured by the car's camera."
)
tool_spec = BingSearchToolSpec(api_key="your-key")
tool_spec_tool_list = tool_spec.to_tool_list()
tools = [turn_car_tool, capture_image_tool, describe_image_tool]
tools.extend(tool_spec_tool_list)
from llama_index.tools.weather import OpenWeatherMapToolSpec
weather_tool_spec = OpenWeatherMapToolSpec(key="your-weather-api-key")
weather_tool_list = weather_tool_spec.to_tool_list()
tools.extend(weather_tool_list)
chat_store = SimpleChatStore()
chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)
agent = ReActAgent.from_tools(tools, llm=self.llm, memory=chat_memory, verbose=True)
prompts = agent.get_prompts()
agent.update_prompts(prompts)
return agent
def save_chat_store(self):
"""Save the chat store to disk."""
self.chat_store.persist(persist_path="chat_store.json")
print("Chat history saved.")
def load_chat_store(self):
"""Load the chat store from disk."""
try:
self.chat_store = SimpleChatStore.from_persist_path(persist_path="chat_store.json")
self.chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=self.chat_store,
chat_store_key="user1",
)
print("Chat history loaded.")
except FileNotFoundError:
print("No previous chat history found. Starting fresh.")
if __name__ == "__main__":
logger.info("Main")
# chatbot_interface()
# vision.start()