def detect(self, image): if self.model is None: return {} image = image.convert('RGB') width, height = image.size image.thumbnail((1024, 1024)) image = img_to_array(image) result = self.model.detect([image])[0] masks = result.get('masks') class_ids = result.get('class_ids') coco_image = im.Image(width=width, height=height) for i in range(masks.shape[-1]): mask = resize(masks[..., i], (height, width)) mask = im.Mask(mask) class_id = class_ids[i] class_name = CLASS_NAMES[class_id] category = im.Category(class_name) coco_image.add(mask, category=category) return coco_image.coco()
def prepare_dataset(self, all_images=False): dataset = imantics.Dataset('INbreast') categories_names = list(self.annotations_df.category.unique()) colors = {name: imantics.Color.random() for name in categories_names} categories = { name: imantics.Category(name, id=i, color=colors[name]) for i, name in enumerate(categories_names) } total = len(self.annotations_df) prepared = 0 for case_id, annotations_data in self.annotations_df.groupby( 'case_id'): width = annotations_data['width'].unique()[0] height = annotations_data['height'].unique()[0] image = imantics.Image(id=case_id, width=width, height=height) image.file_name = '%d.png' % case_id annotations = [] for _, annotation_data in annotations_data.iterrows(): annotation_id = annotation_data['annotation_id'] category = categories[annotation_data['category']] color = colors[annotation_data['category']] points = annotation_data['points'] annotation = imantics.Annotation(id=annotation_id, polygons=[points], category=category, color=color) annotations.append(annotation) image.add(annotations) dataset.add(image) prepared += 1 print('\r%d %d/%d' % (case_id, prepared, total), end="") print('\rprepared %d images' % prepared) print() self.dataset = dataset
def __call__(self): """ Generates imantics category object """ data = { 'name': self.name, 'color': self.color, 'parent': self.supercategory, 'metadata': self.metadata, 'id': self.id } return im.Category(**data)
def to_imantics_annotation(self): """ Returns imantics.annotation.Annotation representation of ObjectAnnotation. """ imantics_category = imantics.Category( id=self.category.id, name=self.category.name ) imantics_mask = imantics.Mask.create(self.mask.bool_mask) imantics_annotation = imantics.annotation.Annotation.from_mask( mask=imantics_mask, category=imantics_category ) return imantics_annotation
def imageCallback(self, img_msg, info_msg): try: img = self.bridge.imgmsg_to_cv2(img_msg, "bgr8") header = img_msg.header except CvBridgeError as err: rospy.logerr(err) return board_mask = np.zeros_like(img[:, :, 0]) if (self.image_roi is None): board_mask[:, :] = 1 else: board_mask[self.image_roi[0]:self.image_roi[1], self.image_roi[2]:self.image_roi[3]] = 1 board_size = board_mask.sum() all_markers = segmentImage(img, self.filter_size, self.filter_const) markers_masked = (all_markers + 1) * board_mask filtered_idxs, filtered_counts = filterMarkers(markers_masked, board_size / 250, board_size / 3) markers_masked[np.isin(markers_masked, filtered_idxs, invert=True)] = 0 ann_img = imantics.Image(image_array=img) colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255), (255, 0, 255), (255, 255, 0)] for j, idx in enumerate(filtered_idxs): mask = imantics.Mask((markers_masked == idx).astype(np.uint8)) ann = imantics.Annotation(image=ann_img, category=imantics.Category( 'obj_{}'.format(j), color=imantics.Color(rgb=colors[j])), mask=mask, bbox=imantics.BBox.from_mask(mask)) ann_img.add(ann) display_img = ann_img.draw(thickness=1, color_by_category=True) try: display_msg = self.bridge.cv2_to_imgmsg(display_img.astype( np.uint8), encoding="bgr8") except CvBridgeError as err: rospy.logerr(err) return display_msg.header = img_msg.header self.image_pub.publish(display_msg)
def to_imantics_annotation(self): """ Returns imantics.annotation.Annotation representation of ObjectAnnotation. """ try: import imantics except ImportError: raise ImportError( 'Please run "pip install -U imantics" ' "to install imantics first for imantics conversion.") imantics_category = imantics.Category(id=self.category.id, name=self.category.name) if self.mask is not None: imantics_mask = imantics.Mask.create(self.mask.bool_mask) imantics_annotation = imantics.annotation.Annotation.from_mask( mask=imantics_mask, category=imantics_category) else: imantics_bbox = imantics.BBox.create(self.bbox.to_voc_bbox()) imantics_annotation = imantics.annotation.Annotation.from_bbox( bbox=imantics_bbox, category=imantics_category) return imantics_annotation
import imantics import scipy as sp import cv2 import os from tqdm import tqdm import json from joblib import Parallel, delayed image_id = 1 annotation_id = 1 coco_dataset = {'images': [], 'categories': [], 'annotations': []} basedir = '/home/spadmin/data/lostandfound_processing/testset' categories = {1: imantics.Category('void'), 2: imantics.Category('object')} i = 0 # loads ego-vehicle mask with open( '/home/spadmin/Downloads/0000_02_Hanns_Klemm_Str_44_000000_000020_leftImg8bit_rgb (1).json', 'r') as f: coco_vehicle = json.load(f) ego_mask = coco_vehicle['annotations'][0]['segmentation'][0] ego_points = np.array(ego_mask).reshape((-1, 2)).round().astype('int') vehicle_mask = np.zeros((1024, 2048, 1)) cv2.fillPoly(vehicle_mask, [ego_points], color=(100, 100, 100)) vehicle_mask = vehicle_mask[..., 0] def process_image(file, image_id): """ Assumes that prediction and image file have similar prefix, see below""" prediction = cv2.imread(os.path.join(basedir, file), cv2.IMREAD_ANYDEPTH) # reduce noise (makes it easier to edit predictions)
in_dicoms_dir = osp.join(inbreast_dir, 'AllDICOMs') in_images_dir = images_dir out_images_dir = osp.join(output_dir, 'images') out_annotations_dir = osp.join(out_images_dir, 'annotations') # cases_ids = [20586960] cases_ids = sorted([ int(osp.basename(x).split('_')[0]) for x in glob(osp.join(in_xmls_dir, '*.xml')) ]) os.makedirs(out_images_dir, exist_ok=True) os.makedirs(out_annotations_dir, exist_ok=True) dataset = imantics.Dataset('INbreast - prectoral muscle') category_breast = imantics.Category('breast') category_pectoral_muscle = imantics.Category('pectoral_muscle') index_breast = 0 index_pectoral_muscle = 1 total = len(cases_ids) processed = 0 for case_id in cases_ids: # check annotation anns_fns = glob(osp.join(in_xmls_dir, '*%d*.xml' % case_id)) assert len(anns_fns) == 1 ann_fn = anns_fns[0] # check dicom dcms_fns = glob(osp.join(in_dicoms_dir, '*%d*.dcm' % case_id)) assert len(dcms_fns) == 1 dcm_fn = dcms_fns[0]