Color Recognition with Python

Color Recognition with Python

This article will take you through a Color Recognition task with Python. We are going to create a basic application that will help us detect colours in an image. The program will let us return the RGB value, which is useful.

Many graphic designers and web designers will understand how useful RGB values can be. This is a great project to start with Computer Vision

Colour Recognition with Python using OpenCV

Firstly, if You never heard of computer vision, now is the best time to find out. Most of the areas of Machine Learning are closely related to computer vision.

Let’s import the libraries we need for this project to get started. I will use only three libraries for this project. Pandas, Numpy and OpenCV:

import numpy as np
import pandas as pd
import cv2

First, we need to train our model to identify colours. To do this, we need data that includes the names of colours and values. Since most colours can be set using red, green, and blue.

This is why we will be using the RGB formats as data points. I found a ready CSV file. I will give a download link at the end of the article

Now we will create 2 paths one for our test image and another for a CSV file

img_path = 'pic2.jpg'
csv_path = 'colors.csv'

Let us read our image. What I meant is that to use our image for processing first our computer should read our image.

For pc, an image is just an array of RGB values of pixels.

# reading csv file
index = ['color', 'color_name', 'hex', 'R', 'G', 'B']
df = pd.read_csv(csv_path, names=index, header=None)

For our program to work properly, we need some global variables. You will know how global variables can be useful when working with functions:

#declaring global variables
clicked = Falser = g = b = xpos = ypos = 0

Colour Recognition Function

The function below will be called when you will double-click on an area of ​​the image. It will return the name of the colour

and the RGB values ​​for that colour. This is where the magic happens:

#function to calculate minimum distance from all colors and get the most matching color
def get_color_name(R,G,B): 
 minimum = 1000  
for i in range(len(df)):    
d = abs(R - int(df.loc[i,'R'])) + abs(G - int(df.loc[i,'G'])) + abs(B - int(df.loc[i,'B']))
# d is the minimum distance from our all color's and abs function return the absolute value.    
if d <= minimum: 
     minimum = d      
    cname = df.loc[i, 'color_name']  
return cname

Mouse Click Function

The function below is a helper function that is a part of our program which will help in the process of double click:

#function to get x,y coordinates of mouse double click
def draw_function(event, x, y, flags, params):
  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)

Processing with Computer Vision

In this step, we’ll open the image in a new window using the OpenCV method. And in this window, we will use the functions we defined earlier. The application is so simple that it returns the name of the colour and the colour values ​​when you double-click on a certain area of ​​the image:

# creating window
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_function)while True:
  cv2.imshow('image', img)
  if clicked:
    #cv2.rectangle(image, startpoint, endpoint, color, thickness)-1 fills entire rectangle
    cv2.rectangle(img, (20,20), (600,60), (b,g,r), -1)    #Creating text string to display( Color name and RGB values )
    text = get_color_name(r,g,b) + ' R=' + str(r) + ' G=' + str(g) + ' B=' + str(b)
    #cv2.putText(img,text,start,font(0-7),fontScale,color,thickness,lineType )
    cv2.putText(img, text, (50,50), 2,0.8, (255,255,255),2,cv2.LINE_AA)    #For very light colours we will display text in black colour
      if r+g+b >=600:
       cv2.putText(img, text, (50,50), 2,0.8, (0,0,0),2,cv2.LINE_AA)  if cv2.waitKey(20) & 0xFF == 27:
    break
cv2.destroyAllWindows()

Now let me show what kind of output we get :

For Source code: Click Here

If you feel any query or something wrong in the code do hit an issue on GitHub Repo : rk972006/color-python (github.com)

If you like content or want to see more exciting projects which can help your cv to be stared. Do Follow my medium page

Will be back with other content on Software Engineering.

Social Media :

Instagram: http://instagram.com/_._rohit_._kumar?utm_source=qr

Linkedin: https://www.linkedin.com/in/rohit-kumar-a13322126

Github: rk972006 (github.com) ( More Projects Repositories have been posted here)