def worker_to_visual_words(wind): meta = pickle.load(open('../data/traintest.pkl', 'rb')) all_imagenames = meta['all_imagenames'] point_method = 'Random' dictionary = pickle.load(open('dictionary%s.pkl' % point_method, 'rb')) filterBank = create_filterbank() for j in range(math.ceil(len(all_imagenames) / num_cores)): img_ind = j * num_cores + wind if img_ind < len(all_imagenames): img_name = all_imagenames[img_ind] print('converting %d-th image %s to visual words' % (img_ind, img_name)) image = cv.imread('../data/%s' % img_name) image = cv.cvtColor(image, cv.COLOR_BGR2RGB) # convert the image from bgr to rgb wordMap = get_visual_words(image, dictionary, filterBank) pickle.dump(wordMap, open('../data/%s_%s.pkl' % (img_name[:-4], point_method), 'wb'))
def get_dictionary(imgPaths, alpha, K, method): filterBank = create_filterbank() pixelResponses = np.zeros((alpha * len(imgPaths), 3 * len(filterBank))) point_count = 0 for i, path in enumerate(imgPaths): print('-- processing %d/%d' % (i, len(imgPaths))) image = cv.imread('../data/%s' % path) image = cv.cvtColor( image, cv.COLOR_BGR2RGB ) # convert the image from bgr to rgb, OpenCV use BGR by default # -----fill in your implementation here -------- filt_response = extract_filter_responses(image, filterBank) if method == 'Random': points = get_random_points(image, alpha) for idx in range(alpha): y = points[0][0][idx] x = points[0][1][idx] for n in range(len(filt_response)): get_pos_value = filt_response[n][y, x] pixelResponses[point_count, n] = get_pos_value point_count = point_count + 1 elif method == 'Harris': points = get_harris_points(image, alpha, k=0.05) for y, x in points: for n in range(len(filt_response)): get_pos_value = filt_response[n][y, x] pixelResponses[point_count, n] = get_pos_value point_count = point_count + 1 # ---------------------------------------------- dictionary = KMeans(n_clusters=K, random_state=0).fit(pixelResponses).cluster_centers_ return dictionary
import pickle import numpy as np from python.createFilterBank import create_filterbank from python.getImageDistance import getImageDistance traintest_file = open('../data/traintest.pkl', 'rb') traintest = pickle.load(traintest_file) traintest_file.close() test_imagenames = traintest['test_imagenames'] filterBank = create_filterbank() with open('dictionaryRandom.pkl', 'rb') as handle: dict_random = pickle.load(handle) with open('dictionaryHarris.pkl', 'rb') as handle: dict_harris = pickle.load(handle) with open('visionRandom.pkl', 'rb') as handle: train_random_histset = pickle.load(handle) with open('visionHarris.pkl', 'rb') as handle: train_harris_histset = pickle.load(handle) K = 100 rand_eu_acc = [] rand_chi_acc = [] harris_eu_acc = [] harris_chi_acc = []