def _extract_image_and_mask_from_path(img_path, mask_path, textboxes, multiple): img = path2img(img_path) if mask_path is None: text_mask = 1 - generate_text_mask(img.shape[:2], textboxes) # returns all 1's if textboxes == None return [(img, text_mask), ] mask = path2mask(mask_path) text_mask = 1 - generate_text_mask(mask.shape, textboxes) if multiple: masks = extract_paintings_from_mask(mask) if masks is None or len(masks) == 0: return [(img, np.ones(img.shape[:2]).astype(np.uint8)), ] return [(img, ((text_mask != 0) & (m != 0)).astype(np.uint8)) for m in masks] return [(img, ((text_mask != 0) & (mask != 0)).astype(np.uint8)), ]
def _extract_features_from_path_mask(_path, _path_mask, textboxes): img, mask = path2img(_path), path2mask(_path_mask) text_mask = 1 - generate_text_mask(mask.shape, textboxes) if multiple: masks = extract_paintings_from_mask(mask) #print(f"{_path_mask} - {len(masks)}") return [ extract_features_func(img, mask=((text_mask != 0) & (m != 0)).astype(np.uint8)) for m in masks ] return [ extract_features_func(img, mask=((text_mask != 0) & (mask != 0)).astype( np.uint8)), ]
def extract_textbox(orig_img, mask=None): """ Given an image (and a mask), extracts the textbox in it, if any found (taking into account a metric). Parameters ---------- orig_img : image from which extract the textbox mask : mask to use. In case of multiple paintings, each connected component will be considered as a different painting. Returns ------- score: bboxes (0 or 1 per connected component found in the mask) """ masks = [] shapes = [] bboxes = [] # extract each painting's mask if mask is None: masks = [np.ones(orig_img.shape[:2])] else: masks = extract_paintings_from_mask(mask) if masks is None: return [[0, 0, 0, 0]] # we try to extract one textbox per painting for m in masks: img = orig_img.copy() img[m == 0] = (0, 0, 0) sc_br, bbox_br = get_best_textbox_candidate(brightText(img), m) sc_dr, bbox_dr = get_best_textbox_candidate(darkText(img), m) bbox = bbox_br if sc_dr == 0 and sc_br == 0: continue if sc_dr > sc_br: bbox = bbox_dr bboxes.append(bbox) return bboxes
def apply_filters(mask, filters, img=None): mask_copy = mask.copy() metadata_list = [] # apply consecutive filters to a mask. for i in range(len(filters)): f = filters[i] final_mask = np.zeros_like(mask) masks = extract_paintings_from_mask(mask) #num_labels, labels = cv2.connectedComponents(mask.astype(np.uint8)) #for lab in range(1, num_labels): #m = (labels == lab).astype(np.uint8) # we will always have a painting if masks is None or len(masks) == 0: x, y, w, h = cv2.boundingRect(mask_copy) mask_copy[y:y+h, x:x+w] = 255 return mask_copy, (extract_angle(img) if img is not None else 0.0, [[x, y], [x+w, y], [x+w, y+h], [x, y+h]]) for m in masks: _,_, w, h = cv2.boundingRect(m) if w / mask.shape[1] < 0.15 or h / mask.shape[1] < 0.15: continue f_m, metadata = f(m, img=img) final_mask += f_m.astype(np.uint8) # last filter gives us the metadata: [angle, [p1,p2,p3,p4]] if i == len(filters) - 1: metadata_list.append(metadata) mask = final_mask """ # we will always have a painting x, y, w, h = cv2.boundingRect(final_mask) if w / mask.shape[1] < 0.10 or h / mask.shape[1] < 0.10: x, y, w, h = cv2.boundingRect(mask_copy) mask_copy[y:y+h, x:x+w] = 255 return mask_copy, (extract_angle(img) if img is not None else 0.0, [[x, y], [x+w, y], [x+w, y+h], [x, y+h]]) """ return final_mask, metadata_list