Giter VIP home page Giter VIP logo

ahmetozlu / color_recognition Goto Github PK

View Code? Open in Web Editor NEW
314.0 12.0 123.0 6.84 MB

:art: Color recognition & classification & detection on webcam stream / on video / on single image using K-Nearest Neighbors (KNN) is trained with color histogram features by OpenCV.

Home Page: https://youtu.be/05PYXwBw3Z0

License: MIT License

Python 100.00%
color-recognition color-detection color-classification color-histogram knn-classification opencv-python numpy computer-vision machine-learning data-science

color_recognition's Introduction

COLOR RECOGNITION

This project focuses on color classifying by K-Nearest Neighbors Machine Learning Classifier which is trained by R, G, B Color Histogram. It can classify White, Black, Red, Green, Blue, Orange, Yellow and Violet. If you want to classify more color or improve the accuracy you should work on the training data or consider about other color features such as Color Moments or Color Correlogram.

You can use color_recognition_api to perform real-time color recognition in your projects. You can find a sample usage of color_recognition_api in this repo. Please contact if you need professional color recognition project with the super high accuracy!

Quick Demo

Run color_classification_webcam.py to perform real-time color recognition on a webcam stream.

Run color_classification_image.py to perform color recognition on a single image.


What does this program do?

  1. Feature Extraction: Perform feature extraction for getting the R, G, B Color Histogram values of training images
  2. Training K-Nearest Neighbors Classifier: Train KNN classifier by R, G, B Color Histogram values
  3. Classifying by Trained KNN: Read Web Cam frame by frame, perform feature extraction on each frame and then classify the mean color of it by trained KNN classifier.

TODOs:

  • "Add New Color" utility will be added.
  • New feature extractors will be added.
  • New classifiers will be added.

Theory

In this study, colors are classified by using K-Neares Neşghbor Machine Learning classifier algorithm. This classifier is trained by image R, G, B Color Histogram values. The general work flow is given at the below.

You should know 2 main pheomena to understand basic Object Detection/Recognition Systems of Computer Vision and Machine Learning.

1.) Feature Extraction

How to represent the interesting points we found to compare them with other interesting points (features) in the image.

2.) Classification

An algorithm that implements classification, especially in a concrete implementation, is known as a classifier. The term "classifier" sometimes also refers to the mathematical function, implemented by a classification algorithm, that maps input data to a category.

For this project;

1.) Feature Extraction = Color Histogram

Color Histogram is a representation of the distribution of colors in an image. For digital images, a color histogram represents the number of pixels that have colors in each of a fixed list of color ranges, that span the image's color space, the set of all possible colors.

2.) Classification = K-Nearest Neighbors Algorithm

K nearest neighbors is a simple algorithm that stores all available cases and classifies new cases based on a similarity measure (e.g., distance functions). KNN has been used in statistical estimation and pattern recognition already in the beginning of 1970’s as a non-parametric technique.

Implementation

OpenCV was used for color histogram calculations and knn classifier. NumPy was used for matrix/n-dimensional array calculations. The program was developed on Python at Linux environment.

In the “src” folder, there are 2 Python classes which are:

In the “color_recognition_api” folder, there are 2 Python classes which are:

1.) Explanation of “feature_extraction.py"

I can get the RGB color histogram of images by this Python class. For example, plot of RGB color histogram for one of the red images is given at the below.

I decided to use bin number of histogram which has the peak value of pixel count for R, G and B as feature so I can get the dominant R, G and B values to create feature vectors for training. For example, the dominant R, G and B values of the red image which is given at above is [254, 0, 2].

I get the dominant R, G, B values by using Color Histogram for each training image then I labelled them because KNN classifier is a supervised learner and I deploy these feature vectors in the csv file. Thus, I create my training feature vector dataset. It can be found in the file which name’s is training.data under src folder.

2.) Explanation of “knn_classifier.py

This class provides these main calculations;

  1. Fetching training data
  2. Fetching test image features
  3. Calculating euclidean distance
  4. Getting k nearest neighbors
  5. Prediction of color
  6. Returning the prediction is true or false

color_classification_webcam.py is the main class of my program, it provides;

  1. Calling feature_extraction.py to create training data by feature extraction
  2. Calling knn_classifier.py for classification

You can find training data in here.

You can find features are got from training data in here.

Conclusion

I think, the training data has a huge important in classification accuracy. I created my training data carefully but maybe the accuracy can be higher with more suitable training data.

Another important thing is lightning and shadows. In my test images, the images which were taken under bad lighting conditions and with shadows are classified wrong (false positives), maybe some filtering algorithm should/can be implemented before the test images send to KNN classifier Thus, accuracy can be improved.

Citation

If you use this code for your publications, please cite it as:

@ONLINE{cr,
    author = "Ahmet Özlü",
    title  = "Color Recognition",
    year   = "2018",
    url    = "https://github.com/ahmetozlu/color_recognition"
}

Author

Ahmet Özlü

License

This system is available under the MIT license. See the LICENSE file for more info.

color_recognition's People

Contributors

ahmetozlu avatar kharshit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

color_recognition's Issues

can I use it for a region of interest?

I tried to implement it for a region of interest but gave me this error.

Traceback (most recent call last):
File "/home/redhwan/1/body_color_training/color_recognition-master/src/webcam_human_color.py", line 72, in
prediction = knn_classifier.main('training.data', 'test.data')
File "/home/redhwan/1/body_color_training/color_recognition-master/src/color_recognition_api/knn_classifier.py", line 87, in main
return classifier_prediction[0]
IndexError: list index out of range

from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import imutils
import time
import cv2

from color_recognition_api import color_histogram_feature_extraction
from color_recognition_api import knn_classifier
import os
import os.path
# global x1 , y1 , w1, h1, endX, startX, endY, startY , area1


CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe('prototxt.txt', 'caffemodel')
print("[INFO] starting video stream...")
prediction = 'n.a.'

# checking whether the training data is ready
PATH = './training.data'

if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
    print ('training data is ready, classifier is loading...')
else:
    print ('training data is being created...')
    open('training.data', 'w')
    color_histogram_feature_extraction.training()
    print ('training data is ready, classifier is loading...')
vs = cv2.VideoCapture(0)
# vs.release()
# vs = cv2.VideoCapture(0)
time.sleep(2.0)
fps = FPS().start()
width = vs.get(3)  # float
height = vs.get(4)  # float
print width, height

fps = FPS().start()
while True:
    ret, frame = vs.read()





    # frame = imutils.resize(frame, width=400)
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5)
    net.setInput(blob)
    detections = net.forward()
    big_area = 0
    big_center = 320
    detected = 0
    for i in np.arange(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        object_type = int(detections[0, 0, i, 1])
        if object_type == 15 and confidence > 0.2:
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
            label = "{}: {:.2f}%".format('person', confidence * 100)
            cv2.rectangle(frame, (startX, startY), (endX, endY), [0, 0, 255], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 0, 255], 2)
            # ROI = green[startX:endX, startY:endY]

            ROI1 = frame[startX:endX, startY:endY]
            color_histogram_feature_extraction.color_histogram_of_test_image(ROI1)

            prediction = knn_classifier.main('training.data', 'test.data')
            print  prediction
            rect_area = (endX-startX)*(endY-startY)
            # print (endX, startX, endY, startY, type(endX),type(startX),type(endY),type(startY))
            detected = 1
            if rect_area > big_area:
                big_area = rect_area

    cv2.imshow("Frame", frame)

    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break
    fps.update()
    fps.stop()
vs.release()    
cv2.destroyAllWindows()

error in your code

Hi.
I just realized an error in your code:
You are calculating the Euclidean distance according to length = 2 (meaning only b and g) , you are ignoring the R. What your doing is sqrt[(x2-x1)^2 + (y2-y1)^2] rather than
sqrt[(x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2]
When you are doing length = len(testInstance) - 1, then since your testInstance length =3, that means you are considering a length of 2 --> so it will be : for I in range (2), and you'll ignore the r channel in that case. Are you intending to do that? As what I know, you should also consider the r channel.
Thanks

Color recognition - improve training

Hey, I'm working with something similar and I wanted to use knn to recognize the colors in "real time". I saw your training data and I would like to know how did you label? (139, 0, 0, red for example)

I'm used to use Image Label to label objects, but with color I got confuse.
Can you help me saying how did you extract these information?

And btw great job.

Thank you

test.data, what is supposed to be?

Hello @ahmetozlu Ahmetozlu,
What is supposed to be inside the test.data file?
You did not provide explanations about that file.
I'm trying to implement your code after object detection, with numpy array and I have trouble like this :

` ~/Documents/Code/yolov3-tf2/yolov3_tf2/utils.py in color_recog(cropped_np)
    165         print('training data is ready, classifier is loading...')
    166 
--> 167     # get the prediction
    168     color_histogram_feature_extraction.color_histogram_of_test_image(cropped_np)
    169     prediction = knn_classifier.main('./data/training.data', './data/test.data')

~/Documents/Code/yolov3-tf2/yolov3_tf2/knn_classifier.py in main(training_data, test_data)
     74     k = 3  # K value of k nearest neighbor
     75     for x in range(len(test_feature_vector)):
---> 76         neighbors = kNearestNeighbors(training_feature_vector, test_feature_vector[x], k)
     77         result = responseOfNeighbors(neighbors)
     78         classifier_prediction.append(result)

~/Documents/Code/yolov3-tf2/yolov3_tf2/knn_classifier.py in kNearestNeighbors(training_feature_vector, testInstance, k)
     25     neighbors = []
     26     for x in range(k):
---> 27         neighbors.append(distances[x][0])
     28     return neighbors
     29 

IndexError: list index out of range

`

Thanks !

IndexError at 'knn_classifier.py'

training data is ready, classifier is loading...
Traceback (most recent call last):
  File ".\src\color_classification_image.py", line 32, in <module>
    prediction = knn_classifier.main('training.data', 'test.data')
  File "...\color_recognition-master\color_recognition-master\src\color_recognition_api\knn_classifier.py", line 87, in main
    return classifier_prediction[0])
IndexError: list index out of range

IDE

Hi ...,
Which IDE are you using?
Best regards,
PeterPham

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.