def main(): import dtlpy as dl import numpy as np import matplotlib.pyplot as plt # Get project and dataset project = dl.projects.get(project_name='Food') dataset = project.datasets.get(dataset_name='BeansDataset') # get item from platform item = dataset.items.get(filepath='/image.jpg') # Create a builder instance builder = item.annotations.builder() # add annotations of type box and label person builder.add(annotation_definition=dl.Box( top=10, left=10, bottom=100, right=100, label='black_bean')) # add annotations of type point with attribute builder.add(annotation_definition=dl.Point(x=80, y=80, label='pea'), attribute=['large']) # add annotations of type polygon builder.add(annotation_definition=dl.Polyline( geo=[[80, 40], [100, 120], [110, 130]], label='beans_can')) # create a mask mask = np.zeros(shape=(item.height, item.width), dtype=np.uint8) # mark some part in the middle mask[50:100, 200:250] = 1 # add annotations of type segmentation builder.add( annotation_definition=dl.Segmentation(geo=mask, label='tomato_sauce')) # plot the all of the annotations you created plt.figure() plt.imshow(builder.show()) for annotation in builder: plt.figure() plt.imshow(annotation.show()) plt.title(annotation.label) # upload annotations to the item item.annotations.upload(builder)
def main(): from PIL import Image import numpy as np import dtlpy as dl # Get project and dataset project = dl.projects.get(project_name='Presidents') dataset = project.datasets.get(dataset_name='William Henry Harrison') # image filepath image_filepath = '/home/images/with_family.png' # annotations filepath - RGB with color for each label annotations_filepath = '/home/masks/with_family.png' # upload item to root directory item = dataset.items.upload(local_path=image_filepath, remote_path='/') # read mask from file mask = np.array(Image.open(annotations_filepath)) # get unique color (labels) unique_colors = np.unique(mask.reshape(-1, mask.shape[2]), axis=0) # init dataloop annotations builder builder = item.annotations.builder() # for each label - create a dataloop mask annotation for i, color in enumerate(unique_colors): print(color) if i == 0: # ignore background continue # get mask of same color class_mask = np.all(color == mask, axis=2) # add annotation to builder builder.add(annotation_definition=dl.Segmentation(geo=class_mask, label=str(i))) # upload all annotations item.annotations.upload(builder)
def run(self, progress, item, config=None): """ Write your main plugin function here :param progress: :param config: :param item: :return: """ progress.logger.info('updating progress') progress.update(message='downloading image') progress.logger.info('downloading image') buffer_batch = item.download(save_locally=False) # check inputs if not isinstance(buffer_batch, list): buffer_batch = [buffer_batch] if config is None: config = dict() if 'annotation_type' not in config: config['annotation_type'] = 'segment' if 'confidence_th' not in config: config['confidence_th'] = 0.50 if 'output_action' not in config: config['output_action'] = 'dict' logger.info('input config: %s' % config) ######################### # load buffer to images # ######################### img_batch = [np.asarray(Image.open(buf)) for buf in buffer_batch] # run single img for inference for img in img_batch: if len(img.shape) > 2: if img.shape[2] == 4: img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB) # Run detection progress.logger.info('updating progress') progress.update(message='running model') progress.logger.info('running model') with self.sess.as_default(): results = self.model.detect([img], verbose=0) # Visualize results r = results[0] annotation_builder = item.annotations.builder() for i_det in range(len(r['class_ids'])): if r['scores'][i_det] < config['confidence_th']: continue label = self.class_names[r['class_ids'][i_det]] ret, thresh = cv2.threshold(r['masks'][:, :, i_det].astype(np.uint8), 0.5, 255, 0) if config['annotation_type'] == 'binary': annotation_definition = dl.Segmentation(geo=thresh, label=label) elif config['annotation_type'] == 'segment': annotation_definition = dl.Polygon.from_segmentation(mask=thresh, label=label) else: continue annotation_builder.add(annotation_definition=annotation_definition) annotation_builder.upload() progress.logger.info('updating progress') progress.update(message='done') progress.logger.info('done')
def run(self, item, annotations, config=None, progress=None): progress.logger.info('GPU available: {}'.format(torch.cuda.is_available())) if config is None: config = {} if 'return_type' not in config: config['return_type'] = 'binary' if config['return_type'] not in ['segment', 'binary']: raise ValueError('unknown return type: {}'.format(config['return_type'])) tic_total = time.time() runtime_annotation = list() runtime_siam = list() runtime_steal = list() progress.logger.info('updating progress') progress.update(message='downloading item') progress.logger.info('downloading item') filepath = item.download(overwrite=True) try: im = cv2.imread(filepath) progress.logger.info('updating progress') progress.update(message='running model') progress.logger.info('running model') count = 1 annotation_builder = item.annotations.builder() for annotation in annotations: coordinates = annotation['coordinates'] label = annotation['label'] attributes = annotation['attributes'] count += 1 tic_annotation = time.time() x = coordinates[0]['x'] y = coordinates[0]['y'] w = coordinates[1]['x'] - x h = coordinates[1]['y'] - y # initial segmentation tic_siam = time.time() state = self.inference_initial_segment(im=im, x=x, y=y, w=w, h=h) runtime_siam.append(time.time() - tic_siam) mask = state['mask'] > state['p'].seg_thr # coarse to fine tic_steal = time.time() final = 1 * mask runtime_steal.append(time.time() - tic_steal) ########## # Upload # ########## runtime_annotation.append(time.time() - tic_annotation) if config['return_type'] == 'binary': annotation_builder.add(annotation_definition=dl.Segmentation(geo=final, label=label, attributes=attributes)) elif config['return_type'] == 'segment': annotation_builder.add(annotation_definition=dl.Polygon.from_segmentation(mask=final, label=label, attributes=attributes)) else: raise ValueError('Unknown return type: {}'.format(config['return_type'])) annotation_builder.upload() finally: if os.path.isfile(filepath): os.remove(filepath) progress.logger.info('updating progress') progress.update(message='done') progress.logger.info('done') runtime_total = time.time() - tic_total progress.logger.info('Runtime:') progress.logger.info('Total: {:02.1f}s'.format(runtime_total)) progress.logger.info('Mean annotations: {:02.1f}s'.format(np.mean(runtime_annotation))) progress.logger.info('Mean Siam: {:02.1f}s'.format(np.mean(runtime_siam))) progress.logger.info('Mean STEAL: {:02.1f}s'.format(np.mean(runtime_steal))) progress.logger.info('Num annotations: {}'.format(len(runtime_annotation)))
def execute(self, buffer, progress, annotations, item, config=None): try: config = self.check_input_config(config) timing_dict = dict() tic = time.time() image = np.asarray(Image.open(buffer)) timing_dict['loading_img'] = time.time() - tic if len(image.shape) == 2: # not channels: image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) elif image.shape[2] == 4: image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB) else: pass progress.logger.info('updating progress') progress.update(message='running models') progress.logger.info('running models') ############# # Run DEXTR # ############# count = 1 assert isinstance(item, dl.Item) annotation_builder = item.annotations.builder() for annotation in annotations: attributes = annotation['attributes'] label = annotation['label'] coordinates = annotation['coordinates'][0] progress.logger.info('annotation {}/{}'.format( count, len(annotations))) pts = [[cor['x'], cor['y']] for cor in coordinates] points = np.round(pts).astype(np.int) tic = time.time() with self.sess.as_default(): mask = self.predict(image, points) timing_dict['model_operation_%d' % count] = time.time() - tic count += 1 ##################### # Create annotation # ##################### if config['annotation_type'] == 'segment': annotation_builder.add( annotation_definition=dl.Polygon.from_segmentation( mask=mask, label=label, attributes=attributes)) elif config['annotation_type'] == 'binary': annotation_builder.add( annotation_definition=dl.Segmentation(geo=mask, label=label)) else: self.logger.exception('Unknown annotation type: %s' % config['annotation_type']) raise ValueError annotation_builder.upload() progress.logger.info('updating progress') progress.update(message='done') progress.logger.info('done') except Exception as err: self.logger.exception('%s\n%s' % (err, traceback.format_exc())) raise
def main(): """ Annotate a batch of images using a model and upload to platform :return: """ import numpy as np from PIL import Image from keras.applications.imagenet_utils import decode_predictions from keras.applications.inception_v3 import InceptionV3, preprocess_input import dtlpy as dl ############## # load model # ############## model = InceptionV3() ########################## # init platform instance # ########################## project = dl.projects.get(project_name='ImageNet') dataset = project.datasets.get(dataset_name='sample') # get pages of images from dataset pages = dataset.items.list() #################### # start annotating # #################### for page in pages: for item in page: if item.type == 'dir': continue img_batch = [item.download(save_locally=False)] # load images img_batch = [Image.open(buf) for buf in img_batch] # get original images shapes before reshaping for model orig_img_shape = [img.size[::-1] for img in img_batch] # reshape and load images batch = np.array([np.array(img.resize((299, 299))) for img in img_batch]) # preprocess batch batch = preprocess_input(batch) # inference the model predictions = model.predict(batch) # get ImageNet labels labels = decode_predictions(predictions, top=1) # create platform annotations instance builder = item.annotations.builder() for i_pred, label in enumerate(labels): # add the class labels ############################## # If model is classification # ############################## builder.add(annotation_definition=dl.Classification(label=label[0][1])) ############################# # If model outputs polygons # ############################# builder.add(annotation_definition=dl.Polyline(geo=pred['polygon_pts'], label=labels[i_pred][0][1])) ######################### # If model outputs mask # ######################### builder.add(annotation_definition=dl.Segmentation(geo=pred['mask'], label=labels[i_pred][0][1])) # upload a annotations to matching items in platform builder.upload()