├── src ├── __init__.py ├── test.data ├── color_recognition_api │ ├── __init__.py │ ├── knn_classifier.py │ └── color_histogram_feature_extraction.py ├── black_cat.jpg ├── training_dataset │ ├── blue │ │ ├── blue.jpg │ │ ├── blue1.jpg │ │ ├── blue2.jpg │ │ ├── blue3.png │ │ ├── blue4.jpg │ │ └── blue5.JPG │ ├── red │ │ ├── red1.jpg │ │ ├── red10.jpg │ │ ├── red2.jpg │ │ ├── red3.jpg │ │ ├── red4.png │ │ ├── red5.jpg │ │ ├── red6.jpg │ │ ├── red7.png │ │ ├── red8.jpg │ │ └── red9.png │ ├── black │ │ ├── black1.png │ │ ├── black2.jpg │ │ ├── black3.png │ │ ├── black4.jpg │ │ ├── black5.jpg │ │ ├── black6.png │ │ ├── black7.jpg │ │ ├── black8.jpg │ │ ├── black9.jpg │ │ ├── black10.png │ │ └── black10.png.jpg │ ├── green │ │ ├── green1.jpg │ │ ├── green1.png │ │ ├── green2.png │ │ ├── green3.jpg │ │ ├── green4.jpg │ │ ├── green5.jpg │ │ ├── green6.png │ │ ├── green7.png │ │ ├── green8.png │ │ ├── green9.png │ │ └── green10.png │ ├── white │ │ ├── white1.png │ │ ├── white2.png │ │ ├── white3.png │ │ ├── white4.png │ │ ├── white5.jpg │ │ ├── white6.jpg │ │ ├── white7.jpg │ │ ├── white8.png │ │ ├── white9.jpg │ │ └── white10.jpg │ ├── orange │ │ ├── orange1.png │ │ ├── orange10.png │ │ ├── orange2.png │ │ ├── orange3.png │ │ ├── orange5.png │ │ ├── orange6.png │ │ ├── orange7.png │ │ ├── orange8.jpg │ │ └── orange9.jpg │ ├── violet │ │ ├── violet1.png │ │ ├── violet10.png │ │ ├── violet2.jpg │ │ ├── violet3.jpg │ │ ├── violet4.jpg │ │ ├── violet5.png │ │ ├── violet6.png │ │ ├── violet7.jpg │ │ ├── violet8.png │ │ └── violet9.png │ └── yellow │ │ ├── yellow1.jpg │ │ ├── yellow10.png │ │ ├── yellow2.png │ │ ├── yellow3.jpg │ │ ├── yellow4.png │ │ ├── yellow5.png │ │ ├── yellow6.png │ │ ├── yellow7.jpg │ │ ├── yellow8.jpg │ │ └── yellow9.jpg ├── training.data ├── color_classification_image.py └── color_classification_webcam.py ├── LICENSE ├── .gitignore └── README.md /src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/test.data: -------------------------------------------------------------------------------- 1 | 137,207,68 -------------------------------------------------------------------------------- /src/color_recognition_api/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/black_cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/black_cat.jpg -------------------------------------------------------------------------------- /src/training_dataset/blue/blue.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/blue/blue.jpg -------------------------------------------------------------------------------- /src/training_dataset/blue/blue1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/blue/blue1.jpg -------------------------------------------------------------------------------- /src/training_dataset/blue/blue2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/blue/blue2.jpg -------------------------------------------------------------------------------- /src/training_dataset/blue/blue3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/blue/blue3.png -------------------------------------------------------------------------------- /src/training_dataset/blue/blue4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/blue/blue4.jpg -------------------------------------------------------------------------------- /src/training_dataset/blue/blue5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/blue/blue5.JPG -------------------------------------------------------------------------------- /src/training_dataset/red/red1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red1.jpg -------------------------------------------------------------------------------- /src/training_dataset/red/red10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red10.jpg -------------------------------------------------------------------------------- /src/training_dataset/red/red2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red2.jpg -------------------------------------------------------------------------------- /src/training_dataset/red/red3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red3.jpg -------------------------------------------------------------------------------- /src/training_dataset/red/red4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red4.png -------------------------------------------------------------------------------- /src/training_dataset/red/red5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red5.jpg -------------------------------------------------------------------------------- /src/training_dataset/red/red6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red6.jpg -------------------------------------------------------------------------------- /src/training_dataset/red/red7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red7.png -------------------------------------------------------------------------------- /src/training_dataset/red/red8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red8.jpg -------------------------------------------------------------------------------- /src/training_dataset/red/red9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/red/red9.png -------------------------------------------------------------------------------- /src/training_dataset/black/black1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black1.png -------------------------------------------------------------------------------- /src/training_dataset/black/black2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black2.jpg -------------------------------------------------------------------------------- /src/training_dataset/black/black3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black3.png -------------------------------------------------------------------------------- /src/training_dataset/black/black4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black4.jpg -------------------------------------------------------------------------------- /src/training_dataset/black/black5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black5.jpg -------------------------------------------------------------------------------- /src/training_dataset/black/black6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black6.png -------------------------------------------------------------------------------- /src/training_dataset/black/black7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black7.jpg -------------------------------------------------------------------------------- /src/training_dataset/black/black8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black8.jpg -------------------------------------------------------------------------------- /src/training_dataset/black/black9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black9.jpg -------------------------------------------------------------------------------- /src/training_dataset/green/green1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green1.jpg -------------------------------------------------------------------------------- /src/training_dataset/green/green1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green1.png -------------------------------------------------------------------------------- /src/training_dataset/green/green2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green2.png -------------------------------------------------------------------------------- /src/training_dataset/green/green3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green3.jpg -------------------------------------------------------------------------------- /src/training_dataset/green/green4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green4.jpg -------------------------------------------------------------------------------- /src/training_dataset/green/green5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green5.jpg -------------------------------------------------------------------------------- /src/training_dataset/green/green6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green6.png -------------------------------------------------------------------------------- /src/training_dataset/green/green7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green7.png -------------------------------------------------------------------------------- /src/training_dataset/green/green8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green8.png -------------------------------------------------------------------------------- /src/training_dataset/green/green9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green9.png -------------------------------------------------------------------------------- /src/training_dataset/white/white1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white1.png -------------------------------------------------------------------------------- /src/training_dataset/white/white2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white2.png -------------------------------------------------------------------------------- /src/training_dataset/white/white3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white3.png -------------------------------------------------------------------------------- /src/training_dataset/white/white4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white4.png -------------------------------------------------------------------------------- /src/training_dataset/white/white5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white5.jpg -------------------------------------------------------------------------------- /src/training_dataset/white/white6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white6.jpg -------------------------------------------------------------------------------- /src/training_dataset/white/white7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white7.jpg -------------------------------------------------------------------------------- /src/training_dataset/white/white8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white8.png -------------------------------------------------------------------------------- /src/training_dataset/white/white9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white9.jpg -------------------------------------------------------------------------------- /src/training_dataset/black/black10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black10.png -------------------------------------------------------------------------------- /src/training_dataset/green/green10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/green/green10.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange1.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange10.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange2.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange3.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange5.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange6.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange7.png -------------------------------------------------------------------------------- /src/training_dataset/orange/orange8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange8.jpg -------------------------------------------------------------------------------- /src/training_dataset/orange/orange9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/orange/orange9.jpg -------------------------------------------------------------------------------- /src/training_dataset/violet/violet1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet1.png -------------------------------------------------------------------------------- /src/training_dataset/violet/violet10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet10.png -------------------------------------------------------------------------------- /src/training_dataset/violet/violet2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet2.jpg -------------------------------------------------------------------------------- /src/training_dataset/violet/violet3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet3.jpg -------------------------------------------------------------------------------- /src/training_dataset/violet/violet4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet4.jpg -------------------------------------------------------------------------------- /src/training_dataset/violet/violet5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet5.png -------------------------------------------------------------------------------- /src/training_dataset/violet/violet6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet6.png -------------------------------------------------------------------------------- /src/training_dataset/violet/violet7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet7.jpg -------------------------------------------------------------------------------- /src/training_dataset/violet/violet8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet8.png -------------------------------------------------------------------------------- /src/training_dataset/violet/violet9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/violet/violet9.png -------------------------------------------------------------------------------- /src/training_dataset/white/white10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/white/white10.jpg -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow1.jpg -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow10.png -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow2.png -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow3.jpg -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow4.png -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow5.png -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow6.png -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow7.jpg -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow8.jpg -------------------------------------------------------------------------------- /src/training_dataset/yellow/yellow9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/yellow/yellow9.jpg -------------------------------------------------------------------------------- /src/training_dataset/black/black10.png.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmetozlu/color_recognition/HEAD/src/training_dataset/black/black10.png.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ozlu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/training.data: -------------------------------------------------------------------------------- 1 | 139,0,0,red 2 | 204,22,0,red 3 | 206,0,25,red 4 | 220,0,3,red 5 | 254,0,0,red 6 | 61,13,3,red 7 | 128,24,24,red 8 | 174,32,26,red 9 | 254,0,2,red 10 | 209,23,23,red 11 | 255,255,0,yellow 12 | 255,242,39,yellow 13 | 249,217,94,yellow 14 | 252,234,4,yellow 15 | 255,183,9,yellow 16 | 247,224,23,yellow 17 | 254,242,0,yellow 18 | 246,191,39,yellow 19 | 255,215,12,yellow 20 | 255,166,0,yellow 21 | 159,217,140,green 22 | 125,194,75,green 23 | 37,202,38,green 24 | 0,166,82,green 25 | 64,189,85,green 26 | 0,128,1,green 27 | 35,67,17,green 28 | 123,252,1,green 29 | 0,255,0,green 30 | 33,83,54,green 31 | 125,232,88,green 32 | 252,79,19,orange 33 | 255,102,0,orange 34 | 255,127,0,orange 35 | 255,128,0,orange 36 | 255,103,0,orange 37 | 255,122,1,orange 38 | 254,101,33,orange 39 | 255,153,0,orange 40 | 255,103,0,orange 41 | 249,255,241,white 42 | 253,251,251,white 43 | 242,233,228,white 44 | 242,233,228,white 45 | 250,240,230,white 46 | 243,239,227,white 47 | 255,237,231,white 48 | 229,224,221,white 49 | 248,249,254,white 50 | 238,228,220,white 51 | 9,0,0,black 52 | 28,29,33,black 53 | 27,32,35,black 54 | 36,29,33,black 55 | 0,0,0,black 56 | 49,54,57,black 57 | 47,47,47,black 58 | 40,39,45,black 59 | 37,37,37,black 60 | 10,18,13,black 61 | 28,28,28,black 62 | 3,91,188,blue 63 | 0,0,254,blue 64 | 0,0,255,blue 65 | 1,119,193,blue 66 | 0,48,143,blue 67 | 0,0,154,blue 68 | -------------------------------------------------------------------------------- /src/color_classification_image.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # ------------------------------------------------------------------------- 4 | # --- Author : Ahmet Ozlu 5 | # --- Mail : ahmetozlu93@gmail.com 6 | # --- Date : 8th July 2018 - before Google inside look 2018 :) 7 | # ------------------------------------------------------------------------- 8 | 9 | import cv2 10 | from color_recognition_api import color_histogram_feature_extraction 11 | from color_recognition_api import knn_classifier 12 | import os 13 | import os.path 14 | import sys 15 | 16 | # read the test image 17 | try: 18 | source_image = cv2.imread(sys.argv[1]) 19 | except: 20 | source_image = cv2.imread('black_cat.jpg') 21 | prediction = 'n.a.' 22 | 23 | # checking whether the training data is ready 24 | PATH = './training.data' 25 | 26 | if os.path.isfile(PATH) and os.access(PATH, os.R_OK): 27 | print ('training data is ready, classifier is loading...') 28 | else: 29 | print ('training data is being created...') 30 | open('training.data', 'w') 31 | color_histogram_feature_extraction.training() 32 | print ('training data is ready, classifier is loading...') 33 | 34 | # get the prediction 35 | color_histogram_feature_extraction.color_histogram_of_test_image(source_image) 36 | prediction = knn_classifier.main('training.data', 'test.data') 37 | print('Detected color is:', prediction) 38 | cv2.putText( 39 | source_image, 40 | 'Prediction: ' + prediction, 41 | (15, 45), 42 | cv2.FONT_HERSHEY_PLAIN, 43 | 3, 44 | 200, 45 | ) 46 | 47 | # Display the resulting frame 48 | cv2.imshow('color classifier', source_image) 49 | cv2.waitKey(0) 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /src/color_classification_webcam.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # ---------------------------------------------- 4 | # --- Author : Ahmet Ozlu 5 | # --- Mail : ahmetozlu93@gmail.com 6 | # --- Date : 31st December 2017 - new year eve :) 7 | # ---------------------------------------------- 8 | 9 | import cv2 10 | from color_recognition_api import color_histogram_feature_extraction 11 | from color_recognition_api import knn_classifier 12 | import os 13 | import os.path 14 | 15 | cap = cv2.VideoCapture(1) 16 | (ret, frame) = cap.read() 17 | prediction = 'n.a.' 18 | 19 | # checking whether the training data is ready 20 | PATH = './training.data' 21 | 22 | if os.path.isfile(PATH) and os.access(PATH, os.R_OK): 23 | print ('training data is ready, classifier is loading...') 24 | else: 25 | print ('training data is being created...') 26 | open('training.data', 'w') 27 | color_histogram_feature_extraction.training() 28 | print ('training data is ready, classifier is loading...') 29 | 30 | while True: 31 | 32 | # Capture frame-by-frame 33 | (ret, frame) = cap.read() 34 | 35 | cv2.putText( 36 | frame, 37 | 'Prediction: ' + prediction, 38 | (15, 45), 39 | cv2.FONT_HERSHEY_PLAIN, 40 | 3, 41 | 200, 42 | ) 43 | 44 | # Display the resulting frame 45 | cv2.imshow('color classifier', frame) 46 | 47 | color_histogram_feature_extraction.color_histogram_of_test_image(frame) 48 | 49 | prediction = knn_classifier.main('training.data', 'test.data') 50 | if cv2.waitKey(1) & 0xFF == ord('q'): 51 | break 52 | 53 | # When everything done, release the capture 54 | cap.release() 55 | cv2.destroyAllWindows() 56 | -------------------------------------------------------------------------------- /src/color_recognition_api/knn_classifier.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # ---------------------------------------------- 4 | # --- Author : Ahmet Ozlu 5 | # --- Mail : ahmetozlu93@gmail.com 6 | # --- Date : 31st December 2017 - new year eve :) 7 | # ---------------------------------------------- 8 | 9 | import csv 10 | import random 11 | import math 12 | import operator 13 | import cv2 14 | 15 | 16 | # calculation of euclidead distance 17 | def calculateEuclideanDistance(variable1, variable2, length): 18 | distance = 0 19 | for x in range(length): 20 | distance += pow(variable1[x] - variable2[x], 2) 21 | return math.sqrt(distance) 22 | 23 | 24 | # get k nearest neigbors 25 | def kNearestNeighbors(training_feature_vector, testInstance, k): 26 | distances = [] 27 | length = len(testInstance) 28 | for x in range(len(training_feature_vector)): 29 | dist = calculateEuclideanDistance(testInstance, 30 | training_feature_vector[x], length) 31 | distances.append((training_feature_vector[x], dist)) 32 | distances.sort(key=operator.itemgetter(1)) 33 | neighbors = [] 34 | for x in range(k): 35 | neighbors.append(distances[x][0]) 36 | return neighbors 37 | 38 | 39 | # votes of neighbors 40 | def responseOfNeighbors(neighbors): 41 | all_possible_neighbors = {} 42 | for x in range(len(neighbors)): 43 | response = neighbors[x][-1] 44 | if response in all_possible_neighbors: 45 | all_possible_neighbors[response] += 1 46 | else: 47 | all_possible_neighbors[response] = 1 48 | sortedVotes = sorted(all_possible_neighbors.items(), 49 | key=operator.itemgetter(1), reverse=True) 50 | return sortedVotes[0][0] 51 | 52 | 53 | # Load image feature data to training feature vectors and test feature vector 54 | def loadDataset( 55 | filename, 56 | filename2, 57 | training_feature_vector=[], 58 | test_feature_vector=[], 59 | ): 60 | with open(filename) as csvfile: 61 | lines = csv.reader(csvfile) 62 | dataset = list(lines) 63 | for x in range(len(dataset)): 64 | for y in range(3): 65 | dataset[x][y] = float(dataset[x][y]) 66 | training_feature_vector.append(dataset[x]) 67 | 68 | with open(filename2) as csvfile: 69 | lines = csv.reader(csvfile) 70 | dataset = list(lines) 71 | for x in range(len(dataset)): 72 | for y in range(3): 73 | dataset[x][y] = float(dataset[x][y]) 74 | test_feature_vector.append(dataset[x]) 75 | 76 | 77 | def main(training_data, test_data): 78 | training_feature_vector = [] # training feature vector 79 | test_feature_vector = [] # test feature vector 80 | loadDataset(training_data, test_data, training_feature_vector, test_feature_vector) 81 | classifier_prediction = [] # predictions 82 | k = 3 # K value of k nearest neighbor 83 | for x in range(len(test_feature_vector)): 84 | neighbors = kNearestNeighbors(training_feature_vector, test_feature_vector[x], k) 85 | result = responseOfNeighbors(neighbors) 86 | classifier_prediction.append(result) 87 | return classifier_prediction[0] 88 | -------------------------------------------------------------------------------- /src/color_recognition_api/color_histogram_feature_extraction.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # ---------------------------------------------- 4 | # --- Author : Ahmet Ozlu 5 | # --- Mail : ahmetozlu93@gmail.com 6 | # --- Date : 31st December 2017 - new year eve :) 7 | # ---------------------------------------------- 8 | 9 | from PIL import Image 10 | import os 11 | import cv2 12 | import numpy as np 13 | import matplotlib.pyplot as plt 14 | from scipy.stats import itemfreq 15 | from color_recognition_api import knn_classifier as knn_classifier 16 | 17 | 18 | def color_histogram_of_test_image(test_src_image): 19 | 20 | # load the image 21 | image = test_src_image 22 | 23 | chans = cv2.split(image) 24 | colors = ('b', 'g', 'r') 25 | features = [] 26 | feature_data = '' 27 | counter = 0 28 | for (chan, color) in zip(chans, colors): 29 | counter = counter + 1 30 | 31 | hist = cv2.calcHist([chan], [0], None, [256], [0, 256]) 32 | features.extend(hist) 33 | 34 | # find the peak pixel values for R, G, and B 35 | elem = np.argmax(hist) 36 | 37 | if counter == 1: 38 | blue = str(elem) 39 | elif counter == 2: 40 | green = str(elem) 41 | elif counter == 3: 42 | red = str(elem) 43 | feature_data = red + ',' + green + ',' + blue 44 | # print(feature_data) 45 | 46 | with open('test.data', 'w') as myfile: 47 | myfile.write(feature_data) 48 | 49 | 50 | def color_histogram_of_training_image(img_name): 51 | 52 | # detect image color by using image file name to label training data 53 | if 'red' in img_name: 54 | data_source = 'red' 55 | elif 'yellow' in img_name: 56 | data_source = 'yellow' 57 | elif 'green' in img_name: 58 | data_source = 'green' 59 | elif 'orange' in img_name: 60 | data_source = 'orange' 61 | elif 'white' in img_name: 62 | data_source = 'white' 63 | elif 'black' in img_name: 64 | data_source = 'black' 65 | elif 'blue' in img_name: 66 | data_source = 'blue' 67 | elif 'violet' in img_name: 68 | data_source = 'violet' 69 | 70 | # load the image 71 | image = cv2.imread(img_name) 72 | 73 | chans = cv2.split(image) 74 | colors = ('b', 'g', 'r') 75 | features = [] 76 | feature_data = '' 77 | counter = 0 78 | for (chan, color) in zip(chans, colors): 79 | counter = counter + 1 80 | 81 | hist = cv2.calcHist([chan], [0], None, [256], [0, 256]) 82 | features.extend(hist) 83 | 84 | # find the peak pixel values for R, G, and B 85 | elem = np.argmax(hist) 86 | 87 | if counter == 1: 88 | blue = str(elem) 89 | elif counter == 2: 90 | green = str(elem) 91 | elif counter == 3: 92 | red = str(elem) 93 | feature_data = red + ',' + green + ',' + blue 94 | 95 | with open('training.data', 'a') as myfile: 96 | myfile.write(feature_data + ',' + data_source + '\n') 97 | 98 | 99 | def training(): 100 | 101 | # red color training images 102 | for f in os.listdir('./training_dataset/red'): 103 | color_histogram_of_training_image('./training_dataset/red/' + f) 104 | 105 | # yellow color training images 106 | for f in os.listdir('./training_dataset/yellow'): 107 | color_histogram_of_training_image('./training_dataset/yellow/' + f) 108 | 109 | # green color training images 110 | for f in os.listdir('./training_dataset/green'): 111 | color_histogram_of_training_image('./training_dataset/green/' + f) 112 | 113 | # orange color training images 114 | for f in os.listdir('./training_dataset/orange'): 115 | color_histogram_of_training_image('./training_dataset/orange/' + f) 116 | 117 | # white color training images 118 | for f in os.listdir('./training_dataset/white'): 119 | color_histogram_of_training_image('./training_dataset/white/' + f) 120 | 121 | # black color training images 122 | for f in os.listdir('./training_dataset/black'): 123 | color_histogram_of_training_image('./training_dataset/black/' + f) 124 | 125 | # blue color training images 126 | for f in os.listdir('./training_dataset/blue'): 127 | color_histogram_of_training_image('./training_dataset/blue/' + f) 128 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # COLOR RECOGNITION 2 | 3 | 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](https://github.com/ahmetozlu/color_classifier/tree/master/src/training_dataset) or consider about other color features such as [Color Moments](https://en.wikipedia.org/wiki/Color_moments) or [Color Correlogram](http://www.cs.cornell.edu/rdz/Papers/ecdl2/spatial.htm). 4 | 5 | You can use [color_recognition_api](https://github.com/ahmetozlu/color_recognition/tree/master/src/color_recognition_api) to perform real-time color recognition in your projects. You can find a sample usage of [color_recognition_api](https://github.com/ahmetozlu/color_recognition/tree/master/src/color_recognition_api) in this [**repo**](https://github.com/ahmetozlu/vehicle_counting_tensorflow). ***Please contact if you need professional color recognition project with the super high accuracy!*** 6 | 7 | ## Quick Demo 8 | 9 | ***Run [color_classification_webcam.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_classification_webcam.py) to perform real-time color recognition on a webcam stream.*** 10 | 11 |

12 | 13 |

14 | 15 | ***Run [color_classification_image.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_classification_image.py) to perform color recognition on a single image.*** 16 | 17 |

18 | 19 |

20 | 21 | --- 22 | **What does this program do?** 23 | 1. **Feature Extraction:** Perform feature extraction for getting the R, G, B Color Histogram values of [training images](https://github.com/ahmetozlu/color_classifier/tree/master/src/training_dataset) 24 | 2. **Training K-Nearest Neighbors Classifier:** Train KNN classifier by R, G, B Color Histogram values 25 | 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. 26 | --- 27 | 28 | **TODOs:** 29 | 30 | - "Add New Color" utility will be added. 31 | - New feature extractors will be added. 32 | - New classifiers will be added. 33 | 34 | 35 | ## Theory 36 | 37 | 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. 38 | 39 |

40 | 41 |

42 | 43 | You should know 2 main pheomena to understand basic Object Detection/Recognition Systems of Computer Vision and Machine Learning. 44 | 45 | **1.) Feature Extraction** 46 | 47 | How to represent the interesting points we found to compare them with other interesting points (features) in the image. 48 | 49 | **2.) Classification** 50 | 51 | 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. 52 | 53 | For this project; 54 | 55 | **1.) Feature Extraction** = Color Histogram 56 | 57 | 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. 58 | 59 |

60 | 61 |

62 | 63 | **2.) Classification** = K-Nearest Neighbors Algorithm 64 | 65 | 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. 66 | 67 |

68 | 69 |

70 | 71 | ## Implementation 72 | 73 | [OpenCV](https://pypi.python.org/pypi/opencv-python) was used for color histogram calculations and knn classifier. [NumPy](https://stackoverflow.com/questions/29499815/how-to-install-numpy-on-windows-using-pip-install) was used for matrix/n-dimensional array calculations. The program was developed on Python at Linux environment. 74 | 75 | In the “[src](https://github.com/ahmetozlu/color_recognition/tree/master/src)” folder, there are 2 Python classes which are: 76 | 77 | - **[color_classification_webcam.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_classification_webcam.py):** test class to perform real-time color recognition form webcam stream. 78 | 79 | - **[color_classification_image.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_classification_image.py):** test class to perform color recognition on a single image. 80 | 81 | In the “[color_recognition_api](https://github.com/ahmetozlu/color_recognition/tree/master/src/color_recognition_api)” folder, there are 2 Python classes which are: 82 | 83 | - **[feature_extraction.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_recognition_api/color_histogram_feature_extraction.py):** feature extraction operation class 84 | 85 | - **[knn_classifier.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_recognition_api/knn_classifier.py):** knn classification class 86 | 87 | **1.) Explanation of “[feature_extraction.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_recognition_api/color_histogram_feature_extraction.py)"** 88 | 89 | 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. 90 | 91 |

92 | 93 |

94 | 95 | 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]. 96 | 97 | 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](https://github.com/ahmetozlu/color_recognition/blob/master/src/training.data) under src folder. 98 | 99 | **2.) Explanation of “[knn_classifier.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_recognition_api/knn_classifier.py)”** 100 | 101 | This class provides these main calculations; 102 | 103 | 1. Fetching training data 104 | 2. Fetching test image features 105 | 3. Calculating euclidean distance 106 | 4. Getting k nearest neighbors 107 | 5. Prediction of color 108 | 6. Returning the prediction is true or false 109 | 110 | **“[color_classification_webcam.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_classification_webcam.py)”** is the main class of my program, it provides; 111 | 112 | 1. Calling [feature_extraction.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_recognition_api/color_histogram_feature_extraction.py) to create training data by feature extraction 113 | 2. Calling [knn_classifier.py](https://github.com/ahmetozlu/color_recognition/blob/master/src/color_recognition_api/knn_classifier.py) for classification 114 | 115 | You can find training data in [here](https://github.com/ahmetozlu/color_classifier/tree/master/src/training_dataset). 116 | 117 | You can find features are got from training data in [here](https://raw.githubusercontent.com/ahmetozlu/color_classifier/master/src/training.data). 118 | 119 | ## Conclusion 120 | 121 | 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. 122 | 123 | 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. 124 | 125 | ## Citation 126 | If you use this code for your publications, please cite it as: 127 | 128 | @ONLINE{cr, 129 | author = "Ahmet Özlü", 130 | title = "Color Recognition", 131 | year = "2018", 132 | url = "https://github.com/ahmetozlu/color_recognition" 133 | } 134 | 135 | ## Author 136 | Ahmet Özlü 137 | 138 | ## License 139 | This system is available under the MIT license. See the LICENSE file for more info. 140 | --------------------------------------------------------------------------------