Are you a designer? Do you use Adobe or Corel applications very often? You might be very particular about the color you use or what you want in your design. Do you try to use the Colors you observe while traveling, shopping, etc? It is hard to recollect the exact shade which you had seen earlier? Well, this Live Color Detector in Python comes to your rescue.
I recently started reading about how I could work with Images in Python. When I came across OpenCV which allows import and manipulation of images in Python, I started to wonder if the information could be extracted out of those images using Machine Learning and used in some way.
We’ve all seen that we can search online on the basis of certain filters one of which is color. These manipulations of images in OpenCV actually inspired me to actually write the code. Code that can extract colors out of images taken from a live stream.
In this article, I will explain how I understood and used the basics of OpenCV and extracted colors from images in live video. The complete notebook is also made available at my repository. (Link provided below)
What is Color Detection?
Color detection is just the process of detecting the code or name of any color. Simple isn’t it? Well, for humans this is an extremely easy task but for computers, it is not straightforward. Human eyes and brains work together to translate light into color. Light receptors that are present in our eyes transmit the signal to the brain. Our brain then recognizes the color. Since childhood, we have mapped certain lights with their color names. We will be using a somewhat similar strategy to detect color names in Live Color Detection in Python project.
Before starting with this Python project – Live Colour Detection, you should be little bit familiar with the computer vision library of Python that is OpenCV and Numpy.
OpenCV, Pandas, and numpy are the Python packages that are necessary for this project in Python. I have explained later in this article how to install them.
A python library which allows us to use the web camera or secondary camera connected to our system.
Python Imaging Library (abbreviated as PIL) (in newer versions known as Pillow) is a free library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. -Wikipedia
NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. -Wikipedia
We need numpy since it allows us to point to the pixel of our interest.
How to run?
Steps for Building a Project in Python – Color Detection
Here are the steps to build an application in Python that can detect colors:
- git clone From Here.
- cd live-color-picker
- pip install -r requirements.txt
- jupyter notebook
- press SHIFT+Enter on each block
Demonstration of Live Color Detector
As the name suggests it is a color detector that is live. You read it right, you can get the exact color code of anything you watch from your naked eyes with this application. All you need to do is press the C button of your keyboard. When you press the C key the application captures the current frame from your feed to detect the color from it.
Now feel free to click any region of this frame a pop window appears showing you how that particular pixel looks like, it also gives you the RGB color code of that pixel so you can now note it and use in your designs.
Here is a video demonstration for you !!
- Firstly let’s import the libraries CV2, Numpy, PIL
2. Start capturing the feed
The 0 in cv2.VideoCapture() tells OpenCV to use the primary camera connected with your system.
We are flipping the feed by using the .flip() function of OpenCV. Feel free to play with the code see what happens if you do not flip the code.
3. Capture the frame on click of a key
The above code captures the current frame and opens a new window beside your live feed when you press the ‘c’ key.
Similarly, when you press the space bar key the application will quit and free your camera.
I will explain the last line of this code snippet latter in the article.
4. Getting the color code – code snippet(PIX function)
When you double click a point in the captured frame .setMouseCallback function passes the x,y coordinates of that frame to PIX() function. After this .getpixel() function is used to get the RGB color code for the x,y pixel. The next part of this function creates a window with the RGB code and also sets the background of the window to the color of the pixel.
In this Python project with source code, we saw about colors and how we can extract color RGB values and the color name of a pixel. This can be used in numerous image editing and drawing apps.
Share Live Color Detector with your friends who might need it.
Liked the project? Hit the star button on Github if you liked the project.
Give your opinions about this live color detector in python in the comment section. Also If you have queries regarding this post, you can write it down in the comment section below.
Feel free to contact us!
Thank you! Happy Coding =)