欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > 人工智能应用实例-自动驾驶

人工智能应用实例-自动驾驶

2025/2/12 16:49:59 来源:https://blog.csdn.net/testManger/article/details/145490447  浏览:    关键词:人工智能应用实例-自动驾驶

自动驾驶是一个极其复杂的系统工程,包含环境感知、决策规划、控制执行等多个环节,很难用一个完整的代码示例来呈现整个自动驾驶系统。不过,我们可以通过 Python 结合一些开源库,模拟实现自动驾驶中的部分关键逻辑,比如简单的目标检测与路径规划。

1. 环境准备

在运行代码前,需要安装以下库:

pip install numpy opencv-python scikit-learn

2. 简单目标检测(模拟)

目标检测是自动驾驶中感知环境的重要步骤,下面代码模拟了使用 OpenCV 对图像中的目标进行检测。

import cv2
import numpy as npdef detect_objects(image_path):# 读取图像image = cv2.imread(image_path)if image is None:print("无法读取图像")return# 转换为灰度图像gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 简单的边缘检测(模拟目标检测)edges = cv2.Canny(gray, 50, 150)# 查找轮廓contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 绘制检测到的目标for contour in contours:if cv2.contourArea(contour) > 100:  # 过滤小的轮廓x, y, w, h = cv2.boundingRect(contour)cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)# 显示结果cv2.imshow('Detected Objects', image)cv2.waitKey(0)cv2.destroyAllWindows()# 使用示例
image_path = 'test_image.jpg'
detect_objects(image_path)

3. 简单路径规划(模拟)

路径规划是根据感知到的环境信息,为车辆规划一条安全、高效的行驶路径。以下代码使用 A* 算法模拟简单的路径规划。

import heapq
import numpy as np# 定义地图
grid = np.array([[0, 0, 0, 0],[0, 1, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0]
])# 定义节点类
class Node:def __init__(self, x, y, g=float('inf'), h=float('inf'), parent=None):self.x = xself.y = yself.g = g  # 从起点到当前节点的实际代价self.h = h  # 从当前节点到目标节点的估计代价self.f = g + h  # 总代价self.parent = parentdef __lt__(self, other):return self.f < other.f# 定义 A* 算法
def astar(grid, start, goal):rows, cols = grid.shapeopen_list = []closed_set = set()start_node = Node(start[0], start[1], g=0, h=abs(start[0] - goal[0]) + abs(start[1] - goal[1]))heapq.heappush(open_list, start_node)while open_list:current_node = heapq.heappop(open_list)if (current_node.x, current_node.y) == goal:path = []while current_node:path.append((current_node.x, current_node.y))current_node = current_node.parentreturn path[::-1]closed_set.add((current_node.x, current_node.y))# 定义相邻节点的偏移量neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)]for dx, dy in neighbors:new_x, new_y = current_node.x + dx, current_node.y + dyif 0 <= new_x < rows and 0 <= new_y < cols and grid[new_x, new_y] == 0 and (new_x, new_y) not in closed_set:new_g = current_node.g + 1new_h = abs(new_x - goal[0]) + abs(new_y - goal[1])new_node = Node(new_x, new_y, g=new_g, h=new_h, parent=current_node)# 检查是否已经在开放列表中found = Falsefor i, node in enumerate(open_list):if node.x == new_x and node.y == new_y:if new_g < node.g:open_list[i] = new_nodeheapq.heapify(open_list)found = Truebreakif not found:heapq.heappush(open_list, new_node)return None# 使用示例
start = (0, 0)
goal = (3, 3)
path = astar(grid, start, goal)
print("规划路径:", path)

代码解释

  • 目标检测

    • 使用 OpenCV 读取图像并将其转换为灰度图像。
    • 通过 Canny 边缘检测算法检测图像中的边缘。
    • 查找边缘的轮廓,并过滤掉面积较小的轮廓。
    • 在原始图像上绘制检测到的目标的边界框。
  • 路径规划

    • 定义了一个简单的二维地图,其中 0 表示可通行区域,1 表示障碍物。
    • 实现了 A* 算法,用于在地图上寻找从起点到目标点的最短路径。
    • 使用优先队列(堆)来管理开放列表,以提高搜索效率。

这些代码只是自动驾驶系统的简化模拟,实际的自动驾驶系统需要更复杂的传感器数据处理、精确的目标检测算法和更高级的路径规划策略。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com