GithubHelp home page GithubHelp logo

ahmetozlu / color_recognition Goto Github PK

View Code? Open in Web Editor NEW
312.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 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

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

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

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

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 !

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()

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.