def get_ann(img_path, inst_path): global classes_dict ann = sly.Annotation.from_img_path(img_path) class_name = 'text' color = [255, 0, 255] if inst_path is not None: with open(inst_path, "r") as file: all_lines = file.readlines() for line in all_lines: line = line.strip('\n').split(',')[:9] text = line[8] if text == '###': text = '' line = line[:8] try: line = list(map(lambda i: int(i), line)) except ValueError: line[0] = line[0][1:] line = list(map(lambda i: int(i), line)) points = [ sly.PointLocation(line[i + 1], line[i]) for i in range(0, 8, 2) ] polygon = sly.Polygon(exterior=points, interior=[]) if not classes_dict.has_key(class_name): obj_class = sly.ObjClass(name=class_name, geometry_type=sly.Polygon, color=color) classes_dict = classes_dict.add( obj_class) # make it for meta.json ann = ann.add_label( sly.Label(polygon, classes_dict.get(class_name), None, text)) return ann
def generate_example(augs_settings, augs=None, preview=True, product_id=None, img=None, ann=None): if product_id is None or img is None or ann is None: product_id, img, ann = get_random_product() if logging.getLevelName(sly.logger.level) == 'DEBUG': sly.image.write(os.path.join(vis_dir, "01_img.png"), img) label_image, label_mask = preprocess_product(img, ann, augs_settings, is_main=True) if logging.getLevelName(sly.logger.level) == 'DEBUG': sly.image.write(os.path.join(vis_dir, "02_label_image.png"), label_image) sly.image.write(os.path.join(vis_dir, "03_label_mask.png"), label_mask) orig_h, orig_w = label_image.shape[:2] for crop_f, place_f, range_index in zip(crops_funcs, place_funcs, list(range(0, 4))): if random.uniform(0, 1) <= augs_settings['noise']['corner_probability']: _, noise_img, noise_ann = get_random_product(ignore_id=product_id) noise_img, noise_ann = crop_label(noise_img, noise_ann, padding=0) noise_mask = draw_white_mask(noise_ann) if logging.getLevelName(sly.logger.level) == 'DEBUG': sly.image.write(os.path.join(vis_dir, "04_noise_img.png"), noise_img) if random.uniform(0, 1) <= augs_settings['noise']['aug_probability']: noise_img, noise_mask = augs.apply_to_foreground(noise_img, noise_mask) y_range = get_y_range(range_index, orig_h, portion=augs_settings["noise"]["max_occlusion_height"]) x_range = get_x_range(range_index, orig_w, portion=augs_settings["noise"]["max_occlusion_width"]) y = random.randint(int(y_range[0]), int(y_range[1])) x = random.randint(int(x_range[0]), int(x_range[1])) noise_img, noise_mask = crop_f(y, x, orig_h, orig_w, noise_img, noise_mask) if logging.getLevelName(sly.logger.level) == 'DEBUG': sly.image.write(os.path.join(vis_dir, f"04_noise_img_{range_index}.png"), noise_img) sly.image.write(os.path.join(vis_dir, f"05_noise_mask_{range_index}.png"), noise_mask) place_f(y, x, label_image, label_mask, noise_img, noise_mask) if logging.getLevelName(sly.logger.level) == 'DEBUG': sly.image.write(os.path.join(vis_dir, "06_final_mask.png"), label_mask) if not np.any(label_mask): # if empty mask - figure may be entirely covered by others return None, None, None label_preview = None if preview is True: label_preview = sly.Label( sly.Bitmap(label_mask[:, :, 0].astype(bool), origin=sly.PointLocation(0, 0)), RESULT_CLASS ) return label_image, label_mask, label_preview
def get_ann(img_path, coords, words): global classes_dict ann = sly.Annotation.from_img_path(img_path) class_name = 'text' color = [255, 0, 0] name = img_path.split('/')[-1] line = coords[name] points = [sly.PointLocation(line[i + 1], line[i]) for i in range(0, 8, 2)] polygon = sly.Polygon(exterior=points, interior=[]) if not classes_dict.has_key(class_name): obj_class = sly.ObjClass(name=class_name, geometry_type=sly.Polygon, color=color) classes_dict = classes_dict.add(obj_class) # make it for meta.json ann = ann.add_label( sly.Label(polygon, classes_dict.get(class_name), None, words[name])) return ann
def get_ann(img_path, coords_text): global classes_dict ann = sly.Annotation.from_img_path(img_path) class_name = 'text' color = [255, 0, 255] len_polygon_points = 9 for i in range(0, len(coords_text), len_polygon_points): line = coords_text[i:i + len_polygon_points] text = line[8] points = [ sly.PointLocation(line[i + 1], line[i]) for i in range(0, 8, 2) ] polygon = sly.Polygon(exterior=points, interior=[]) if not classes_dict.has_key(class_name): obj_class = sly.ObjClass(name=class_name, geometry_type=sly.Polygon, color=color) classes_dict = classes_dict.add(obj_class) # make it for meta.json ann = ann.add_label( sly.Label(polygon, classes_dict.get(class_name), None, text)) return ann
def convert_points(simple_points): # TODO: Maybe use row_col_list_to_points here? return [sly.PointLocation(int(p[1]), int(p[0])) for p in simple_points]
def synthesize(api: sly.Api, task_id, state, meta: sly.ProjectMeta, image_infos, labels, bg_images, cache_dir, preview=True): progress_cb = refresh_progress_preview if preview is False: progress_cb = refresh_progress augs = yaml.safe_load(state["augs"]) sly.logger.info("Init augs from yaml file") aug.init_fg_augs(augs) visibility_threshold = augs['objects'].get('visibility', 0.8) classes = state["selectedClasses"] bg_info = random.choice(bg_images) sly.logger.info("Download background") bg = api.image.download_np(bg_info.id) sly.logger.debug(f"BG shape: {bg.shape}") res_image = bg.copy() res_labels = [] # sequence of objects that will be generated res_classes = [] to_generate = [] for class_name in classes: original_class: sly.ObjClass = meta.get_obj_class(class_name) res_classes.append(original_class.clone(geometry_type=sly.Bitmap)) count_range = augs["objects"]["count"] count = random.randint(*count_range) for i in range(count): to_generate.append(class_name) random.shuffle(to_generate) res_meta = sly.ProjectMeta(obj_classes=sly.ObjClassCollection(res_classes)) progress = sly.Progress("Processing foregrounds", len(to_generate)) progress_cb(api, task_id, progress) progress_every = max(10, int(len(to_generate) / 20)) cover_img = np.zeros(res_image.shape[:2], np.int32) # size is (h, w) objects_area = defaultdict(lambda: defaultdict(float)) cached_images = {} # generate objects for idx, class_name in enumerate(to_generate, start=1): if class_name not in labels: progress.iter_done_report() continue image_id = random.choice(list(labels[class_name].keys())) label: sly.Label = random.choice(labels[class_name][image_id]) if image_id in cached_images: source_image = cached_images[image_id] else: image_info = image_infos[image_id] source_image = _get_image_using_cache(api, cache_dir, image_id, image_info) cached_images[image_id] = source_image label_img, label_mask = get_label_foreground(source_image, label) #sly.image.write(os.path.join(cache_dir, f"{index}_label_img.png"), label_img) #sly.image.write(os.path.join(cache_dir, f"{index}_label_mask.png"), label_mask) label_img, label_mask = aug.apply_to_foreground(label_img, label_mask) #sly.image.write(os.path.join(cache_dir, f"{index}_aug_label_img.png"), label_img) #sly.image.write(os.path.join(cache_dir, f"{index}_aug_label_mask.png"), label_mask) label_img, label_mask = aug.resize_foreground_to_fit_into_image( res_image, label_img, label_mask) #label_area = g.area find_place = False for attempt in range(3): origin = aug.find_origin(res_image.shape, label_mask.shape) g = sly.Bitmap(label_mask[:, :, 0].astype(bool), origin=sly.PointLocation(row=origin[1], col=origin[0])) difference = count_visibility(cover_img, g, idx, origin[0], origin[1]) allow_placement = True for object_idx, diff in difference.items(): new_area = objects_area[object_idx]['current'] - diff visibility_portion = new_area / objects_area[object_idx][ 'original'] if visibility_portion < visibility_threshold: #sly.logger.warn(f"Object '{idx}', attempt {attempt + 1}: " # f"visible portion ({visibility_portion}) < threshold ({visibility_threshold})") allow_placement = False break if allow_placement is True: find_place = True break else: continue if find_place is False: sly.logger.warn( f"Object '{idx}' is skipped: can not be placed to satisfy visibility threshold" ) continue try: aug.place_fg_to_bg(label_img, label_mask, res_image, origin[0], origin[1]) g.draw(cover_img, color=idx) for object_idx, diff in difference.items(): objects_area[object_idx]['current'] -= diff current_obj_area = g.area objects_area[idx]['current'] = current_obj_area objects_area[idx]['original'] = current_obj_area res_labels.append(sly.Label(g, res_meta.get_obj_class(class_name))) except Exception as e: #sly.logger.warn(repr(e)) sly.logger.warn( f"FG placement error:: label shape: {label_img.shape}; mask shape: {label_mask.shape}", extra={"error": repr(e)}) progress.iter_done_report() if idx % progress_every == 0: # progress.need_report(): progress_cb(api, task_id, progress) progress_cb(api, task_id, progress) res_ann = sly.Annotation(img_size=bg.shape[:2], labels=res_labels) # debug visualization # sly.image.write(os.path.join(cache_dir, "__res_img.png"), res_image) #res_ann.draw(res_image) #sly.image.write(os.path.join(cache_dir, "__res_ann.png"), res_image) res_meta, res_ann = rasterize.convert_to_nonoverlapping(res_meta, res_ann) return res_image, res_ann, res_meta
def convert_points(simple_points): return [sly.PointLocation(int(p[1]), int(p[0])) for p in simple_points]