top of page

Traffic Sign Recognition: Decoding the Streets

Updated: Oct 23, 2023





Introduction

Traffic Sign Recognition (TSR) is a technology that uses computer vision and machine learning techniques to automatically identify and classify traffic signs from images or video streams. It involves detecting and interpreting traffic signs, which are standardized symbols or icons intended to communicate specific messages related to traffic rules, warnings, or navigation instructions to drivers and pedestrians.


Applications of Traffic Sign Recognition
  1. Advanced Driver Assistance Systems (ADAS): Modern vehicles are equipped with ADAS that utilize TSR to warn drivers about upcoming traffic signs or to comply with traffic rules in semi-autonomous driving modes.

  2. Autonomous Vehicles: Self-driving cars rely heavily on TSR systems to navigate roads safely. Recognizing and interpreting traffic signs correctly is crucial for making driving decisions.

  3. Mobile Mapping Systems: Vehicles equipped with imaging systems often use TSR to automatically annotate and update digital maps with the location and type of traffic signs.

  4. Traffic Infrastructure Maintenance: Municipalities and road maintenance agencies can use TSR to identify signs that might be degraded, vandalized, or obscured by foliage, helping prioritize maintenance and replacement efforts.

  5. Traffic Studies and Planning: TSR can be used to automatically inventory and classify traffic signs in a given area, aiding urban planners and traffic engineers in their work.

  6. Driving Simulators and Training: Driving simulators can use TSR technologies to create realistic virtual environments, and in driving training apps to test and train users on traffic sign recognition.

  7. Augmented Reality (AR) Navigation Apps: AR-based navigation apps can overlay traffic sign information onto the live view from a smartphone's camera, enhancing real-time navigation guidance.

  8. Traffic Monitoring and Surveillance: TSR can be used in surveillance systems to monitor compliance with traffic rules and detect violations.

  9. Enhanced GPS Systems: Some GPS devices and apps can alert drivers in real-time about upcoming traffic signs or warnings based on TSR combined with stored map data.

  10. Research and Education: Universities and research institutions use TSR datasets and algorithms to study and improve computer vision and machine learning techniques.


Implementation
class RoadSignDetector:
    """
    A class to detect road signs from images and videos.
    """
    
    SIGNS = ["ERROR",
             "STOP",
             "TURN LEFT",
             "TURN RIGHT",
             "DO NOT TURN LEFT",
             "DO NOT TURN RIGHT",
             "ONE WAY",
             "SPEED LIMIT",
             "OTHER"]
    
    def __init__(self, min_size_components=1000, similitary_contour_with_circle=0.65, file_name=None):
        """
        Initializes the RoadSignDetector with the given parameters.
        
        Args:
        - min_size_components (int): Minimum component size for filtering.
        - similitary_contour_with_circle (float): Similarity threshold for contour matching with a circle.
        - file_name (str): Name of the input file (image or video).
        """
        
    def _clear_cached_images(self):
        """
        Removes any cached PNG images from the current directory.
        """
        
    def apply_contrast_limit(self, image):
        """
        Applies a contrast limit to the given image.
        
        Args:
        - image (array): Input image array.
        
        Returns:
        - array: Processed image.
        """
    def apply_laplacian_of_gaussian(self, image):
        """
        Applies a Laplacian of Gaussian filter to the given image.
        
        Args:
        - image (array): Input image array.
        
        Returns:
        - array: Processed image.
        """
    def binarize_image(self, image):
        """
        Binarizes the given image based on a threshold.
        
        Args:
        - image (array): Input image array.
        
        Returns:
        - array: Binarized image.
        """
    def preprocess(self, image):
        """
        Performs preprocessing operations on the given image.
        
        Args:
        - image (array): Input image array.
        
        Returns:
        - array: Preprocessed image.
        """
         def filter_small_components(self, image, threshold):
        """
        Filters out small components in the image based on the given threshold.

        Args:
        - image (ndarray): The input image.
        - threshold (int): The size threshold below which components will be removed.

        Returns:
        - ndarray: The processed image.
        """
    def detect_contours(self, image):
        """
        Detects contours in the given image.

        Args:
        - image (ndarray): The input image.

        Returns:
        - list: A list of detected contours.
        """
    def is_valid_sign_contour(self, perimeter, centroid, threshold):
        """
        Checks if a contour is a valid sign based on its perimeter and centroid.

        Args:
        - perimeter (float): The contour perimeter.
        - centroid (tuple): The contour centroid.
        - threshold (float): The threshold for validity check.

        Returns:
        - bool: True if the contour is a valid sign, otherwise False.
        """def get_cropped_contour(self, image, center, max_distance):
        """
        Retrieves a cropped contour based on center and maximum distance.

        Args:
        - image (ndarray): The input image.
        - center (tuple): The center of the contour.
        - max_distance (float): The maximum distance from the center.

        Returns:
        - ndarray: The cropped contour.
        """
    def crop_detected_sign(self, image, coordinate):
        """
        Crops the detected sign from the image based on given coordinates.

        Args:
        - image (ndarray): The input image.
        - coordinate (tuple): The coordinates of the sign's top-left corner.

        Returns:
        - ndarray: The cropped sign.
        """
    def identify_largest_sign(self, image, contours, threshold, distance_threshold):
        """
        Identifies the largest traffic sign from the given contours.

        Args:
        - image (ndarray): The input image.
        - contours (list): A list of detected contours.
        - threshold (float): Threshold for contour validity check.
        - distance_threshold (float): Threshold for maximum distance check.

        Returns:
        - ndarray: The largest detected sign.
        """
        
    def identify_all_signs(self, image, contours, threshold, distance_threshold):
        """
        Identifies all valid traffic signs from the given contours.

        Args:
        - image (ndarray): The input image.
        - contours (list): A list of detected contours.
        - threshold (float): Threshold for contour validity check.
        - distance_threshold (float): Threshold for maximum distance check.

        Returns:
        - list: A list of all detected signs.
        """
    def localize_signs(self, image):
        """
        Localizes all traffic signs in the given image.

        Args:
        - image (ndarray): The input image.

        Returns:
        - list: A list of coordinates for all detected signs.
        """def filter_out_lines(self, img):
        """
        Filters out unwanted lines from the image.

        Args:
        - img (ndarray): The input image.

        Returns:
        - ndarray: The processed image.
        """
     def filter_out_unwanted_colors(self, img):
        """
        Filters out unwanted colors from the image.

        Args:
        - img (ndarray): The input image.

        Returns:
        - ndarray: The processed image.
        """
        
     def run(self):
        """
        Main logic for detecting road signs. Processes the file given during initialization.
        """

Overview:

The RoadSignDetector class is designed for detecting road signs from both images and videos.


Components:

Attributes:

SIGNS: A list of predefined road signs. "ERROR" seems to be a default value, possibly used when no match is found.


Methods:

__init__:

  • Purpose: Initializes an instance of the class.

  • Parameters:

    • min_size_components: Specifies a minimum component size for filtering out small components.

    • similarity_contour_with_circle: Sets a threshold for determining if a contour is sufficiently circle-like to be considered a road sign.

    • file_name: If provided, this is the path to an image or video file to be processed.


_clear_cached_images:

  • Purpose: Clears cached images from the current directory, probably used to free up memory or remove temporary files.


apply_contrast_limit:

  • Purpose: Enhances the image contrast to improve visibility of signs.

  • Input: An image.

  • Output: Processed image with enhanced contrast.


apply_laplacian_of_gaussian:

  • Purpose: Applies a Laplacian of Gaussian filter, which can be used to detect edges and improve the clarity of the image.

  • Input: An image.

  • Output: Image after applying the filter.


binarize_image:

  • Purpose: Converts the image to binary format (i.e., black and white) based on a threshold.

  • Input: An image.

  • Output: Binary image.


preprocess:

  • Purpose: Combines various preprocessing steps (like those previously mentioned) on an image to prepare it for contour detection.

  • Input: An image.

  • Output: Preprocessed image.


filter_small_components:

  • Purpose: Removes small components or noise from the image which are smaller than the given threshold.

  • Input: An image and threshold size.

  • Output: Image with small components removed.


detect_contours:

  • Purpose: Identifies contours or shapes in the image.

  • Input: An image.

  • Output: A list of detected contours.


is_valid_sign_contour:

  • Purpose: Checks if a given contour matches the characteristics of a road sign.

  • Input: Perimeter, centroid of a contour, and a validity threshold.

  • Output: Boolean indicating if the contour is likely a road sign.


get_cropped_contour:

  • Purpose: Retrieves a specific region of the image based on the center and a distance value.

  • Input: An image, center coordinates, and max distance.

  • Output: Cropped image containing the contour.


crop_detected_sign:

  • Purpose: Crops out a detected road sign from the original image.

  • Input: An image and the coordinates of the sign.

  • Output: The cropped road sign.


identify_largest_sign and identify_all_signs:

  • Purpose: From the detected contours, these methods respectively identify the largest sign and all valid signs.

  • Input: An image, a list of contours, and thresholds for validity and distance.

  • Output: Image of the largest sign or a list of all detected signs.


localize_signs:

  • Purpose: Determines the locations of all detected road signs in the image.

  • Input: An image.

  • Output: A list of coordinates representing each detected sign's location.


filter_out_lines and filter_out_unwanted_colors:

  • Purpose: Process the image by removing unwanted lines and colors, respectively, to improve detection accuracy.

  • Input: An image.

  • Output: Processed image.


run:

  • Purpose: Represents the main workflow of the class. It will likely call the above methods in sequence to process the provided file and detect road signs.

# Example of how to use the class:
if __name__ == '__main__':
    detector = RoadSignDetector(file_name="sample_video.mp4")
    detector.run()

detector = RoadSignDetector(file_name="sample_video.mp4"):

  • This line creates a new instance of the RoadSignDetector class.

  • We're initializing this instance with a specific video file, "sample_video.mp4". This file is expected to be present in the same directory as the script or the specified path.

  • Once initialized, the detector object now represents our road sign detector, set up to process "sample_video.mp4".

detector.run():

  • With our detector object ready, we call its run method.

  • As previously explained, the run method represents the main workflow of the RoadSignDetector class. When invoked, it will start the process of detecting road signs from the provided video file.

  • This is essentially where all the magic happens. The video will be processed frame by frame, and the methods within the RoadSignDetector class will be used to detect, crop, and possibly classify the road signs found in each frame.


Output


We have predicted a traffic sign with 99 percent accuracy.


We have provided only the code template. For a complete implementation, contact us.




If you require assistance with the implementation of the topic mentioned above, or if you need help with related projects, please don't hesitate to reach out to us.

35 views0 comments

Recent Posts

See All

Comments


bottom of page