def test_qs1():
    print('GET DB')
    db_text = get_text()
    db_images = [[cv2.imread(item)]
                 for item in sorted(glob(os.path.join(db, "*.jpg")))]
    print('done')

    # -- READ -- #
    print('READ FILES')
    with open(res_root + os.sep + 'denoised.pkl', 'rb') as ff:
        qs1_images = pickle.load(ff)
    with open(res_root + os.sep + 'qs1_bbox.pkl', 'rb') as ff:
        qs1_bbox = pickle.load(ff)
    with open(res_root + os.sep + 'qs1_mask.pkl', 'rb') as ff:
        qs1_mask = pickle.load(ff)
    print('done')

    # -- TRANSFORM -- #
    print('computing combined descriptors')
    db_desc_trans = TransformDescriptor(db_images, None, None)
    db_desc_trans.compute_descriptors(transform_type='lbp')
    qs_desc_trans = TransformDescriptor(qs1_images, qs1_mask, None)
    qs_desc_trans.compute_descriptors(transform_type='lbp')
    # -- SEARCH -- #
    qs_searcher = Searcher(db_desc_trans.result, qs_desc_trans.result)
    qs_searcher.search(limit=5)
    print("Done.")
    qs_eval = EvaluateDescriptors(qs_searcher.result,
                                  os.path.join(qs1_w3, 'gt_corresps.pkl'))
    qs_eval.compute_mapatk(limit=1)
    print('DESC MAP1: [' + str(qs_eval.score) + ']')
    qs_eval.compute_mapatk(limit=5)
    print('DESC MAP5: [' + str(qs_eval.score) + ']')
    print('done')

    # -- TEXT -- #
    print('computing text descriptors')
    qs_desc = TextDescriptor(qs1_images, qs1_bbox)
    qs_desc.compute_descriptors()
    # -- SEARCH -- #
    qs_searcher = SearcherText(db_text, qs_desc.result)
    qs_searcher.search(limit=5)
    print("Done.")
    qs_eval = EvaluateDescriptors(qs_searcher.result,
                                  os.path.join(qs1_w3, 'gt_corresps.pkl'))
    qs_eval.compute_mapatk(limit=1)
    print('DESC MAP1: [' + str(qs_eval.score) + ']')
    qs_eval.compute_mapatk(limit=5)
    print('DESC MAP5: [' + str(qs_eval.score) + ']')
    print('done')

    # -- COLOR -- #
    print('computing color descriptors')
    db_desc_col = SubBlockDescriptor(db_images, None)
    db_desc_col.compute_descriptors()
    qs_desc_col = SubBlockDescriptor(qs1_images, qs1_mask)
    qs_desc_col.compute_descriptors()
    # -- SEARCH -- #
    qs_searcher = Searcher(db_desc_col.result, qs_desc_col.result)
    qs_searcher.search(limit=5)
    print("Done.")
    qs_eval = EvaluateDescriptors(qs_searcher.result,
                                  os.path.join(qs1_w3, 'gt_corresps.pkl'))
    qs_eval.compute_mapatk(limit=1)
    print('DESC MAP1: [' + str(qs_eval.score) + ']')
    qs_eval.compute_mapatk(limit=5)
    print('DESC MAP5: [' + str(qs_eval.score) + ']')
    print('done')

    # -- SEARCH -- #
    qs_searcher = SearcherCombined(db_desc_col.result,
                                   qs_desc_col.result,
                                   db_desc_trans.result,
                                   qs_desc_trans.result,
                                   db_text,
                                   qs_desc.result,
                                   use_text=True)
    db_desc_col.clear_memory()
    qs_desc_col.clear_memory()
    db_desc_trans.clear_memory()
    qs_desc_trans.clear_memory()
    qs_searcher.search(limit=5)
    print("Done.")
    qs_eval = EvaluateDescriptors(qs_searcher.result,
                                  os.path.join(qs1_w3, 'gt_corresps.pkl'))
    qs_eval.compute_mapatk(limit=1)
    print('DESC MAP1: [' + str(qs_eval.score) + ']')
    qs_eval.compute_mapatk(limit=5)
    print('DESC MAP5: [' + str(qs_eval.score) + ']')
    print('done')
Exemple #2
0
def main_qs1w3(evaluate=False):
	print("QSD1_W3")
	print("Reading Images...")
	db_text = get_text()
	denoiser = Denoise(qs1_w3)
	db_images = [[cv2.imread(item)] for item in sorted(glob(os.path.join(db,"*.jpg")))]
	print("Denoising Images...")
	denoiser.median_filter(3)
	qs_images = denoiser.tv_bregman(weight=0.01,max_iter=1000,eps=0.001,isotropic=True)
	# cv2.imwrite(r"C:\Users\PC\Documents\Roger\Master\M1\Project\Week3\tests_folder\testdenoise.png",qs_images[0][0])
	# qs_images = [[cv2.imread(item)] for item in sorted(glob(os.path.join(qs1_w3,"*.jpg")))] # No denoising
	print("Done.")

	print("Obtaining textbox masks for each painting...")
	query_mask = []
	query_bbox = []
	for ind,img in enumerate(qs_images):
		print(ind,"of",len(qs_images))
		for paint in img:
			mask, textbox = TextBoxRemoval(paint)
			bbox = [textbox[0][1],textbox[0][0],textbox[1][1],textbox[1][0]]
			query_mask.append([mask])
			query_bbox.append([bbox])
			cv2.imwrite(os.path.join(res_root,'QS1W3','{0:05d}.png'.format(ind)),mask)
			# cv2.imwrite(os.path.join(tests_path,'{0:05d}_mask.png'.format(ind)),mask)
	print("Done.")
	# input("Stop execution...")

	if evaluate:
		eval_iou = EvaluateIoU(query_bbox,os.path.join(qs1_w3,"text_boxes.pkl"))
		eval_iou.compute_iou()
		print("Bbox masks IoU:",eval_iou.score)

	# -- SAVE BBOXES -- #
	print("Writing final bboxs...")
	with open(os.path.join(res_root,"qs1_bbox.pkl"),'wb') as file:
		pickle.dump(query_bbox,file)
	print("Done.")

	# -- DESCRIPTORS -- #
	# -- COLOR -- #
	print('computing color descriptors')
	db_desc_col = SubBlockDescriptor(db_images,None)
	db_desc_col.compute_descriptors()
	qs_desc_col = SubBlockDescriptor(qs_images,query_mask)
	qs_desc_col.compute_descriptors()
	# -- SEARCH -- #
	qs_searcher = Searcher(db_desc_col.result,qs_desc_col.result)
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs1_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for color descriptors ", map_at_1)
		print("MAP@5 for color descriptors ", map_at_5)
	print("Done.")

	print("Writing color desc...")
	with open(os.path.join(res_root,"qs1_color_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- TRANSFORM -- #
	print('Obtaining transform descriptors.')
	db_desc_trans = TransformDescriptor(db_images,None,None)
	db_desc_trans.compute_descriptors(transform_type='hog')
	qs_desc_trans = TransformDescriptor(qs_images,query_mask,None)
	qs_desc_trans.compute_descriptors(transform_type='hog')
	# -- SEARCH -- #
	qs_searcher = Searcher(db_desc_trans.result,qs_desc_trans.result)
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs1_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for transform descriptors ", map_at_1)
		print("MAP@5 for transform descriptors ", map_at_5)
	print("Done.")

	print("Writing transform desc...")
	with open(os.path.join(res_root,"qs1_transform_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- TEXT -- #
	print('computing text descriptors')
	qs_desc_text = TextDescriptor(qs_images,query_bbox)
	qs_desc_text.compute_descriptors()
	save_text(qs_desc_text.result,'qs1')
	# -- SEARCH -- #
	qs_searcher = SearcherText(db_text,qs_desc_text.result)
	qs_desc_text.clear_memory()
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs1_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for text descriptors with levensthein ", map_at_1)
		print("MAP@5 for text descriptors with levensthein ", map_at_5)
	print("Done.")

	print("Writing text desc...")
	with open(os.path.join(res_root,"qs1_text_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- COMBINED-- #
	print('computing combined descriptors without text')
	# -- SEARCH -- #
	qs_searcher = SearcherCombined(db_desc_col.result,qs_desc_col.result,db_desc_trans.result,qs_desc_trans.result, db_text, qs_desc_text.result, False)
	db_desc_col.clear_memory()
	qs_desc_col.clear_memory()
	db_desc_trans.clear_memory()
	qs_desc_trans.clear_memory()
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs1_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for combined descriptors without text ", map_at_1)
		print("MAP@5 for combined descriptors without text ", map_at_5)
	print("Done.")

	print("Writing combined desc...")
	with open(os.path.join(res_root,"qs1_combined_without_text_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- COMBINED-- #
	print('computing combined descriptors with text')
	# -- SEARCH -- #
	qs_searcher = SearcherCombined(db_desc_col.result,qs_desc_col.result,db_desc_trans.result,qs_desc_trans.result, db_text, qs_desc_text.result, True)
	db_desc_col.clear_memory()
	qs_desc_col.clear_memory()
	db_desc_trans.clear_memory()
	qs_desc_trans.clear_memory()
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs1_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for combined descriptors with text ", map_at_1)
		print("MAP@5 for combined descriptors with text ", map_at_5)
	print("Done.")

	print("Writing combined desc...")
	with open(os.path.join(res_root,"qs1_combined_with_text_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")
Exemple #3
0
def main_qs2w3(evaluate=False):
	# -- GET IMAGES -- #
	print("Denoising Images...")
	folder_path = qs2_w3
	db_text = get_text()
	denoiser = Denoise(folder_path)
	denoiser.median_filter(3)
	qs_images = denoiser.tv_bregman(weight=0.01,max_iter=1000,eps=0.001,isotropic=True)
	print("Done.")
	print("Obtaining list of paintings...")
	img2paintings = getListOfPaintings(qs_images,"EDGES")
	db_images = []
	for db_path in sorted(glob(os.path.join(db,"*.jpg"))):
		db_images.append([cv2.imread(db_path)])
	print("Done.")

	print("Obtaining background masks for each painting...")
	img2paintings_mask = []
	for ind,img in enumerate(img2paintings):
		print(ind,"of",len(img2paintings))
		img2paintings_mask.append([])
		for painting in img:
			mask, mean_points = BackgroundMask4(painting)
			img2paintings_mask[-1].append({"painting":painting,"mask":mask,"mean_points":mean_points})
			"""UNCOMMENT LINE TO PRODUCE THE MASK TO UPLOAD TO THE SERVER"""
		cv2.imwrite(os.path.join(res_root,"QS2W3","{0:05d}.png".format(ind)),np.concatenate([item["mask"] for item in img2paintings_mask[-1]],axis=1))
	print("Done.")

	print("Obtaining textbox masks for each painting...")
	img2paintings_items = []
	img2paintings_bboxs = []
	for ind,img in enumerate(img2paintings_mask):
		print(ind,"of",len(img2paintings_mask))
		img2paintings_items.append([])
		for painting_items in img:
			painting_masked = painting_items["painting"][painting_items["mean_points"]["top"]:painting_items["mean_points"]["bottom"],painting_items["mean_points"]["left"]:painting_items["mean_points"]["right"],:]
			mask, textbox = TextBoxRemoval(painting_masked)
			bbox_mask = np.zeros(shape=(painting_items["painting"].shape[0],painting_items["painting"].shape[1]))
			bbox_mask[painting_items["mean_points"]["top"]:painting_items["mean_points"]["bottom"],painting_items["mean_points"]["left"]:painting_items["mean_points"]["right"]] = mask
			bbox = [textbox[0][1],textbox[0][0],textbox[1][1],textbox[1][0]]
			bbox[1] = bbox[1] + painting_items["mean_points"]["top"]
			bbox[3] = bbox[3] + painting_items["mean_points"]["top"]
			bbox[0] = bbox[0] + painting_items["mean_points"]["left"]
			bbox[2] = bbox[2] + painting_items["mean_points"]["left"]
			bbox_detected = False if np.mean(mask) == 255 else True
			img2paintings_items[-1].append({"fg_mask":painting_items["mask"],
											"mean_points":painting_items["mean_points"],
											"bbox_mask":bbox_mask,
											"bbox":bbox,
											"bbox_detected":bbox_detected})
	print("Done.")

	print("Combining masks in one picture + adapting bboxes...")
	final_masks = []
	img2paintings_final_mask = []
	img2paintings_fg_bboxs = []
	final_bboxs = []
	for ind,img in enumerate(img2paintings_items):
		print(ind,"of",len(img2paintings_items))
		to_concatenate = []
		fg_bboxs = []
		bboxs = []
		for ind2,painting_items in enumerate(img):
			total_mask = painting_items["fg_mask"]
			if painting_items["bbox_detected"]:
				total_mask[painting_items["bbox"][1]:painting_items["bbox"][3],painting_items["bbox"][0]:painting_items["bbox"][2]] = 0
			to_concatenate.append(total_mask)
			if ind2 == 0:
				bboxs.append(painting_items["bbox"])
			else:
				missing_size = 0
				for item in to_concatenate[:-1]:
					missing_size += item.shape[1]
				bbox = painting_items["bbox"]
				bbox[0] += missing_size
				bbox[2] += missing_size
				bboxs.append(bbox)
			fg_bboxs.append(painting_items["mean_points"])
		img2paintings_fg_bboxs.append(fg_bboxs)
		img2paintings_final_mask.append(to_concatenate)
		final_mask = np.concatenate(to_concatenate,axis=1)
		final_masks.append(final_mask)
		final_bboxs.append(bboxs)
	print("Done.")

	if evaluate:
		eval_iou = EvaluateIoU(final_bboxs,os.path.join(qs2_w3,"text_boxes.pkl"))
		eval_iou.compute_iou()
		print("Bbox masks IoU:",eval_iou.score)

	print("Writing final bboxs...")
	with open(os.path.join(res_root,"qs2_bbox.pkl"),"wb") as f:
		pickle.dump(final_bboxs,f)
	print("Done.")

	print("Writing final masks...")
	for ind,final_mask in enumerate(final_masks):
		cv2.imwrite(os.path.join(res_root,"QS2W3","{0:05d}.png".format(ind)),final_mask)
	print("Done.")

	print("Obtaining descriptors.")

	# -- DESCRIPTORS -- #
	# -- COLOR -- #
	print('computing color descriptors')
	db_desc_col = SubBlockDescriptor(db_images,None)
	db_desc_col.compute_descriptors()
	qs_desc_col = SubBlockDescriptor(img2paintings,img2paintings_final_mask)
	qs_desc_col.compute_descriptors()
	# -- SEARCH -- #
	qs_searcher = Searcher(db_desc_col.result,qs_desc_col.result)
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs2_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for color descriptors ", map_at_1)
		print("MAP@5 for color descriptors ", map_at_5)
	print("Done.")

	print("Writing color desc...")
	with open(os.path.join(res_root,"qs2_color_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- TRANSFORM -- #
	print('Obtaining transform descriptors.')
	db_desc_trans = TransformDescriptor(db_images,None,None)
	db_desc_trans.compute_descriptors(transform_type='hog')
	qs_desc_trans = TransformDescriptor(img2paintings,img2paintings_final_mask,img2paintings_fg_bboxs)
	qs_desc_trans.compute_descriptors(transform_type='hog')
	# -- SEARCH -- #
	qs_searcher = Searcher(db_desc_trans.result,qs_desc_trans.result)
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs2_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for transform descriptors ", map_at_1)
		print("MAP@5 for transform descriptors ", map_at_5)
	print("Done.")

	print("Writing color desc...")
	with open(os.path.join(res_root,"qs2_transform_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- TEXT -- #
	print('computing text descriptors')
	qs_desc_text = TextDescriptor(img2paintings,img2paintings_fg_bboxs)
	qs_desc_text.compute_descriptors()
	save_text(qs_desc_text.result,'qs2')
	# -- SEARCH -- #
	qs_searcher = SearcherText(db_text,qs_desc_text.result)
	qs_desc_text.clear_memory()
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs2_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for text descriptors with levenshtein ", map_at_1)
		print("MAP@5 for text descriptors with levenshtein", map_at_5)
	print("Done.")

	print("Writing text desc...")
	with open(os.path.join(res_root,"qs2_text_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- COMBINED-- #
	print('computing combined descriptors without text')
	# -- SEARCH -- #
	qs_searcher = SearcherCombined(db_desc_col.result,qs_desc_col.result,db_desc_trans.result,qs_desc_trans.result, db_text, qs_desc_text.result, False)
	db_desc_col.clear_memory()
	qs_desc_col.clear_memory()
	db_desc_trans.clear_memory()
	qs_desc_trans.clear_memory()
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs2_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for combined descriptors without text ", map_at_1)
		print("MAP@5 for combined descriptors without text ", map_at_5)
	print("Done.")

	print("Writing combined desc...")
	with open(os.path.join(res_root,"qs2_combined_without_text_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")

	# -- COMBINED-- #
	print('computing combined descriptors with text')
	# -- SEARCH -- #
	print(db_text)
	print(qs_desc_text.result)
	qs_searcher = SearcherCombined(db_desc_col.result,qs_desc_col.result,db_desc_trans.result,qs_desc_trans.result, db_text, qs_desc_text.result, True)
	db_desc_col.clear_memory()
	qs_desc_col.clear_memory()
	db_desc_trans.clear_memory()
	qs_desc_trans.clear_memory()
	qs_searcher.search(limit=10)
	if evaluate:
		evaluator = EvaluateDescriptors(qs_searcher.result, qs2_corresps_path)
		map_at_1 = evaluator.compute_mapatk(1)
		map_at_5 = evaluator.compute_mapatk(5)
		print("MAP@1 for combined descriptors with text ", map_at_1)
		print("MAP@5 for combined descriptors with text ", map_at_5)
	print("Done.")

	print("Writing combined desc...")
	with open(os.path.join(res_root,"qs2_combined_with_text_result.pkl"),'wb') as file:
		pickle.dump(qs_searcher.result,file)
	print("Done.")
Exemple #4
0
def main_qs2():
    # -- GET IMAGES -- #
    folder_path = qs2_w2
    img_paths = sorted(glob(folder_path + os.sep + '*.jpg'))
    print("Obtaining list of paintings...")
    img2paintings = getListOfPaintings(folder_path, "EDGES")
    db_images = []
    for db_path in sorted(glob(db + os.sep + '*.jpg')):
        db_images.append([cv2.imread(db_path)])
    print("Done.")

    print("Obtaining background masks for each painting...")
    img2paintings_mask = []
    for ind, (img_path, img) in enumerate(zip(img_paths, img2paintings)):
        print(ind, "of", len(img2paintings))
        img2paintings_mask.append([])
        for painting in img:
            mask, mean_points = BackgroundMask4(painting)
            img2paintings_mask[-1].append({
                "painting": painting,
                "mask": mask,
                "mean_points": mean_points
            })
        cv2.imwrite(
            res_root + os.sep + 'QS2W2/{0:05d}.png'.format(ind),
            np.concatenate([item["mask"] for item in img2paintings_mask[-1]],
                           axis=1))
    print("Done.")

    print("Obtaining textbox masks for each painting...")
    img2paintings_items = []
    img2paintings_bboxs = []
    for ind, img in enumerate(img2paintings_mask):
        print(ind, "of", len(img2paintings_mask))
        img2paintings_items.append([])
        for painting_items in img:
            painting_masked = painting_items["painting"][
                painting_items["mean_points"]["top"]:
                painting_items["mean_points"]["bottom"],
                painting_items["mean_points"]["left"]:
                painting_items["mean_points"]["right"], :]
            mask, textbox = TextBoxRemoval(painting_masked)
            bbox_mask = np.zeros(shape=(painting_items["painting"].shape[0],
                                        painting_items["painting"].shape[1]))
            bbox_mask[painting_items["mean_points"]["top"]:
                      painting_items["mean_points"]["bottom"],
                      painting_items["mean_points"]["left"]:
                      painting_items["mean_points"]["right"]] = mask
            bbox = [textbox[0][1], textbox[0][0], textbox[1][1], textbox[1][0]]
            bbox[1] = bbox[1] + painting_items["mean_points"]["top"]
            bbox[3] = bbox[3] + painting_items["mean_points"]["top"]
            bbox[0] = bbox[0] + painting_items["mean_points"]["left"]
            bbox[2] = bbox[2] + painting_items["mean_points"]["left"]
            img2paintings_items[-1].append({
                "fg_mask": painting_items["mask"],
                "bbox_mask": bbox_mask,
                "bbox": bbox
            })
    print("Done.")

    print("Combining masks in one picture + adapting bboxes...")
    final_masks = []
    img2paintings_final_mask = []
    final_bboxs = []
    for ind, img in enumerate(img2paintings_items):
        print(ind, "of", len(img2paintings_items))
        to_concatenate = []
        bboxs = []
        for ind2, painting_items in enumerate(img):
            total_mask = painting_items["fg_mask"]
            total_mask[painting_items["bbox"][1]:painting_items["bbox"][3],
                       painting_items["bbox"][0]:painting_items["bbox"][2]] = 0
            to_concatenate.append(total_mask)
            if ind2 == 0:
                bboxs.append(painting_items["bbox"])
            else:
                missing_size = 0
                for item in to_concatenate[:-1]:
                    missing_size += item.shape[1]
                bbox = painting_items["bbox"]
                bbox[0] += missing_size
                bbox[2] += missing_size
                bboxs.append(bbox)
        img2paintings_final_mask.append(to_concatenate)
        final_mask = np.concatenate(to_concatenate, axis=1)
        final_masks.append(final_mask)
        final_bboxs.append(bboxs)
    print("Done.")

    print("Writing final bboxs...")
    with open(res_root + os.sep + 'qs2_bbox.pkl', "wb") as f:
        pickle.dump(final_bboxs, f)
    print("Done.")

    print("Writing final masks...")
    # for ind,final_mask in enumerate(final_masks):
    #    cv2.imwrite(res_root+os.sep+'QS2W2/{0:05d}.png'.format(ind),final_mask)
    print("Done.")

    print('Obtaining descriptors.')
    # -- DESCRIPTORS -- #
    db_desc = SubBlockDescriptor(db_images, None, flag=False)
    db_desc.compute_descriptors(grid_blocks=[8, 8],
                                quantify=[32, 8, 8],
                                color_space='hsv')
    q2_desc = SubBlockDescriptor(img2paintings, img2paintings_final_mask)
    q2_desc.compute_descriptors(grid_blocks=[8, 8],
                                quantify=[32, 8, 8],
                                color_space='hsv')

    # -- SEARCH -- #
    q2_searcher = Searcher(db_desc.result, q2_desc.result)
    db_desc.clear_memory()
    q2_desc.clear_memory()
    q2_searcher.search(limit=10)
    print("Done.")

    # -- SAVE RESULTS -- #
    print('Save results')
    with open(res_root + os.sep + 'qs2_result.pkl', 'wb') as f:
        pickle.dump(q2_searcher.result, f)
    print("Done.")
Exemple #5
0
def main_qs1():

    # -- GET IMAGES -- #
    print("Obtaining list of paintings...")
    query_images = []
    db_images = []
    for db_path in sorted(glob(db + os.sep + '*.jpg')):
        db_images.append([cv2.imread(db_path)])
    for qimg_paths in sorted(glob(qs1_w2 + os.sep + '*.jpg')):
        query_images.append([cv2.imread(qimg_paths)])
    print("Done.")

    # -- REMOVE TEXT and GENERATE MASK -- #
    print("Obtaining textbox masks for each painting...")
    query_mask = []
    query_bbox = []
    for ind, img in enumerate(query_images):
        print(ind, "of", len(query_images))
        for paint in img:
            mask, textbox = TextBoxRemoval(paint)
            bbox = [textbox[0][1], textbox[0][0], textbox[1][1], textbox[1][0]]
            query_mask.append([mask])
            query_bbox.append([bbox])
            cv2.imwrite(res_root + os.sep + 'QS1W2/{0:05d}.png'.format(ind),
                        mask)
    print("Done.")

    # -- SAVE BBOXES -- #
    print("Writing final bboxs...")
    with open(os.path.join(res_root, "qs1_bbox.pkl"), 'wb') as file:
        pickle.dump(query_bbox, file)
    print("Done.")

    # -- DESCRIPTORS -- #
    print('Obtaining descriptors.')
    db_desc = SubBlockDescriptor(db_images, None, flag=False)
    db_desc.compute_descriptors(grid_blocks=[8, 8],
                                quantify=[32, 8, 8],
                                color_space='hsv')
    q1_desc = SubBlockDescriptor(query_images, query_mask)
    q1_desc.compute_descriptors(grid_blocks=[8, 8],
                                quantify=[32, 8, 8],
                                color_space='hsv')

    # -- SEARCH -- #
    q1_searcher = Searcher(db_desc.result, q1_desc.result)
    db_desc.clear_memory()
    q1_desc.clear_memory()
    q1_searcher.search(limit=10)
    print("Done.")

    # -- SAVE RESULTS -- #
    print('Save results')
    with open(res_root + os.sep + 'qs1_result.pkl', 'wb') as f:
        pickle.dump(q1_searcher.result, f)
    print("Done.")
def test_sub_blocks():
	start = [8]
	quant = [[64,8,8]]
	color = ['hsv']
	for s in start:
		for q in quant:
			print('--- # -- ')
			db_desc = SubBlockDescriptor(db)
			q1_desc = SubBlockDescriptor(qs1_w1)
			q2_desc = SubBlockDescriptor(qs2_w1,masks=True,mask_path=mask_root)
			db_desc.compute_descriptors(grid_blocks=[s,s],quantify=q,color_space=color[0])
			q1_desc.compute_descriptors(grid_blocks=[s,s],quantify=q,color_space=color[0])
			q2_desc.compute_descriptors(grid_blocks=[s,s],quantify=q,color_space=color[0])
			# -- SEARCH -- #
			q1_search = Searcher(db_desc.result,q1_desc.result)
			q2_search = Searcher(db_desc.result,q2_desc.result)
			q1_desc.clear_memory()
			q2_desc.clear_memory()
			db_desc.clear_memory()
			q1_search.search(limit=3)
			q2_search.search(limit=3)
			# -- EVALUATION -- #
			q1_eval = EvaluateDescriptors(q1_search.result,res_root+os.sep+'gt_corresps1.pkl')
			q2_eval = EvaluateDescriptors(q2_search.result,res_root+os.sep+'gt_corresps2.pkl')
			q1_search.clear_memory()
			q2_search.clear_memory()
			q1_eval.compute_mapatk(limit=1)
			q2_eval.compute_mapatk(limit=1)
			filename = res_root+os.sep+'tests'+os.sep+'sub_res_'+str(s)+'_'+str(q[0])+'.pkl'
			with open(filename,'wb') as f:
				pickle.dump(q1_eval.score,f)
				pickle.dump(q2_eval.score,f)
			print('--- # -- ')