def runSaliency(self): # get the saliency maps using the 3 implemented methods #rbd = psal.get_saliency_rbd(self.filename).astype('uint8') ft = psal.get_saliency_ft(self.filename).astype('uint8') mbd = psal.get_saliency_mbd(self.filename).astype('uint8') # often, it is desirable to have a binary saliency map binary_sal = psal.binarise_saliency_map(mbd,method='adaptive') img = cv2.imread(self.filename) cv2.imshow('img',img) #cv2.imshow('rbd',rbd) cv2.imshow('ft',ft) cv2.imshow('mbd',mbd) #openCV cannot display numpy type 0, so convert to uint8 and scale cv2.imshow('binary',255 * binary_sal.astype('uint8')) cv2.waitKey(0) cv2.destroyAllWindows()
def add_saliency_mbd(train=True): input_names = train_input_names if train else val_input_names while input_names: current_job_filename = input_names.pop() try: if os.path.isfile(current_job_filename): subfolder_basename_index = current_job_filename.find('\\') subfolder_basename_dir = current_job_filename[ subfolder_basename_index:] output_dir = (train_output_dir if train else val_output_dir) + subfolder_basename_dir[1:] os.makedirs(os.path.dirname(output_dir), exist_ok=True) # rgb = cv2.imread(infile_name, cv2.IMREAD_UNCHANGED) if os.path.isfile(output_dir): print('File already exists') else: mbd = psal.get_saliency_mbd(current_job_filename).astype( 'uint8') encode_mbd_to_pickle(output_dir, mbd) except: with open('D:/ILSVRC2012/saliency_adder_log.txt', 'a') as f: f.write(current_job_filename + '\n') print( 'Error while handling the image. Filename added to the log.' ) f.close()
def saliency_detection(item): try: mbd = psal.get_saliency_mbd([args.base_image_path + '/' + item['image_id']+'.jpg']) imsave(os.path.join(args.output_image_path, item['image_id']+'.jpg'), mbd) except numpy.linalg.linalg.LinAlgError: print('Unable to compute saliency for: ' + item['image_id']) return {'image_id': item['image_id']}
def calcualteSaliencyMaps(filename): # get the saliency maps using the 3 implemented methods rbd = psal.get_saliency_rbd(filename).astype('uint8') ft = psal.get_saliency_ft(filename).astype('uint8') mbd = psal.get_saliency_mbd(filename).astype('uint8') # often, it is desirable to have a binary saliency map mbd_binary_sal = psal.binarise_saliency_map(mbd,method='adaptive') ft_binary_sal = psal.binarise_saliency_map(ft,method='adaptive') rbd_binary_sal = psal.binarise_saliency_map(rbd,method='adaptive') return (rbd, ft, mbd, rbd_binary_sal, ft_binary_sal, mbd_binary_sal)
def saliency_5(frame, threshold=0.5): #candidate 3 #https://github.com/yhenon/pyimgsaliency #get the saliency maps using the 3 implemented methods mbd = psal.get_saliency_mbd(frame).astype('uint8') #Jianming Zhang, Stan Sclaroff, Zhe Lin, Xiaohui Shen, Brian Price and Radomír Měch. "Minimum Barrier Salient Object Detection at 80 FPS." #often, it is desirable to have a binary saliency map #check 'binarise.py' #the method can be either adaptive or fixed #binarise_saliency_map(saliency_map,method='adaptive',threshold=0.5) binary_mbd = psal.binarise_saliency_map(mbd,method='adaptive',threshold=threshold) return mbd, 255*binary_mbd.astype('uint8')
def __saliency_detection__(extract_all_images, path, root_dir): saliency_detection_output = os.path.join( root_dir + '/saliency_detection_output') split_path = str(path).split('/') saliency_detection_output = os.path.join(saliency_detection_output, split_path[-1]) if not os.path.exists(saliency_detection_output): os.makedirs(saliency_detection_output) else: shutil.rmtree(saliency_detection_output) os.makedirs(saliency_detection_output) for i, name in enumerate(extract_all_images): # print(os.path.join(saliency_detection_output, name)) mbd = psal.get_saliency_mbd( os.path.join(path, extract_all_images[i])) imsave(os.path.join(saliency_detection_output, name), mbd) return saliency_detection_output
import pyimgsaliency as psal import cv2 # path to the image filename = 'bird.jpg' # get the saliency maps using the 3 implemented methods rbd = psal.get_saliency_rbd(filename).astype('uint8') ft = psal.get_saliency_ft(filename).astype('uint8') mbd = psal.get_saliency_mbd(filename).astype('uint8') # often, it is desirable to have a binary saliency map binary_sal = psal.binarise_saliency_map(mbd,method='adaptive') img = cv2.imread(filename) cv2.imshow('img',img) cv2.imshow('rbd',rbd) cv2.imshow('ft',ft) cv2.imshow('mbd',mbd) #openCV cannot display numpy type 0, so convert to uint8 and scale cv2.imshow('binary',255 * binary_sal.astype('uint8')) cv2.waitKey(0)
def evaluate(img_dir, gt_dir): # valid_extensions = ['.jpg', '.png'] results_precision = {} results_recall = {} for filename in os.listdir(img_dir): if filename.endswith('.jpg') or filename.endswith('.png'): basename = os.path.splitext(filename)[0] gt_image_path = None if os.path.isfile(gt_dir + '/' + basename + '.png'): gt_image_path = gt_dir + '/' + basename + '.png' elif os.path.isfile(gt_dir + '/' + basename + '.jpg'): gt_image_path = gt_dir + '/' + basename + '.jpg' else: print('No match in gt directory for file {}, skipping.'.format( filename)) continue print(img_dir + '/' + filename) sal_image = pyimgsaliency.get_saliency_mbd( img_dir + '/' + filename).astype('uint8') gt_image = cv2.imread(gt_image_path, cv2.CV_LOAD_IMAGE_GRAYSCALE) cv2.imshow('sal', sal_image) cv2.imshow('img', gt_image) cv2.waitKey(0) if gt_image.shape != sal_image.shape: print('Size of image and GT image does not match, skipping') continue # precision, recall, thresholds = sklearn.metrics.precision_recall(y_true,y_scores) # precisions = {} # recalls = {} # # num_pixels = sal_image.shape[0] * sal_image.shape[1] # # p = np.count_nonzero(gt_image) # n = num_pixels - p # # for v in xrange(0,255): # culled = np.copy(sal_image) # culled[culled < v] = 0 # if np.count_nonzero(culled) == 0: # recall = 1 # sensitivity = 0 # else: # tp = np.count_nonzero(culled & gt_image) # recall = float(tp)/p # precision = float(tp) / np.count_nonzero(culled) # # precisions[v] = precision # recalls[v] = recall # # results_precision[filename] = precisions # results_recall[filename] = recalls # # x = [] # y = [] # for x1 in precisions: # #print x1 # x.append(precisions[x1]) # y.append(recalls[x1]) # plt.plot(x,y) # plt.show() # pdb.set_trace() return results_precision, results_recall
import numpy as np import pyimgsaliency as psal import cv2 if '__main__' == __name__: # path to the image filename = 'bird.jpg' # get the saliency maps using the 3 implemented methods rbd = psal.get_saliency_rbd(filename).astype(np.uint8) ft = psal.get_saliency_ft(filename).astype(np.uint8) mbd = psal.get_saliency_mbd(filename).astype(np.uint8) # often, it is desirable to have a binary saliency map binary_sal = psal.binarise_saliency_map(mbd, method='adaptive') img = cv2.imread(filename) cv2.imshow('img', img) cv2.imshow('rbd', rbd) cv2.imshow('ft', ft) cv2.imshow('mbd', mbd) # OpenCV cannot display numpy type 0, so convert to uint8 and scale cv2.imshow('binary', 255 * binary_sal.astype(np.uint8)) cv2.waitKey(0)
import pyimgsaliency as psal import cv2 # path to the image filename = '/Users/zmalik/image-similarity-fusion/bird.jpg' # get the saliency maps using the 3 implemented methods rbd = psal.get_saliency_rbd(filename).astype('uint8') ft = psal.get_saliency_ft(filename).astype('uint8') mbd = psal.get_saliency_mbd(filename).astype('uint8') # often, it is desirable to have a binary saliency map binary_sal = psal.binarise_saliency_map(mbd, method='adaptive') img = cv2.imread(filename) cv2.imshow('img', img) cv2.imshow('rbd', rbd) cv2.imshow('ft', ft) cv2.imshow('mbd', mbd) # openCV cannot display numpy type 0, so convert to uint8 and scale cv2.imshow('binary', 255 * binary_sal.astype('uint8')) cv2.waitKey(0)
def evaluate(img_dir,gt_dir,methods): valid_extensions = ['.jpg','.png'] results_precision = {} results_recall = {} for filename in os.listdir(img_dir): if filename.endswith(".jpg") or filename.endswith(".png"): basename = os.path.splitext(filename)[0] gt_image_path = None if os.path.isfile(gt_dir + '/' + basename + '.png'): gt_image_path = gt_dir + '/' + basename + '.png' elif os.path.isfile(gt_dir + '/' + basename + '.jpg'): gt_image_path = gt_dir + '/' + basename + '.jpg' else: print('No match in gt directory for file' + str(filename) + ', skipping.') continue print(img_dir + '/' + filename) sal_image = pyimgsaliency.get_saliency_mbd(img_dir + '/' + filename).astype('uint8') gt_image = cv2.imread(gt_image_path,cv2.CV_LOAD_IMAGE_GRAYSCALE) cv2.imshow('sal',sal_image) cv2.imshow('img',gt_image) cv2.waitKey(0) if gt_image.shape != sal_image.shape: print('Size of image and GT image does not match, skipping') continue #precision, recall, thresholds = sklearn.metrics.precision_recall(y_true,y_scores) ''' precisions = {} recalls = {} num_pixels = sal_image.shape[0] * sal_image.shape[1] p = np.count_nonzero(gt_image) n = num_pixels - p for v in xrange(0,255): culled = np.copy(sal_image) culled[culled < v] = 0 if np.count_nonzero(culled) == 0: recall = 1 sensitivity = 0 else: tp = np.count_nonzero(culled & gt_image) recall = float(tp)/p precision = float(tp) / np.count_nonzero(culled) precisions[v] = precision recalls[v] = recall results_precision[filename] = precisions results_recall[filename] = recalls x = [] y = [] for x1 in precisions: #print x1 x.append(precisions[x1]) y.append(recalls[x1]) plt.plot(x,y) plt.show() ''' #pdb.set_trace() return (results_precision,results_recall)
if filename.endswith('.jpg'): # Get RBD saliency map try: # Unknown error handler rbd = get_saliency_rbd('./input/dataset/MSRA-B/' + filename).astype('uint8') cv2.imwrite('./input/unsup_labels/rdb/' + filename[:-4] + '_ngt.png', rbd) except: with open('output/failed.txt', 'a+') as f: f.write(filename + '\n') # Get FT saliency map ft = get_saliency_ft('./input/dataset/MSRA-B/' + filename).astype('uint8') cv2.imwrite('./input/unsup_labels/ft/' + filename[:-4] + '_ngt.png', ft) # Get MBD saliency map mbd = get_saliency_mbd('./input/dataset/MSRA-B/' + filename).astype('uint8') cv2.imwrite('./input/unsup_labels/mbd/' + filename[:-4] + '_ngt.png', mbd) # Get HC saliency map hc = get_saliency_hc('./input/dataset/MSRA-B/' + filename).astype('uint8') cv2.imwrite('./input/unsup_labels/hc/' + filename[:-4] + '_ngt.png', hc) # Get RC saliency map rc = get_saliency_rc('./input/dataset/MSRA-B/' + filename).astype('uint8') cv2.imwrite('./input/unsup_labels/rc/' + filename[:-4] + '_ngt.png', rc) else: path = "input/" dataset_name = "unsup_labels" if not os.path.isdir(path + dataset_name): print("[==> Extracting unsupervised labels ...") with zipfile.ZipFile(path + dataset_name + ".zip", 'r') as zip_ref:
# binary_sal = psal.binarise_saliency_map(rbd*255,method='adaptive') # cv2.imshow('rbd',rbd) # import pdb; pdb.set_trace() # cv2.imshow('ft',binary_sal) # cv2.waitKey(0) # mbd = psal.get_saliency_mbd(filename).astype('uint8') PATH = '/home/victor/clipped_live_dice_nodice/clipped_live_dice/' fns = os.listdir(PATH) for fn in [file for file in fns if file.endswith('.png')]: outfn = 'result-' + P.basename(fn) print('making: %s' % outfn) im = cv2.imread(P.join(PATH, fn)) mbd = psal.get_saliency_mbd(im).astype('uint8') # often, it is desirable to have a binary saliency map binary_sal = psal.binarise_saliency_map(mbd, method='adaptive') plt.subplot(1, 2, 1), plt.imshow(im) plt.title('Input image') plt.subplot(1, 2, 2), plt.imshow(binary_sal) plt.title('Mask') plt.savefig('/home/victor/clipped_live_dice_nodice/out/%s' % outfn) # # cv2.imshow('img',img) # # cv2.imshow('rbd',rbd) # # cv2.imshow('ft',ft) # # cv2.imshow('mbd',mbd)
a[i] += 1 def encode_mbd_to_pickle(output_dir, mbd): # output_directory.jpeg -> output_directory.pickle filename = output_dir[:-5] + '.pickle' if os.path.isfile(filename): print('File already exists') else: with open(filename, 'wb') as f_out: pickle.dump(mbd, f_out) f_out.close() print('Successfully serialized saliency map (mbd) to:', filename) if __name__ == "__main__": n = 10000000 a = np.ones(n, dtype=np.float64) b = np.ones(n, dtype=np.float32) start = timer() func(a) print("With GPU (@jit):", timer() - start) start = timer() func2(a) print("With GPU (@jit(nopython=True)):", timer() - start) mbd = psal.get_saliency_mbd(demo_img) encode_mbd_to_pickle(os.getcwd() + '\\' + demo_img, mbd)
def evaluate(img_dir, gt_dir, methods): valid_extensions = ['.jpg', '.png'] results_precision = {} results_recall = {} for filename in os.listdir(img_dir): if filename.endswith(".jpg") or filename.endswith(".png"): basename = os.path.splitext(filename)[0] gt_image_path = None if os.path.isfile(gt_dir + '/' + basename + '.png'): gt_image_path = gt_dir + '/' + basename + '.png' elif os.path.isfile(gt_dir + '/' + basename + '.jpg'): gt_image_path = gt_dir + '/' + basename + '.jpg' else: print('No match in gt directory for file' + str(filename) + ', skipping.') continue print(img_dir + '/' + filename) sal_image = pyimgsaliency.get_saliency_mbd( img_dir + '/' + filename).astype('uint8') gt_image = cv2.imread(gt_image_path, cv2.CV_LOAD_IMAGE_GRAYSCALE) cv2.imshow('sal', sal_image) cv2.imshow('img', gt_image) cv2.waitKey(0) if gt_image.shape != sal_image.shape: print('Size of image and GT image does not match, skipping') continue #precision, recall, thresholds = sklearn.metrics.precision_recall(y_true,y_scores) ''' precisions = {} recalls = {} num_pixels = sal_image.shape[0] * sal_image.shape[1] p = np.count_nonzero(gt_image) n = num_pixels - p for v in xrange(0,255): culled = np.copy(sal_image) culled[culled < v] = 0 if np.count_nonzero(culled) == 0: recall = 1 sensitivity = 0 else: tp = np.count_nonzero(culled & gt_image) recall = float(tp)/p precision = float(tp) / np.count_nonzero(culled) precisions[v] = precision recalls[v] = recall results_precision[filename] = precisions results_recall[filename] = recalls x = [] y = [] for x1 in precisions: #print x1 x.append(precisions[x1]) y.append(recalls[x1]) plt.plot(x,y) plt.show() ''' #pdb.set_trace() return (results_precision, results_recall)