{"id":15718983,"url":"https://github.com/shashankkumbhare/traffic-light-classifier","last_synced_at":"2026-05-02T19:36:50.328Z","repository":{"id":61200400,"uuid":"541288169","full_name":"ShashankKumbhare/traffic-light-classifier","owner":"ShashankKumbhare","description":"A computer vision project to classify traffic light signals as either red, yellow or green.","archived":false,"fork":false,"pushed_at":"2022-11-08T16:20:53.000Z","size":122169,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-09T01:04:53.581Z","etag":null,"topics":["autonomous-car","autonomous-vehicles","computer-vision","deep-learning","machine-learning","opencv","opencv-python","self-driving-cars"],"latest_commit_sha":null,"homepage":"https://github.com/ShashankKumbhare/traffic-light-classifier","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShashankKumbhare.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-25T19:35:34.000Z","updated_at":"2022-11-09T00:14:12.000Z","dependencies_parsed_at":"2023-01-21T16:04:03.151Z","dependency_job_id":null,"html_url":"https://github.com/ShashankKumbhare/traffic-light-classifier","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShashankKumbhare%2Ftraffic-light-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShashankKumbhare%2Ftraffic-light-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShashankKumbhare%2Ftraffic-light-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShashankKumbhare%2Ftraffic-light-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShashankKumbhare","download_url":"https://codeload.github.com/ShashankKumbhare/traffic-light-classifier/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246389877,"owners_count":20769459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autonomous-car","autonomous-vehicles","computer-vision","deep-learning","machine-learning","opencv","opencv-python","self-driving-cars"],"created_at":"2024-10-03T21:54:27.604Z","updated_at":"2026-05-02T19:36:50.292Z","avatar_url":"https://github.com/ShashankKumbhare.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Traffic Light Classification\n\n### A computer vision project to classify traffic light signals as either red, yellow or green.\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/ShashankKumbhare/traffic-light-classifier/main/auxil/images/all_lights.png\"  width=\"400\"\u003e\u003c/p\u003e\n\n## Table of Contents\n\n- [**Project Overview**](#Project-Overview)\n- [**Data Description**](#Data-Description)\n- [**Methodology**](#Methodology)\n- [**Results**](#Results)\n- [**Python package `traffic_light_classifier`**](#python-package-traffic_light_classifier)\n- [**Package Usage**](#Package-Usage)\n\n---\n\n## Project Overview\n\n- Traffic light classification plays an important role in Advanced Driver Assist as well as self-driving vehicle systems which ensures timely and appropriate reaction to traffic lights in cross sections.  \n- In this project, a robust probabilistic approach based classifier has been implemented from scratch to classify traffic light signal's status using computer vision and machine learning techniques.\n- Several data cleaning steps, features extraction and a probabilistic metric has been utilized.\n- The classifier has been validated on a testing dataset with a **accuracy of 99.66 %**.\n- All training stages and prediction stages has been throughly visualized \u0026 analyzed and thus improvised.\n- The methodology utilized in this project can be generalized and applied to many other computer vision applications.\n- The project presentation notebook is [Notebook Traffic_Light_Classifier](https://github.com/ShashankKumbhare/traffic-light-classifier/blob/main/Traffic_Light_Classifier.ipynb).\n- The implemented Python package code is [traffic_light_classifier](https://github.com/ShashankKumbhare/traffic-light-classifier/tree/main/traffic_light_classifier).\n\n---\n\n## Data Description\n\nThis traffic light dataset consists of 1484 number of color images in 3 categories - red, yellow, and green. As with most human-sourced data, the data is not evenly distributed among the types. There are:\n\n- 904 red traffic light images\n- 536 green traffic light images\n- 44 yellow traffic light images\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/ShashankKumbhare/traffic-light-classifier/main/auxil/images/data_examples.png\"  width=\"600\"\u003e\u003c/p\u003e\n\nNote: All images come from this [MIT self-driving car course](https://selfdrivingcars.mit.edu/) and are licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).\n\n- Each image is a numpy array of shape (n_row, n_col, 3) i.e each image has a 3 channels (RGB - red, green, blue color space) where n_col and n_row is the height and the width of the image.\n- Each image gives information about its colors of the pixels and their location.\n- The RGB color space of the images can be converted to HSV color space channels (hue, saturation, value) i.e. the information about the hue, saturation and brightness of the pixels of the images.\n\n---\n\n## Methodology\n\n### Feature extraction from the dataset\nThe features are in the form of either rgb values or hsv values of the pixels of the image and their locations.\n- Some feature extracted:\n  - Average hsv image for red images, yellow images \u0026 green images.\n  - 1D range of high saturation along width and along height for each image.\n  - 1D range of high brightness along width and along height for each image..\n  - 2D Region of high saturation for each image.\n  - 2D Region of high brightness for each image.\n  - Location of light for each image.\n  - Extraction of average red light hues, average yellow light hues, average green light hues from each image.\n  - Average s (saturation) of light located.\n  - Average v (bright) of light located.\n  - etc.\n\n### The classifier's training stages:\n1. Get average image for red, yellow and green training images.\n2. Get region of high saturation in average red, yellow \u0026 green images to narrow down the search window for individual images to look for the red, yellow, and green lights.\n3. Crop all training images at their respective color's average image's high saturation region.\n4. Locate lights in all images by using high saturation and high brightness regions.\n5. Crop images at their respective light's position\n6. Get average image of red light, yellow light \u0026 average green light to see the distribution of hue, saturation and brightness in red lights, yeellow light, and green lights.\n7. Get distribtution of hues, saturations and brightnesses of red lights, average yellow lights \u0026 green lights.\n8. Optimize classifier's metric's parameters for red, yellow and green lights.\n9. Predict \u0026 get accuracy of training dataset for classifier's optimized parameters.\n\n### Probabilities calculations and prediction.\n\n#### \u003cspan style=\"color:skyblue\"\u003e Hues extraction \u003c/span\u003e\n\nFor a single image, the classifier extracts the following hues from the 3 regions:\n1. Model's red    hues from the light located in model's red    light region.\n2. Model's yellow hues from the light located in model's yellow light region.\n3. Model's green  hues from the light located in model's green  light region.\n\n#### \u003cspan style=\"color:red\"\u003e Probabilistic model \u003c/span\u003e\n\nThis classifier classifies an input image either red, yellow or green based on *probabilities*.\n\nFor a single input image, 3 lights will be located in the 3 regions (i.e. red, yellow and green light regions). Then the classifier calculates **3 probabilities**:\n1. Probability of image being red\n2. Probability of image being yellow\n3. Probability of image being green\n\nAnd propobilities are calculated by,\n- $Probability\\ of\\ image\\ being\\ red    = \\dfrac {strength_{red}}    {strength_{red} + strength_{yellow} + strength_{green}}$\n- $Probability\\ of\\ image\\ being\\ yellow = \\dfrac {strength_{yellow}} {strength_{red} + strength_{yellow} + strength_{green}}$\n- $Probability\\ of\\ image\\ being\\ green  = \\dfrac {strength_{green}}  {strength_{red} + strength_{yellow} + strength_{green}}$\n\n  where,  \n  - $strength_{red}    = \\mu_{saturation_{red}}^a    \\times \\mu_{brightness_{red}}^b$\n  - $strength_{yellow} = \\mu_{saturation_{yellow}}^a \\times \\mu_{brightness_{yellow}}^b$\n  - $strength_{green}  = \\mu_{saturation_{green}}^a  \\times \\mu_{brightness_{green}}^b$\n  \n  and,  \n    - $\\mu_{saturation_{red}}$   : mean saturation of the light located in model's red    light region\n    - $\\mu_{brightness_{red}}$   : mean brightness of the light located in model's red    light region\n    - $\\mu_{saturation_{yellow}}$: mean saturation of the light located in model's yellow light region\n    - $\\mu_{brightness_{yellow}}$: mean brightness of the light located in model's yellow light region\n    - $\\mu_{saturation_{green}}$ : mean saturation of the light located in model's green  light region\n    - $\\mu_{brightness_{green}}$ : mean brightness of the light located in model's green  light region\n    - $a$ \u0026 $b$     : model's parameters\n\nDetailed analysis and visualization of each stage has been given in [Notebook Traffic_Light_Classifier](https://github.com/ShashankKumbhare/traffic-light-classifier/blob/main/Traffic_Light_Classifier.ipynb).\n\n---\n\n## Results\n\n- A custom made Python package `traffic_light_classifier` has been implemented for this project.\n- All training stages and prediction stages has been throughly visualized \u0026 analyzed and thus improvised.\n- The classifier has been validated on a testing dataset with a accuracy of 99.66 %.\n- The project results and package usage have been clearly demonstrated in the [Notebook Traffic_Light_Classifier](https://github.com/ShashankKumbhare/traffic-light-classifier/blob/main/Traffic_Light_Classifier.ipynb).\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/ShashankKumbhare/traffic-light-classifier/main/auxil/images/analysis_ex.png\"  width=\"600\"\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/ShashankKumbhare/traffic-light-classifier/main/auxil/images/predict_ex.png\"  width=\"400\"\u003e\u003c/p\u003e\n\n---\n\n## Python package `traffic_light_classifier`\n\n- This project utilizes a custom-made package `traffic_light_classifier` which contains a classifier, plotting \u0026 feature extraction functionalities, and datasets for the project.\n- Libraries used: `OpenCV-Python`, `scipy`, `matplotlib`, `numpy`.\n- This library offers tools which enables to analyze and visualize the entire training and prediction process stages.\n\n---\n\n### Installation\n\n``` python \n# Install package from PyPI \u003e\u003e\n!pip install traffic_light_classifier\n# or\n# Install package from GitHub \u003e\u003e\n!pip install git+https://github.com/ShashankKumbhare/traffic-light-classifier.git#egg=traffic-light-classifier\n```\n\n---\n\n## Package Usage\n\n#### Import package `traffic_light_classifier`\n``` python \n# Import package `traffic_light_classifier` \u003e\u003e\nimport traffic_light_classifier as tlc\n```\n\n#### Create an instance of class Model\n``` python\n# Create an instance of class Model provided in the package\nmodel = tlc.Model()\n```\n\n#### Train model\n``` python\n# Call `compile` method of model object to train the model\n# Note: Use parameter `show_analysis = True` to see the detailed process of the training/compiling stages.\nmodel.compile()\nmodel.compile(show_analysis = True)\n```\n\n#### Predict image\n``` python\n# Get a random red image from the test dataset provided in the package\nimport random\nimage_red = random.choice( tlc.datasets.train.images_std.red )\ntlc.plots.plot_images(image_red)\n# Note: Use parameter `show_probabilities = True` to see the classification probabilities.\n#       Use parameter `show_analysis = True` to see the detailed process of the prediction stages.\nlabel_predicted = model.predict( image_red )\nlabel_predicted = model.predict( image_red, show_probabilities = True )\nlabel_predicted = model.predict( image_red, show_analysis = True )\n\n# For yellow and green test images\nimage_yellow = random.choice( tlc.datasets.train.images_std.yellow )\nimage_green  = random.choice( tlc.datasets.train.images_std.green )\nlabel_predicted = model.predict( image_yellow, show_analysis = True )\nlabel_predicted = model.predict( image_green,  show_analysis = True )\n```\n\n#### Predicting an entire dataset\n``` python \n# Use `predict_dataset()` method to predict an entire dataset \u003e\u003e\nimages_std = tlc.datasets.test.images_std.all\nlabels_std = tlc.datasets.test.labels_std    # optional\naccuracy   = model.predict_dataset(images_std, labels_std)\nprint(accuracy)\n```\n\n#### Analyzing \u0026 visualizing compilation/training process\nAn ardent user might want to see what is happening in the compiling/training process. He might also want to revisit or play with them.\n``` python \n# After compilation/training, all the compilation stages are stored in `model.compilation` attribute \u003e\u003e\n# To access them:\n\n# Compilation-Stage 1: Average image for red, yellow and green training images\nimage1 = model.compilation.stg1_image_avg.red\nimage2 = model.compilation.stg1_image_avg.yellow\nimage3 = model.compilation.stg1_image_avg.green\ntlc.plots.plot_images([image1, image2, image3])\ntlc.plots.plot_channels(image1, \"hsv\")\ntlc.plots.plot_channels(image2, \"hsv\")\ntlc.plots.plot_channels(image3, \"hsv\")\n\n# Compilation-Stage 2a: Region of high saturation in average red, yellow \u0026 green images\nprint(model.compilation.stg2a_region_high_s.red)\nprint(model.compilation.stg2a_region_high_s.yellow)\nprint(model.compilation.stg2a_region_high_s.green)\n\n# Compilation-Stage 2b: Masked average images at their respective high saturation region\nimage1 = model.compilation.stg2b_image_avg_masked_region_high_s.red\nimage2 = model.compilation.stg2b_image_avg_masked_region_high_s.yellow\nimage3 = model.compilation.stg2b_image_avg_masked_region_high_s.green\ntlc.plots.plot_images([image1, image2, image3])\ntlc.plots.plot_channels(image1, \"hsv\")\ntlc.plots.plot_channels(image2, \"hsv\")\ntlc.plots.plot_channels(image3, \"hsv\")\n\n# Compilation-Stage 3: Cropped average images at high saturation region\nimages1 = model.compilation.stg3_dataset_images_cropped_high_s_region.red[0:10]\nimages2 = model.compilation.stg3_dataset_images_cropped_high_s_region.yellow[0:10]\nimages3 = model.compilation.stg3_dataset_images_cropped_high_s_region.green[0:10]\ntlc.plots.plot_images(images1)\ntlc.plots.plot_images(images2)\ntlc.plots.plot_images(images3)\n\n# Compilation-Stage 4: Locations of lights in all training images\nprint(model.compilation.stg4_locations_light.red[0:5])\nprint(model.compilation.stg4_locations_light.yellow[0:5])\nprint(model.compilation.stg4_locations_light.green[0:5])\n\n# Compilation-Stage 5: Cropped images at their respective light's position\nimages1 = model.compilation.stg5_dataset_images_light.red[0:10]\nimages2 = model.compilation.stg5_dataset_images_light.yellow[0:10]\nimages3 = model.compilation.stg5_dataset_images_light.green[0:10]\ntlc.plots.plot_images(images1)\ntlc.plots.plot_images(images2)\ntlc.plots.plot_images(images3)\n\n# Compilation-Stage 6: Average image of red lights, yellow lights \u0026 green lights\nimage1 = model.compilation.stg6_image_light_avg.red\nimage2 = model.compilation.stg6_image_light_avg.yellow\nimage3 = model.compilation.stg6_image_light_avg.green\ntlc.plots.plot_images([image1, image2, image3])\ntlc.plots.plot_channels(image1, \"hsv\")\ntlc.plots.plot_channels(image2, \"hsv\")\ntlc.plots.plot_channels(image3, \"hsv\")\n\n# Compilation-Stage 7: Hues, saturations and brightnesses of average red light, average yellow light \u0026 average green light\nprint(model.compilation.stg7a_hue_avg_light.red.mu)\nprint(model.compilation.stg7a_hue_avg_light.red.sigma)\nprint(model.compilation.stg7a_hue_avg_light.red.dist)\n\nprint(model.compilation.stg7b_sat_avg_light.red.mu)\nprint(model.compilation.stg7b_sat_avg_light.red.sigma)\nprint(model.compilation.stg7b_sat_avg_light.red.dist)\n\nprint(model.compilation.stg7c_brt_avg_light.red.mu)\nprint(model.compilation.stg7c_brt_avg_light.red.sigma)\nprint(model.compilation.stg7c_brt_avg_light.red.dist)\n\n# Compilation-Stage 8: Optimized parameters  a \u0026 b for maximum accuracy\nprint(model.compilation.stg8_params_optimised)\n\n# Compilation-Stage 9: Prediction analysis \u0026 accuracy of training dataset for classifier's optimized parameters\nprint( dir(model.compilation.stg9a_dataset_train_analysis.green[0]) )\nprint( dir(model.compilation.stg9a_dataset_train_analysis.green[0]) )\nprint( dir(model.compilation.stg9b_misclassified.green[0]) )\nprint( f\"Accuracy red     = {model.compilation.stg9c_accuracy_train.red}\" )\nprint( f\"Accuracy yellow  = {model.compilation.stg9c_accuracy_train.yellow}\" )\nprint( f\"Accuracy green   = {model.compilation.stg9c_accuracy_train.yellow}\" )\nprint( f\"Accuracy overall = {model.compilation.stg9c_accuracy_train.all}\" )\n```\n\n#### Analyzing \u0026 visualizing prediction process\nAn ardent user might want to see what is happening behind the prediction process. Analyzing misclassified images might give user the understanding of the flaws of the classifier model and help improve the algorithm.\n``` python \nimport random\nimage_red = random.choice( tlc.datasets.train.images_std.red )\ntlc.plots.plot_images(image_red)\nlabel_predicted = model.predict( image_red )\n# After prediction, all the compilation stages are stored in `model.prediction` attribute \u003e\u003e \n# To access them:\n# Compilation-Stage 1: Croped image at model's optimal high saturation region for red, yellow, green light's position\nimage1 = model.prediction.stg1_image_high_s_region.red\nimage2 = model.prediction.stg1_image_high_s_region.yellow\nimage3 = model.prediction.stg1_image_high_s_region.green\ntlc.plots.plot_images([image1, image2, image3])\ntlc.plots.plot_channels(image1, \"hsv\")\ntlc.plots.plot_channels(image2, \"hsv\")\ntlc.plots.plot_channels(image3, \"hsv\")\n\n# Compilation-Stage 2: Located lights in model's optimal regions of red, yellow, green lights\nmodel.prediction.stg2_locations_light.red\nprint(model.prediction.stg2_locations_light.red)\nprint(model.prediction.stg2_locations_light.yellow)\nprint(model.prediction.stg2_locations_light.green)\n\n# Compilation-Stage 3: Cropped lights\nimage1 = model.prediction.stg3_image_light.red\nimage2 = model.prediction.stg3_image_light.yellow\nimage3 = model.prediction.stg3_image_light.green\ntlc.plots.plot_images([image1, image2, image3])\ntlc.plots.plot_channels(image1, \"hsv\")\ntlc.plots.plot_channels(image2, \"hsv\")\ntlc.plots.plot_channels(image3, \"hsv\")\n\n# Compilation-Stage 4: Extracted model's red, yellow, green light's colors from the respective cropped parts of the input image\nimage1 = model.prediction.stg4_image_colors_extracted.red\nimage2 = model.prediction.stg4_image_colors_extracted.yellow\nimage3 = model.prediction.stg4_image_colors_extracted.green\ntlc.plots.plot_channels(image1, \"hsv\")\ntlc.plots.plot_channels(image2, \"hsv\")\ntlc.plots.plot_channels(image3, \"hsv\")\n\n# Compilation-Stage 5: Distribution of hues extracted from the model's red, yellow \u0026 green light region\nprint(model.prediction.stg5a_hue_input_light.red.mu)\nprint(model.prediction.stg5a_hue_input_light.red.sigma)\nprint(model.prediction.stg5a_hue_input_light.red.dist)\n\nprint(model.prediction.stg5a_hue_input_light.yellow.mu)\nprint(model.prediction.stg5a_hue_input_light.yellow.sigma)\nprint(model.prediction.stg5a_hue_input_light.yellow.dist)\n\nprint(model.prediction.stg5a_hue_input_light.green.mu)\nprint(model.prediction.stg5a_hue_input_light.green.sigma)\nprint(model.prediction.stg5a_hue_input_light.green.dist)\n    # Similarly for saturation and brightness\n\n# Compilation-Stage 6: Probabilities of image being red, yellow and green\nprint( model.prediction.stg6_probabilities.image_being_red )\nprint( model.prediction.stg6_probabilities.image_being_yellow )\nprint( model.prediction.stg6_probabilities.image_being_green )\n\n# Compilation-Stage 7: Predicted label\nprint( model.prediction.stg7_label_predicted )\nprint( model.prediction.stg7_label_predicted_str )\n```\nThe package usage have been clearly demonstrated in the [Notebook Traffic_Light_Classifier](https://github.com/ShashankKumbhare/traffic-light-classifier/blob/main/Traffic_Light_Classifier.ipynb).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshashankkumbhare%2Ftraffic-light-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshashankkumbhare%2Ftraffic-light-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshashankkumbhare%2Ftraffic-light-classifier/lists"}