FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
in AI-ML-Data Science Projects by (115 points)

Color Detection using CNN & Python

1 Answer

0 like 0 dislike
by (115 points)
selected by
Best answer

Color Detection is the process of detecting name of any color. Simple isn't? Well for humans it's an extremely easy task but for computers it's not that straightforward. Human eyes and brain work together to translate light into color. Light receptors that are present in our eyes transmit the signal to the brain. Our brain than recognizes the color. We'll be using somewhat same strategy to detect color in computers.

In this project we built an application through which we can automatically get the name of the color by clicking on them. For that we need a dataset that contains the color name and its values.

We represent colors on a computers by color-space or color models which basically describes range of colors as tuples of numbers.

Instead of going for each color, we’ll discuss most common color-space we use .i.e. RGB(Red, Green, Blue) and HSV (Hue, Saturation, Value).

RGB basically describes color as a tuple of three components. Each component can take a value between 0 and 255, where the tuple (0, 0, 0) represents black and (255, 255, 255) represents white. For example, if we were to show a pure blue pixel on-screen, then the R value would be 0, the G value would be 0, and the B value would be 255.

Steps for Building the Project– Color Detection

Here are the steps to build an application in Python that can detect colors:

1. Download and unzip the zip file for dataset.

Color Detection Zip File

2. Importing Libraries and taking an image from the user

In below line of code, first two lines handle all the imports. In third line, I’m importing imutils module, which helps in resizing images and finding the range of colors. In line 4 we’ve open the image.

import cv2
import numpy as np
import imutils
img = cv2.imread('color2.jpg')

3. Next, we read the CSV file with pandas

The pandas library is very useful when we need to perform various operations on data files like CSV. pd.read_csv() reads the CSV file and loads it into the pandas DataFrame. We have assigned each column with a name for easy accessing. We have converted the image to an hsv image because hsv helps to differentiate intensity from color.

#Reading csv file with pandas and giving names to each column


csv = pd.read_csv('colors.csv', names=index, header=None)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

4. Set a mouse callback event on a window

First, we created a window in which the input image will display. Then, we set a callback function which will be called when a mouse event happens.



5. Create the draw_function

It will calculate the rgb values of the pixel which we double click. The function parameters have the event name, (x,y) coordinates of the mouse position, etc. In the function, we check if the event is double-clicked then we calculate and set the r,g,b values along with x,y positions of the mouse.

def draw_function(event, x,y,flags,param):

if event == cv2.EVENT_LBUTTONDBLCLK:

global b,g,r,xpos,ypos, clicked

clicked = True

xpos = x

ypos = y

b,g,r = img[y,x]

b = int(b)

g = int(g)

r = int(r)

6. Calculate distance to get color name

We have the r,g and b values. Now, we need another function which will return us the color name from RGB values. To get the color name, we calculate a distance(d) which tells us how close we are to color and choose the one having minimum distance.

Our distance is calculated by this formula:

d = abs(Red – ithRedColor) + (Green – ithGreenColor) + (Blue – ithBlueColor)


def getColorName(R,G,B):        

       minimum = 10000

       for i in range(len(csv)):

            d = abs(R- int(csv.loc[i,"R"])) + abs(G- int(csv.loc[i,"G"]))+ abs(B- int(csv.loc[i,"B"]))


                 minimum = d

                 cname = csv.loc[i,"color_name"]

                 return cname

7. Display image on the window

Whenever a double click event occurs, it will update the color name and RGB values on the window.

Using the cv2.imshow() function, we draw the image on the window. When the user double clicks the window, we draw a rectangle and get the color name to draw text on the window using cv2.rectangle and cv2.putText() functions.

8. Run Python File

The beginner Python project is now complete, you can run the Python file from the command prompt. Make sure to give an image path using ‘-i’ argument. If the image is in another directory, then you need to give full path of the image:

python -i <add your image path here>



In this Python project with source code, we learned about colors and how we can extract color RGB values and the color name of a pixel


The color detection project is developed by Jagrati from Maharani Girls Engineering College, Jaipur. This project is developed during Goeduhub online summer training in Artificial Intelligence, Machine Learning and Deep Learning.

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs


About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © Social::   |  | 


Free Online Directory