/
color_classification_webcam.py
55 lines (44 loc) · 1.45 KB
/
color_classification_webcam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ----------------------------------------------
# --- Author : Ahmet Ozlu
# --- Mail : ahmetozlu93@gmail.com
# --- Date : 31st December 2017 - new year eve :)
# ----------------------------------------------
import cv2
from color_recognition_api import color_histogram_feature_extraction
from color_recognition_api import knn_classifier
import os
import os.path
cap = cv2.VideoCapture(1)
(ret, frame) = cap.read()
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...')
while True:
# Capture frame-by-frame
(ret, frame) = cap.read()
cv2.putText(
frame,
'Prediction: ' + prediction,
(15, 45),
cv2.FONT_HERSHEY_PLAIN,
3,
200,
)
# Display the resulting frame
cv2.imshow('color classifier', frame)
color_histogram_feature_extraction.color_histogram_of_test_image(frame)
prediction = knn_classifier.main('training.data', 'test.data')
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()