def color_recognition(crop_img): height, width, channels = crop_img.shape crop_img = crop_image.crop_center( crop_img, 50, 50 ) # crop the detected vehicle image and get a image piece from center of it both for debugging and sending that image piece to color recognition module # for debugging #cv2.imwrite(current_path + "/debug_utility"+".png",crop_img) # save image piece for debugging open(current_path + "/utils/color_recognition_module/" + "test.data", "w") color_histogram_feature_extraction.color_histogram_of_test_image( crop_img) # send image piece to regonize vehicle color prediction = knn_classifier.main( current_path + "/utils/color_recognition_module/" + "training.data", current_path + "/utils/color_recognition_module/" + "test.data") return prediction
mount = [0,0,0,0,0,0,0,0,0,0] for i in range(0,len(coll)): io.imsave('test1.jpg',coll[i]) io.imsave('test2.jpg',coll[i]) #print(coll[i]) img1 = Image.open("test1.jpg") cut_lower_half(img1) frame = cv2.imread("linshi.jpg") pre_class = cv2.imread("test1.jpg") prediction = "n.a." frame2 = cv2.imread("test2.jpg") color_histogram_feature_extraction.color_histogram_of_test_image(frame) prediction = knn_classifier.main("training.data", "test.data") cv2.putText(frame2, prediction, (15, 45 ), cv2.FONT_HERSHEY_PLAIN, 3, (255,255,255),5) cv2.imshow('pre color',pre_class) #cv2.waitKey(0)&0xFF cv2.imshow('color classifier',frame2) cv2.imwrite('./result_picture/' + picture_name[i],frame2) #用于统计正确率 if prediction == "black": mount[0] = mount[0] + 1 elif prediction == "gray": mount[1] = mount[1] + 1 elif prediction == "white": mount[2] = mount[2] + 1 elif prediction == "red": mount[3] = mount[3] + 1
def detectShape(path): # for detecting the contour of the pill and its length, and the pill's predicted color img = cv2.imread(path, 1) imgheight = img.shape[0] imgwidth = img.shape[1] img = cv2.resize(img, (int(imgwidth * (480 / imgheight)), 480)) img = cv2.GaussianBlur(img, (3, 3), 0) # Gaussian blurring to remove noise in the image kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) # img = cv2.filter2D(img, -1, kernel) drugShape = 'UNDEFINED' # initializing the drug shape edges = cv2.Canny(img, 100, 200) # detecting the edges to identify the pill contours, hierarchy = cv2.findContours( edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # finding the contours in the image areas = np.array([cv2.contourArea(c) for c in contours]) # array of the areas of all contours contours = np.array(contours) # array of all contours in the image arr1inds = areas.argsort() # sorting contours contours = contours[ arr1inds[:: -1]] # descendingly, as the pill will be tha largest contour approx = cv2.approxPolyDP(contours[0], 0.01 * cv2.arcLength(contours[0], True), True) x, y, w, h = cv2.boundingRect( contours[0]) # offsets - with this you get 'mask' cv2.drawContours(img, [contours[0]], 0, (255, 0, 0), 2) # to get the average of the colors inside the largest contour "inside the pill" newIM = img[y:y + h, x:x + w] yn = newIM.shape[0] xn = newIM.shape[1] y = y + int(yn * 20 / 100) h = h - int(yn * 40 / 100) x = x + int(xn * 20 / 100) w = w - int(xn * 30 / 100) newImage = img[y:y + h, x:x + w] # inside the contour colors = np.array(cv2.mean(newImage)).astype( np.uint8) # average of the colors inside newImage prediction = 'n.a.' # increase saturation before white detection for light colors elimination hsvImage = cv2.cvtColor(newImage, cv2.COLOR_BGR2HSV) hsvImage[:, :, 1] = hsvImage[:, :, 1] * 2.5 # increasing the saturation of the color backImage = cv2.cvtColor(hsvImage, cv2.COLOR_HSV2BGR) # using BGR backColors = np.array(cv2.mean(backImage)).astype( np.uint8) # average of colors after increasing the saturation # the prediction of the color classifier RGB prediction = knn_classifier.main( 'training.data', np.array([backColors[2], backColors[1], backColors[0]])) if prediction == 'white': # for white only not the light colors #RED GREEN BLUE if backColors[2] >= 180 and backColors[1] >= 150 and backColors[ 0] <= 90: # it will not be white using trial and error prediction = 'other' else: backColors = np.array(cv2.mean(newImage)).astype( np.uint8) # average of colors after increasing the saturation # the prediction of the color classifier RGB prediction = knn_classifier.main( 'training.data', np.array([backColors[2], backColors[1], backColors[0]])) if backColors[2] >= 180 and backColors[1] >= 150 and backColors[ 0] <= 90: # it will not be white using trial and error prediction = 'other' if prediction == 'other': # using HSV colors = rgb2hsv(colors[2], colors[1], colors[0]) # the prediction of the color classifier HSV prediction = knn_classifier.main( 'newData.data', np.array([colors[0], colors[1], colors[2]])) return len(approx), prediction, contours[0]
import cv2 import feature import knn_classifier import os import os.path source_image = cv2.imread('redshirt.jpg') 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: open('training.data', 'w') feature.training() feature.test_image(source_image) prediction = knn_classifier.main('training.data', 'test.data') cv2.putText( source_image, 'Prediction: ' + prediction, (35, 25), cv2.FONT_HERSHEY_PLAIN, 1, 50, ) cv2.imshow('color classifier', source_image) cv2.waitKey(0)
# cv2.waitKey(0) # ### Video Testing source_video = "video.mp4" cap = cv2.VideoCapture(source_video) while (True): # Capture frame-by-frame ret, frame = cap.read() cv2.imwrite("frame.jpg", frame) featureExtractor.main(2, "frame.jpg") # extract feature for our test image prediction = knn_classifier.main('features.data', 'test.data') cv2.putText(frame, prediction, (15, 45), cv2.FONT_HERSHEY_PLAIN, 3, 200, thickness=3) cv2.imshow('frame', frame) if cv2.waitKey(120) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
import knn_classifier import numpy as np prediction = 'n.a.' prediction = knn_classifier.main('training.data', np.array([255,255,255])) print('pred', prediction)
def main(argv): arg_index_start = 0 DEBUG = "FALSE" try: opts, args = getopt.getopt(argv, 'd', ['debug']) if not args: usage() sys.exit(2) except getopt.GetoptError as err: usage() sys.exit(2) for opt, arg in opts: if opt in ('-d'): DEBUG = "TRUE" arg_index_start = 1 # arguments if not argv[arg_index_start + 4]: usage() sys.exit(2) if os.path.isdir(argv[arg_index_start]): feature_files_dir = argv[arg_index_start] else: print("Feature directory does not exist: ", argv[arg_index_start], file=sys.stderr) usage() sys.exit(2) if os.path.isfile(argv[arg_index_start + 1]): outtype_dataset_file = argv[arg_index_start + 1] else: print("Out type datafile does not exist: ", argv[arg_index_start + 1], file=sys.stderr) usage() sys.exit(2) i = 2 debug_opt = "" datatype_filenames = [] datatypes = [] dist_metrics = [] weights = [] while i < len(args): if os.path.isfile(argv[arg_index_start + i]): datatype_filename = argv[arg_index_start + i] datatype_filenames.append(datatype_filename) datatype = os.path.basename(datatype_filename.rsplit('-', 1)[0]) datatypes.append(datatype) else: print("Input datatype file does not exist: ", argv[arg_index_start + i], file=sys.stderr) usage() sys.exit(2) if argv[arg_index_start + (i + 1)]: dist_metrics.append(argv[arg_index_start + (i + 1)]) else: print("No distance metric provided for ", argv[arg_index_start + i], file=sys.stderr) usage() sys.exit(2) if argv[arg_index_start + (i + 2)]: weights.append(argv[arg_index_start + (i + 2)]) else: print("No weight provided for ", argv[arg_index_start + i], file=sys.stderr) usage() sys.exit(2) i = i + 3 outtype_col_name = "Id" if DEBUG == "TRUE": print("*** DEBUG: " + sys.argv[0] + ": datatypes:", datatypes, file=sys.stderr) print("*** DEBUG: " + sys.argv[0] + ": dist_metrics:", dist_metrics, file=sys.stderr) print("*** DEBUG: " + sys.argv[0] + ": weights:", weights, file=sys.stderr) cumulative_prediction = 0 for datatype_num in range(len(datatypes)): if DEBUG == "TRUE": print("*** DEBUG: " + sys.argv[0] + ": datatype_num:", datatype_num, file=sys.stderr) print("*** DEBUG: " + sys.argv[0] + ": datatype:", datatypes[datatype_num], file=sys.stderr) features_file = feature_files_dir + "/" + datatype + ".tmp" if not os.path.isfile(features_file): print("Features file does not exist: ", features_file, file=sys.stderr) sys.exit(2) if DEBUG == "TRUE": debug_opt = "-d " knn_args = debug_opt + features_file + " " + outtype_dataset_file + " " + datatype_filenames[ datatype_num] + " " + dist_metrics[datatype_num] prediction = knn_classifier.main(knn_args.split()) if DEBUG == "TRUE": print("*** DEBUG: " + sys.argv[0] + ": prediction:", prediction, file=sys.stderr) cumulative_prediction = cumulative_prediction + prediction if DEBUG == "TRUE": print("*** DEBUG: " + sys.argv[0] + ": cumulative_prediction:", cumulative_prediction, file=sys.stderr) avg_prediction = cumulative_prediction / (len(datatypes)) if DEBUG == "TRUE": print("*** DEBUG: " + sys.argv[0] + ": avg_prediction:", avg_prediction, file=sys.stderr) if avg_prediction < 0.5: final_prediction = 0 else: final_prediction = 1 return (final_prediction)