Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/Unmanned_car_security/drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def __init__(self, main):
self.screen = main.game.screen
self.font = pygame.font.SysFont('Arial', 32)
self.small_font = pygame.font.SysFont('Arial', 20)
self.warning_font = pygame.font.SysFont('Arial', 24, bold=True)

# 摄像头图像存储
self.camera_image = None

def display_speed(self, speed):
"""显示速度在屏幕左上角"""
Expand Down Expand Up @@ -54,10 +58,47 @@ def display_warning(self, warning_message, color, warning_level):
fill_rect = pygame.Rect(indicator_x, indicator_y, fill_width, indicator_height)
pygame.draw.rect(self.screen, color, fill_rect, border_radius=5)

def display_camera(self):
"""显示摄像头图像 - 新增功能"""
if self.camera_image is not None:
try:
# 摄像头图像显示在右上角
cam_x = self.screen.get_width() - 420 # 右边距20,宽度400
cam_y = 20 # 上边距

# 确保图像是有效的numpy数组
if self.camera_image.shape[0] > 0 and self.camera_image.shape[1] > 0:
# 将numpy数组转换为Pygame表面
# 注意:numpy数组是(height, width, 3)格式,需要转置为(width, height, 3)
image_surface = pygame.surfarray.make_surface(self.camera_image.swapaxes(0, 1))

# 调整图像大小以适应显示区域
target_width = 400
target_height = 300
image_surface = pygame.transform.scale(image_surface, (target_width, target_height))

# 绘制图像
self.screen.blit(image_surface, (cam_x, cam_y))

# 绘制边框和标题
pygame.draw.rect(self.screen, (100, 100, 100),
(cam_x - 2, cam_y - 2, target_width + 4, target_height + 4),
2, border_radius=5)

# 添加摄像头标签
camera_label = self.small_font.render("摄像头视图", True, (255, 255, 255))
self.screen.blit(camera_label, (cam_x + 150, cam_y + target_height + 10))

except Exception as e:
# 如果绘制失败,静默处理(避免影响主程序)
# 可以在调试时取消注释下面的打印
# print(f"绘制摄像头图像失败: {e}")
pass

def draw_camera(self, image_array):
"""绘制摄像头图像(如果需要)"""
# 这里可以添加摄像头图像的绘制逻辑
pass
"""绘制摄像头图像(兼容旧代码)"""
# 将图像存储起来供display_camera使用
self.camera_image = image_array

def draw_lidar(self, point_cloud):
"""绘制激光雷达点云(如果需要)"""
Expand Down
16 changes: 14 additions & 2 deletions src/Unmanned_car_security/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,18 @@ def process_camera(self, image):
# 将CARLA图像转换为numpy数组
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
array = array[:, :, :3]

# 转换格式:BGRA → RGB,并且调整方向
# CARLA默认是BGRA,Pygame需要RGB
array = array[:, :, :3] # 去掉Alpha通道
array = array[:, :, ::-1] # BGR → RGB

# 将图像数据传递给绘制器
if hasattr(self, 'drawer'):
self.drawer.camera_image = array

except Exception as e:
pass
print(f"❌ 处理摄像头数据失败: {e}")

def on_tick(self):
"""每一帧调用的主函数"""
Expand All @@ -224,13 +232,17 @@ def on_tick(self):
# 更新绘制器显示
self.drawer.display_speed(speed_kmh)
self.drawer.display_location(location)

# 显示障碍物警告信息
self.drawer.display_warning(
self.obstacle_detector.warning_message,
self.obstacle_detector.get_warning_color(),
self.obstacle_detector.warning_level
)

# 🆕 新增:显示摄像头图像
self.drawer.display_camera()

# 更新观察者视角跟随车辆
self.update_spectator()

Expand Down
Loading