Autonomous vehicles depends on variety of sensors like(Lidar ,radar,cameras ultrasonic)
AI algorithm process visual data from cameras allowing vehicles to detect the objects the obstacles,pedestrian walk,any of the car is coming from the other side,predicts the person ,traffic signals,road signs also.
Ai combines the multiple sensors and to create correct view of the environment,improving the accuracy and reliability by integrating various signals.
By using SLAM algorithms we can track the vehicles in which area it was there.
HD (High definition )mappings:AI driven systems provides hd maps,to track the routes of roads lanes, traffic
AI decides how the vehicle will behave in various situations, such as merging onto a highway, stopping at a crosswalk, or handling intersections.(Behavioural Planning)
Some autonomous systems use reinforcement learning (a trial-and-error learning approach) in simulations to improve decision-making and prepare for complex real-world situations.(Reinforcement Planning)
Ai algorithm translates their decisions physically to the braking and acceleration systems.
AI enables vehicles to respond immediately to changes, like sudden stops from cars ahead or unexpected obstacles, ensuring safe operation.
pip install torch torchvision
pip install opencv-python
pip install -r requirements.txt
Download YOLOv5 Pre-trained Weights: We’ll use a pre-trained YOLOv5 model on the COCO dataset, which contains classes like cars, trucks, and people.
python detect.py –weights yolov5s.pt –source 0
Here’s the Python code to run object detection on a video file or live webcam feed.
import torch
import cv2
from yolov5.models.experimental import attempt_load
from yolov5.utils.torch_utils import select_device
from yolov5.utils.general import non_max_suppression, scale_coords
from yolov5.utils.plots import plot_one_box
# Load YOLOv5 model
model = attempt_load(‘yolov5s.pt’, map_location=’cpu’) # or ‘cuda’ for GPU
model.eval()
device = select_device(‘cpu’) # Choose ‘cpu’ or ‘cuda’ based on availability
# Open video capture (0 for webcam, or use a video file path)
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Prepare frame for YOLOv5
img = torch.from_numpy(frame).to(device)
img = img.permute(2, 0, 1).float() / 255.0
img = img.unsqueeze(0)
# Perform inference
with torch.no_grad():
pred = model(img)[0]
pred = non_max_suppression(pred, 0.25, 0.45, classes=None, agnostic=False)
# Process detections
for i, det in enumerate(pred):
if len(det):
# Rescale boxes from img_size to original frame size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round()
for *xyxy, conf, cls in det:
label = f'{model.names[int(cls)]} {conf:.2f}’
plot_one_box(xyxy, frame, label=label, color=(0, 255, 0), line_thickness=2)
# Display the frame with detections
cv2.imshow(‘YOLOv5 Object Detection’, frame)
# Press ‘q’ to exit
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
cap.release()
cv2.destroyAllWindows()
-pip install torch torchvisionpip install opencv-pythonpip install -r requirements.txt
You’ll need pre-trained weights to perform object detection effectively. The pre-trained weights yolov5s.pt are available from the YOLOv5 repository, and these weights are trained on the COCO dataset, which contains classes such as cars, trucks, and people.
You can test the weights using:
python detect.py –weights yolov5s.pt –source 0
The following code runs YOLOv5 on a video or live webcam feed to detect and display bounding boxes around objects.
Indian Institute of Embedded Systems – IIES