def load_bow(booksize, files, mute=False): bow = np.zeros([len(files), booksize]) cnt = -1 for impath in files: cnt = cnt + 1 if not mute: print '\r' + str(cnt) + '/' + str(len(files)) + '): ' + impath, filpat, filnam, filext = tools.fileparts(impath) filpat2, filnam2, filext2 = tools.fileparts(filpat) bow[cnt, :] = week56.load_bow('../../data/bow_objects/codebook_' + str(booksize) + '/' + filnam2 + '/' + filnam + '.pkl') if not mute: print '' return bow
def compute_sift(impath, edge_thresh = 10, peak_thresh = 5): params = '--edge-thresh ' + str(edge_thresh) + ' --peak-thresh ' + str(peak_thresh) im1 = Image.open(impath).convert('L') filpat1, filnam1, filext1 = tools.fileparts(impath) temp_im1 = 'tmp_' + filnam1 + '.pgm' im1.save(temp_im1) import struct is_64bit = struct.calcsize('P') * 8 == 64 if platform.system() == 'Windows' and is_64bit == True: sift_exec = '..\\..\\external\\vlfeat-0.9.17\\bin\\win64\\sift.exe' command = sift_exec + ' \'' + os.getcwd() + '\\' + temp_im1 + '\' --output \'' + os.getcwd() + '\\' + filnam1 + '.sift.output' + '\' ' + params elif platform.system() == 'Windows' and is_64bit == False: sift_exec = '..\\..\\external\\vlfeat-0.9.17\\bin\\win32\\sift.exe' command = sift_exec + ' \'' + os.getcwd() + '\\' + temp_im1 + '\' --output \'' + os.getcwd() + '\\' + filnam1 + '.sift.output' + '\' ' + params elif platform.system() == 'Linux': sift_exec = '..//..//external//vlfeat-0.9.17//bin//glnxa64//sift' command = sift_exec + ' \'' + os.getcwd() + '//' + temp_im1 + '\' --output \'' + os.getcwd() + '//' + filnam1 + '.sift.output' + '\' ' + params elif platform.system() == 'Darwin': sift_exec = '..//..//external//vlfeat-0.9.17//bin//maci64//sift' command = sift_exec + ' \'' + os.getcwd() + '//' + temp_im1 + '\' --output \'' + os.getcwd() + '//' + filnam1 + '.sift.output' + '\' ' + params os.system(command) frames, sift = read_sift_from_file(filnam1 + '.sift.output') os.remove(temp_im1) os.remove(filnam1 + '.sift.output') return frames, sift
def compute_sift(impath, edge_thresh=10, peak_thresh=5): global CACHING, WRITTEN_TO_CACHE, RENEW_CACHE params = ('--edge-thresh ' + str(edge_thresh) + ' --peak-thresh ' + str(peak_thresh)) im1 = Image.open(impath).convert('L') if CACHING and not RENEW_CACHE: if im1 in cache['compute_sift']: print "retrieving from cache: 'compute_sift'" return cache['compute_sift'][hash(im1)] filpat1, filnam1, filext1 = tools.fileparts(impath) temp_im1 = 'tmp_' + filnam1 + '.pgm' im1.save(temp_im1) import struct is_64bit = struct.calcsize('P') * 8 == 64 if platform.system() == 'Windows' and is_64bit: sift_exec = '..\\..\\external\\vlfeat-0.9.17\\bin\\win64\\sift.exe' command = sift_exec + ' \'' + os.getcwd() + '\\' + temp_im1 + '\' --output \'' + os.getcwd() + '\\' + filnam1 + '.sift.output' + '\' ' + params elif platform.system() == 'Windows' and not is_64bit: sift_exec = '..\\..\\external\\vlfeat-0.9.17\\bin\\win32\\sift.exe' command = sift_exec + ' \'' + os.getcwd() + '\\' + temp_im1 + '\' --output \'' + os.getcwd() + '\\' + filnam1 + '.sift.output' + '\' ' + params elif platform.system() == 'Linux': sift_exec = '..//..//external//vlfeat-0.9.17//bin//glnxa64//sift' command = sift_exec + ' \'' + os.getcwd() + '//' + temp_im1 + '\' --output \'' + os.getcwd() + '//' + filnam1 + '.sift.output' + '\' ' + params elif platform.system() == 'Darwin': sift_exec = '..//..//external//vlfeat-0.9.17//bin//maci64//sift' command = sift_exec + ' \'' + os.getcwd() + '//' + temp_im1 + '\' --output \'' + os.getcwd() + '//' + filnam1 + '.sift.output' + '\' ' + params os.system(command) frames, sift = read_sift_from_file(filnam1 + '.sift.output') os.remove(temp_im1) os.remove(filnam1 + '.sift.output') if CACHING: cache['compute_sift'][im1] = frames, sift WRITTEN_TO_CACHE = True return frames, sift
#### # Get object data files, labels, label_names, unique_labels, trainset, testset = week56.get_objects_filedata() #### ### # Load Bag-Of-Words ### C = 100 bow = np.zeros([len(files), C]) cnt = -1 for impath in files: cnt = cnt + 1 print str(cnt) + '/' + str(len(files)) + '): ' + impath filpat, filnam, filext = tools.fileparts(impath) filpat2, filnam2, filext2 = tools.fileparts(filpat) bow[cnt, :] = week56.load_bow('../../data/bow_objects/codebook_' + str(C) + '/' + filnam2 + '/' + filnam + '.pkl') ############################################################################### # Q1: IMPLEMENT HERE kNN CLASSIFIER. ############################################################################### # Normalize Bag-Of-Words for i in range(len(files)): bow[i] = tools.normalizeL1(bow[i]) # k-NN Classifier dist = np.zeros([len(testset),len(trainset)]) for j in range(len(testset)): for k in range(len(trainset)):
Results['ItemDuration'] = Results['ItemDuration'] / ts # Item # Results['Item'] = pl.arange(1, len(Results['Time']) + 1) nitems = len(Results['Item']) Results['Activation'] = pl.zeros(nitems) Results['Amplitude'] = pl.zeros(nitems) Results['MeanHR'] = pl.zeros(nitems) Results['StdHR'] = pl.zeros(nitems) onset = EDA['Onset'] * SamplingRate beat = copy(BVP['Onset']) # beat=BVP['ZeroDerivative']/DownSampling beat /= DownSampling fparts = tls.fileparts(fname) rdir = tls.fullfile('../results', fparts[-2]) if not os.path.isdir(rdir): os.mkdir(rdir) t = pl.arange(len(EDA['Signal'])) / ts / 60. item = 0 for i in pl.arange(len(idx)): blbl = 'Block' + str(i + 1) pl.figure() y = EDA['Signal'][Results[blbl][0]:Results[blbl][-1]] ti = t[Results[blbl][0]:Results[blbl][-1]]