Exemple #1
0
def do_classify(
        img,  # image matrix
        train_data_file_name  # image matrix
):
    # read the test image
    source_image = cv2.imread(img)
    prediction = 'n.a.'

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

    if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
        # print('training data is ready, classifier is loading...')
        print("")
    else:
        print('training data is being created...')
        open('training.data', 'w')
        color_histogram_feature_extraction.training()
        print('training data is ready, classifier is loading...')

    # get the prediction
    color_histogram_feature_extraction.color_histogram_of_test_image(
        source_image)
    prediction = knn_classifier.main(train_data_file_name, 'test.data')
    print('training data is over,result', train_data_file_name, prediction)
 def __init__(self):
     print("Initializing ....")
     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...')
    def training(self):
        # checking whether the training data is ready
        self.PATH = './training.data'

        if os.path.isfile(self.PATH) and os.access(self.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...')
def getColor(imageinput):
    source_image=cv2.imread(imageinput)
    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()
    # get the prediction
    color_histogram_feature_extraction.color_histogram_of_test_image(source_image)
    prediction = knn_classifier.main('training.data', 'test.data')
    print('Detected color is:', prediction)
    return prediction
def color_detect():
    rospy.init_node('color_detection', anonymous=True)
    color_pub = rospy.Publisher('detect_color', String, queue_size=10)
    r = rospy.Rate(10)
    cap = cv2.VideoCapture(0)
    (ret, frame) = cap.read()
    prediction = 'n.a.'
    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...')
    old_predict = prediction
    count = 0

    while not rospy.is_shutdown():
        (ret, frame) = cap.read()  # record
        cv2.putText(frame, 'Prediction: ' + prediction, (15, 45),
                    cv2.FONT_HERSHEY_PLAIN, 4, 300)
        # 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

        if old_predict == prediction:
            count += 1
            print(">> " + prediction)
        else:
            count = 0
            old_predict = prediction

        if count > 20:
            color_pub.publish(prediction)
        r.sleep()

    cap.release()
    cv2.destroyAllWindows()
import os
import os.path

# read the test image
source_image = cv2.imread('black_cat.jpg')
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...')

# get the prediction
color_histogram_feature_extraction.color_histogram_of_test_image(source_image)
prediction = knn_classifier.main('training.data', 'test.data')
cv2.putText(
    source_image,
    'Prediction: ' + prediction,
    (15, 45),
    cv2.FONT_HERSHEY_PLAIN,
    3,
    200,
    )

# Display the resulting frame