TechieYan Technologies

Colour Identification in an Image using Machine learning


Colour Identification through OpenCV is a Simple but very effective Project which involves in identifying colour on a particular thing, this is useful in various applications, especially for the people who are having difficulty in recognizing colours and for the blind people, if we add Text to Speech Library. In order to identify the colours, the model is trained on various coloured images using KNN Classifier which is a popular Machine Learning Algorithm and whenever we pass a new image to the model it will try to predict that particular colour by comparing the colour code within its database which the model is trained and then it displays it on the Screen.

colour identification in an image using machine learning

Code Description & Execution

Algorithm Description

Colour identification api has been used in order to train and test the colour a specific image given. In the colour recognition API, we have colour histogram feature extraction methodology and KNN classifier has been used to train the model. Colour histogram feature extraction is a method of extracting or generating histograms for an image colour channels (blue, green, red) i.e, cv2.calcHist(). K-Nearest Neighborclassifier is basically non-parametric supervised learning algorithm, which can be used for both classification and regression problem. The classification over here is done basically by making cluster of classes.


How to Execute?

Note: Make sure you have checked add to path check boxes at the end of installation process, which solves a major problem of code execution error.

If you are just starting and want to know how to install anaconda refer to this link. or If you already have anaconda and want to check on how to create anaconda environment,

If you already have anaconda and want to check on how to create anaconda environment, set up jupyter notebook refer to this article. You can skip the article if you have knowledge of installing anaconda, setting up environment and installing requirements.txt

  • After installing, python, anaconda and any code editor (Optional).
  • Open anaconda prompt and create an environment, more details of this installing procedure has been provided in the above link.
  • Locate the path where your executable folder is present and type it in anaconda prompt. Eg: cd A:/Projects/project1
  • After creating an environment, activate and install the necessary libraries required for the project, which will be provided in requirements.txt, this can be executed by following command. (pip install -r requirements.txt), If the requirement.txt gives any trouble you can downlaod each module seperately. Example; pip install “module_name”, pip install pandas
  • If the file you want to run is .py you can directly run it via anaconda prompt by with the image to detect the colour on. example; python 
  • Run to use the primary camera to detect the colours.

Data Description

So, we have used multiple combination of plain coloured images with 7 basic days to day used colours. These images are fed to color_histogram_extraction file where each image is converted into a specific RGB format along with its colour class (0->Red, 1->Blue,). These are then used to train our KNN classifier model. whenever we try to predict any colour of an image, we need to pass the whole image, or we can test it out by converting that image into rgb format and passing it to KNN classifier to predict the colour.

Issues you may face while executing the code

  1. We might face an issue while installing specific libraries.
  2. Make sure you have the latest version of python, since sometimes it might cause version mismatch.
  3. Adding path to environment variables in order to run python files and anaconda environment in code editor, specifically in visual studio code.
  4. Make sure to change the path where your executable file/folder is saved.


Colour Identification in an Image using Machine learning
NOTE : Credits to the owner of the Research Paper for giving reference to the project. 
Click here to download the code and associated files.
+91 7075575787