def run(self, item, progress=None): """ In this example we upload an annotation to item :param progress: Use this to update the progress of your package :return: """ # these lines can be removed assert isinstance(progress, dl.Progress) progress.update(status='inProgress', progress=0) print('Item received! filename: {}'.format(item.filename)) builder = item.annotations.builder() builder.add(dl.Classification(label='from_function')) item.annotations.upload(builder) print('Annotation uploaded!')
def video_snapshots_generator(item_id=None, item=None, frame_interval=30, image_ext="png"): """ Create video-snapshots :param item_id: item id for the video :param item: item id for the video :param frame_interval: number of frames to take next snapshot :param image_ext: png/jpg :return: the uploaded items """ if item_id is not None: item = dl.items.get(item_id=item_id) if item is None: raise ValueError('Missing input item (or item_id)') if not isinstance(frame_interval, int): raise AttributeError('frame_interval is mast to be integer') if "video" not in item.mimetype: raise AttributeError( "Got {} file type but only video files are supported".format( item.mimetype)) video_path = item.download() # Get the time for single frame from metadata (duration/# of frames) if 'system' in item.metadata and \ 'ffmpeg' in item.metadata['system'] and \ 'duration' in item.metadata['system']['ffmpeg'] and \ 'nb_frames' in item.metadata['system']['ffmpeg']: nb_frames = int(item.metadata["system"]["ffmpeg"]["nb_frames"]) duration = float(item.metadata["system"]["ffmpeg"]["duration"]) video_fps = duration / nb_frames else: try: import cv2 except ImportError: logger.error( 'Import Error! Cant import cv2. ' 'Annotations operations will be limited. import manually and fix errors' ) raise video = cv2.VideoCapture(video_path) video_fps = video.get(cv2.CAP_PROP_FPS) nb_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) duration = video_fps * nb_frames images_path = Videos.disassemble(filepath=video_path, frame_interval=frame_interval, image_ext=image_ext) snapshots_items = None try: # rename files images = [] video_basename = os.path.basename(video_path) for f in os.listdir(images_path): images.append(f) for image in images: image_split_name, ext = os.path.splitext(image) try: frame = int(image_split_name) * frame_interval os.rename( os.path.join(images_path, image), os.path.join( images_path, "{}.frame.{}{}".format(video_basename, frame, ext))) except Exception as e: logger.debug("Rename {} has been failed: {}".format( os.path.join(images_path, image), e)) remote_path = os.path.join( os.path.split(item.filename)[0], "snapshots") snapshots_items = item.dataset.items.upload( local_path=images_path, remote_path=remote_path) # classification tpe annotation creation for each file builder = item.annotations.builder() annotation_itemlinks = [] if not isinstance(snapshots_items, list): snapshots_items = [snapshots_items] for snapshot_item in snapshots_items: item_frame = snapshot_item.name.rsplit("frame.", 1)[1].split(".")[0] if item_frame.isnumeric(): item_time = int(item_frame) * video_fps else: item_frame = item_time = 0 snapshot_item.metadata["system"]["itemLinks"] = [{ "type": "snapshotFrom", "itemId": item.id, "frame": item_frame, "time": item_time }] annotation_itemlinks.append({ "type": "snapshotTo", "itemId": snapshot_item.id, "frame": item_frame, "time": item_time }) snapshot_item.update(system_metadata=True) annotation_definition = dl.Classification(label="Snapshot") builder.add(annotation_definition=annotation_definition, frame_num=int(item_frame), end_frame_num=nb_frames if int(item_frame) + int(video_fps) > nb_frames else int(item_frame) + int(video_fps), start_time=item_time, end_time=duration if item_time + 1 > duration else item_time + 1) annotations = item.annotations.upload(annotations=builder) # update system metadata for annotations count = 0 for annotation in annotations: annotation.metadata["system"]["itemLinks"] = [ annotation_itemlinks[count] ] count += 1 annotations.update(system_metadata=True) except Exception as err: logger.exception(err) finally: if os.path.isdir(images_path): shutil.rmtree(images_path) return snapshots_items
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()