AI-Driven Traffic Lights: Reinforcement Learning and YOLOv5 for Smarter Roads | by Keith Chadberc Niven Villanueva | Nov, 2024


1. Video Feed for Object Detection

The system begins by capturing video from site visitors cameras monitoring intersections. These video feeds are processed by YOLOv5 to detect autos in real-time. Every body is analyzed, and the variety of autos on the intersection is counted.

# Course of video frame-by-frame
import cv2
cap = cv2.VideoCapture('traffic_video.mp4')
whereas True:
ret, body = cap.learn()
if not ret:
break
outcomes = mannequin(body) # Carry out YOLOv5 inference
vehicle_count = len(outcomes.xyxy[0]) # Rely detected autos

The way it works:

  • The video is break up into frames.
  • YOLOv5 processes every body and detects autos.
  • The automobile rely is handed to the RL agent, which determines the optimum motion (hold the sunshine inexperienced, flip it purple, and so forth.).

2. RL Atmosphere for Visitors Management

The RL setting simulates an intersection with site visitors lights, the place the RL agent takes actions (altering lights) primarily based on automobile counts offered by YOLOv5. The RL agent receives rewards primarily based on how properly it manages site visitors, encouraging it to attenuate ready occasions and keep away from pointless mild adjustments.

RL setting particulars:

  • State: The variety of autos ready at all sides of the intersection.
  • Motion: Change the site visitors mild from purple to inexperienced or vice versa.
  • Reward: Optimistic reward for lowering wait occasions, unfavourable reward for inflicting site visitors jams.

3. Movement of Knowledge and Determination-Making

  • The RL agent receives the variety of detected autos from YOLOv5.
  • Based mostly on the site visitors density, it determines whether or not the sunshine ought to stay inexperienced or change to purple.
  • A timer is included to make sure no mild stays inexperienced or purple for too lengthy.
  • The choice is rendered in actual time, and the site visitors lights are managed primarily based on this suggestions.
# Show the present mild standing on the body
cv2.putText(frame_with_detections, f"Mild: {current_light}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 255, 0) if current_light == "GREEN" else (0, 0, 255), 2)
cv2.imshow('Visitors Mild Management', frame_with_detections)

Abstract
Integrating YOLOv5 and RL in site visitors administration programs gives a robust resolution for dynamic site visitors mild adjustment, lowering congestion, and enhancing city transport effectivity.

Future Instructions

  • Enhanced Coaching: Increasing site visitors situations to additional enhance RL mannequin coaching.
  • Multi-Intersection Management: Extending the system to handle a number of intersections.
  • Lengthy-Time period Knowledge Evaluation: Leveraging knowledge over time to boost site visitors predictions and management methods.

Thank you for being a valued member of the Nirantara family! We appreciate your continued support and trust in our apps.

If you haven’t already, we encourage you to download and experience these fantastic apps. Stay connected, informed, stylish, and explore amazing travel offers with the Nirantara family!

Source link



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *