def run(self, item, progress=None):
        assert isinstance(item, dl.Item)
        logger.info('Downloading video')
        video_filepath = item.download()
        try:
            logger.info('Running dection on video')
            annotations_dict = detect_video(graph=self.graph,
                                            yolo=self.yolo,
                                            encoder=self.encoder,
                                            video_filepath=video_filepath,
                                            mark_on_video=False,
                                            show=False)

            logger.info('Building Dataloop annotations')
            annotations_builder = item.annotations.builder()
            for i_frame, annotations in annotations_dict.items():
                for annotation in annotations:
                    annotations_builder.add(
                        annotation_definition=dl.Box(
                            top=annotation['top'],
                            left=annotation['left'],
                            bottom=annotation['bottom'],
                            right=annotation['right'],
                            label=annotation['label']),
                        object_id=annotation['object_id'],
                        frame_num=i_frame,
                        end_frame_num=i_frame,
                    )
            logger.info('Uploading annotations...')
            item.annotations.upload(annotations_builder)
            logger.info(
                'Done! detection and annotations finished successfully')
        finally:
            if os.path.isfile(video_filepath):
                os.remove(video_filepath)
예제 #2
0
    def predict_item(self,
                     item,
                     checkpoint_path=None,
                     with_upload=True,
                     model_name='object_detection'):
        filepath = item.download()
        results_path = self.predict_single_image(
            image_path=filepath, checkpoint_path=checkpoint_path)
        if with_upload:
            with open(results_path) as fg:
                results = fg.readlines()
            builder = item.annotations.builder()
            for result in results:
                result_ls = result.split(' ')
                builder.add(dl.Box(left=int(result_ls[2]),
                                   top=int(result_ls[3]),
                                   right=int(result_ls[4]),
                                   bottom=int(result_ls[5]),
                                   label=result_ls[0]),
                            model_info={
                                'confidence': result_ls[1],
                                'name': model_name
                            })
            item.annotations.upload(builder)

        dirname = os.path.dirname(filepath)
        return dirname
def main():
    """
    This is an example how to upload files and annotations to Dataloop platform.
    Image folder contains the images to upload.
    Annotations folder contains json file of the annotations. Same name as the image.
    We read the images one by one and create the Dataloop annotations using the annotation builder.
    Finally, we upload both the image and the matching annotations

    :return:
    """
    import json
    import os
    import dtlpy as dl

    # Get project and dataset
    project = dl.projects.get(project_name='Yachts')
    dataset = project.datasets.get(dataset_name='Open Seas')

    images_folder = '/home/local/images'
    annotations_folder = '/home/local/annotations'

    for img_filename in os.listdir(images_folder):
        # get the matching annotations json
        _, ext = os.path.splitext(img_filename)
        ann_filename = os.path.join(annotations_folder,
                                    img_filename.replace(ext, '.json'))
        img_filename = os.path.join(images_folder, img_filename)

        # Upload or get annotations from platform (if already exists)
        item = dataset.items.upload(local_path=img_filename,
                                    remote_path='/in_storm',
                                    overwrite=False)
        assert isinstance(item, dl.Item)

        # read annotations from file
        with open(ann_filename, 'r') as f:
            annotations = json.load(f)

        # create a Builder instance and add all annotations to it
        builder = item.annotations.builder()
        for annotation in annotations:
            # line format if 4 points of bbox
            # this is where you need to update according to your annotation format
            label_id = annotation['label']
            left = annotation['left']
            top = annotation['top']
            right = annotation['right']
            bottom = annotation['bottom']
            builder.add(annotation_definition=dl.Box(top=top,
                                                     left=left,
                                                     bottom=bottom,
                                                     right=right,
                                                     label=str(label_id)))

        # upload annotations
        item.annotations.upload(builder)
예제 #4
0
파일: converter.py 프로젝트: serans1/dtlpy
    def my_converter(annotation):
        """
        :param annotation: custom annotation format
        :type annotation: dict
        :return: dataloop Annotation object
        """
        annotations = dl.Annotation.new(annotation_definition=dl.Box(top=annotation['top'],
                                                                     bottom=annotation['bottom'],
                                                                     left=annotation['left'],
                                                                     right=annotation['right'],
                                                                     label=annotation['label']))

        return annotation
예제 #5
0
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)
예제 #6
0
def main():
    import dtlpy as dl
    import matplotlib.pyplot as plt

    # Get project and dataset
    project = dl.projects.get(project_name='Food')
    dataset = project.datasets.get(dataset_name='BeansDataset')
    item = dataset.items.get(filepath='/flying boxes.mp4')

    ############################
    # using annotation builder #
    ############################
    # create annotation builder
    builder = item.annotations.builder()

    for i_frame in range(100):
        # go over 100 frame
        for i_detection in range(10):
            # for each frame we have 10 different detections (location is just for the example)
            builder.add(
                annotation_definition=dl.Box(top=2 * i_frame,
                                             left=2 * i_detection,
                                             bottom=2 * i_frame + 10,
                                             right=2 * i_detection + 100,
                                             label="moving box"),
                frame_num=i_frame,  # set the frame for the annotation
                object_id=i_detection + 1
                # need to input the element id to create the connection between frames
            )
            # starting from frame 50 add another 10 new annotations of a moving point
            if i_frame > 50:
                builder.add(annotation_definition=dl.Point(
                    x=2 * i_frame, y=2 * i_detection, label="moving point"),
                            frame_num=i_frame,
                            object_id=20 + (i_detection + 1))
    # get frame annotations
    frame_annotations = builder.get_frame(frame_num=55)
    # Plot the annotations in frame 55 of the created annotations
    plt.figure()
    plt.imshow(frame_annotations.show())

    # plot each annotations separately
    for annotation in frame_annotations:
        plt.figure()
        plt.imshow(annotation.show())
        plt.title(annotation.label)

    # Add the annotations to platform
    item.annotations.upload(builder)

    #####################
    # single annotation #
    #####################
    # create one annotations for a video (without using tracker and object ids)
    annotation = dl.Annotation.new(item=item)

    for i_frame in range(100):
        # go over 100 frame
        annotation.add_frame(
            annotation_definition=dl.Box(
                top=2 * i_frame,
                left=2 * (i_frame + 10),
                bottom=2 * (i_frame + 50),
                right=2 * (i_frame + 100),
                label="moving box",
            ),
            frame_num=i_frame,  # set the frame for the annotation
        )

    # upload to platform
    annotation.upload()

    ##############################################
    # show annotation state in a specified frame #
    ##############################################

    # Get from platform
    annotations = item.annotations.list()

    # Plot the annotations in frame 55 of the created annotations
    plt.figure()
    plt.imshow(annotations.get_frame(frame_num=55).show())

    # Play video with the Dataloop video player
    annotations.video_player()
def main():
    """
    Convert yolo annotation and images to Dataloop
    Yolo annotations format:
    For each image there is a text file with same name that has a list of box location and label index
    """
    import dtlpy as dl
    from PIL import Image
    import os

    # Get project and dataset
    project = dl.projects.get(project_name='Fruits')
    dataset = project.datasets.get(dataset_name='Rambutan')

    images_and_annotations_path = '/home/fruits/data'

    # read all images from local dataset
    img_filepaths = list()
    for path, subdirs, files in os.walk(images_and_annotations_path):
        for filename in files:
            striped, ext = os.path.splitext(filename)
            if ext in ['.jpeg']:
                img_filepaths.append(os.path.join(path, filename))

    classes_filepath = '/home/fruits/classes.txt'
    # get labels from file
    with open(classes_filepath, 'r') as f:
        labels = {
            i_line: label.strip()
            for i_line, label in enumerate(f.readlines())
        }

    for filepath in img_filepaths:
        # get image height and width
        img = Image.open(filepath)
        width, height = img.size()
        # upload item to platform
        item = dataset.items.update(filepath=filepath)

        # get YOLO annotations
        _, ext = os.path.splitext(filepath)
        annotation_filepath = filepath.replace(ext, '.txt')
        with open(annotation_filepath, 'r') as f:
            annotations = f.read().split('\n')

        builder = item.annotations.builder()
        # convert to Dataloop annotations
        for annotation in annotations:
            if not annotation:
                continue
            # convert annotation format
            elements = annotation.split(" ")
            label_id = elements[0]

            xmin_add_xmax = float(elements[1]) * (2.0 * float(width))
            ymin_add_ymax = float(elements[2]) * (2.0 * float(height))

            w = float(elements[3]) * float(width)
            h = float(elements[4]) * float(height)

            left = (xmin_add_xmax - w) / 2
            top = (ymin_add_ymax - h) / 2
            right = left + w
            bottom = top + h

            # add to annotations
            builder.add(annotation_definition=dl.Box(top=top,
                                                     left=left,
                                                     bottom=bottom,
                                                     right=right,
                                                     label=labels[label_id]))
            # upload all annotations of an item
            builder.upload()
def main():
    """
    Detect and track (using model and some tracker) and upload annotation to platform
    :return:
    """
    import cv2
    import dtlpy as dl

    ##########################
    # Load model and tracker #
    ##########################
    # load your model for detection
    model = load_some_model()
    # load any tracking algorithm to track detected elements
    tracker = load_some_tracker()

    ##############
    # load video #
    ##############
    video_path = 'some/video/path'

    vid = cv2.VideoCapture(video_path)
    if not vid.isOpened():
        raise IOError("Couldn't open webcam or video")
    video_fps = vid.get(cv2.CAP_PROP_FPS)
    video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
                  int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    video_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))

    ############
    # Platform #
    ###########
    # get the item from platform
    item = dl.projects.get(project_name='MyProject') \
        .datasets.get(dataset_name='MyDataset') \
        .items.get(filepath='/path/to/video.mp4')
    builder = item.annotations.builder()

    #######
    # Run #
    #######
    frame_num = 0
    while True:
        # get new frame from video
        return_value, frame = vid.read()
        if not return_value:
            break

        # get detection
        detections = model.predict(frame)

        # update tracker
        tracked_elements = tracker.update(detections, frame)

        # update annotations object
        for element in tracked_elements:
            # element.bb - format of the bounding box is 2 points in 1 array - [x_left, y_top, x_right, y_bottom])
            # tracking id of each element is in element.id. to keep the ids of the detected elements
            left, top, bottom, right = element.bb,  # points bounding box annotation
            builder.add(annotation_definition=dl.Box(top=top,
                                                     left=left,
                                                     right=right,
                                                     bottom=bottom,
                                                     label=element.label),
                        object_id=element.id,
                        frame_num=frame_num)
        # increase frame number
        frame_num += 1
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    ##################################
    # Upload annotations to platform #
    ##################################
    item.annotations.upload(builder.to_platform())
예제 #9
0
    def track_bounding_box(self,
                           item,
                           annotation,
                           frame_duration=100,
                           progress=None):
        """

        :param item: dl.Item
        :param annotation: dl.Annotation
        :param frame_duration: How many frames ahead to track
        :param progress:
        :return:
        """
        try:
            assert isinstance(item, dl.Item)
            assert isinstance(annotation, dl.Annotation)
            item_stream_url = item.stream
            bbx = annotation.coordinates
            start_frame = annotation.start_frame
            annotation.fps = item.fps

            logger.info('[Tracker] Started')
            tic_get_cap = time.time()
            logger.info('[Tracker] video url: {}'.format(item_stream_url))
            cap = cv2.VideoCapture('{}?jwt={}'.format(item_stream_url,
                                                      dl.token()))
            runtime_get_cap = time.time() - tic_get_cap
            if not cap.isOpened():
                logger.error('[Tracker] failed opening video url')
                raise ValueError('cant open video stream. item id: {}'.format(
                    item_stream_url))
            logger.info('[Tracker] received bbs(xyxy): {}'.format(bbx))
            tic_first_frame_set = time.time()
            cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
            runtime_first_frame_set = time.time() - tic_first_frame_set
            mask_enable = False
            runtime_load_frame = list()
            runtime_track = list()
            tic_total = time.time()
            state_bbx = None
            for i_frame in range(frame_duration):
                logger.info(
                    '[Tracker] processing frame #{}'.format(start_frame +
                                                            i_frame))
                tic = time.time()
                ret, frame = cap.read()
                if not ret:
                    break
                runtime_load_frame.append(time.time() - tic)
                tic = time.time()
                # get bounding box
                top = bbx[0]['y']
                left = bbx[0]['x']
                bottom = bbx[1]['y']
                right = bbx[1]['x']
                w = right - left
                h = bottom - top
                if i_frame == 0:  # init
                    target_pos = np.array([left + w / 2, top + h / 2])
                    target_sz = np.array([w, h])
                    # init tracker
                    state_bbx = siamese_init(im=frame,
                                             target_pos=target_pos,
                                             target_sz=target_sz,
                                             model=self.siammask,
                                             hp=self.cfg['hp'],
                                             device=self.device)
                else:
                    # track
                    state_bbx = siamese_track(state=state_bbx,
                                              im=frame,
                                              mask_enable=mask_enable,
                                              refine_enable=True,
                                              device=self.device)
                    if mask_enable:
                        state_bbx['ploygon'].flatten()
                        mask = state_bbx['mask'] > state_bbx['p'].seg_thr

                        annotation.add_frame(annotation_definition=dl.Box(
                            top=np.min(np.where(mask)[0]),
                            left=np.min(np.where(mask)[1]),
                            bottom=np.max(np.where(mask)[0]),
                            right=np.max(np.where(mask)[1]),
                            label=annotation.label),
                                             frame_num=start_frame + i_frame)
                    else:
                        top = state_bbx['target_pos'][
                            1] - state_bbx['target_sz'][1] / 2
                        left = state_bbx['target_pos'][
                            0] - state_bbx['target_sz'][0] / 2
                        bottom = state_bbx['target_pos'][
                            1] + state_bbx['target_sz'][1] / 2
                        right = state_bbx['target_pos'][
                            0] + state_bbx['target_sz'][0] / 2

                        annotation.add_frame(annotation_definition=dl.Box(
                            top=top,
                            left=left,
                            bottom=bottom,
                            right=right,
                            label=annotation.label),
                                             frame_num=start_frame + i_frame)
                    runtime_track.append(time.time() - tic)
            runtime_total = time.time() - tic_total
            fps = frame_duration / runtime_total
            logger.info('[Tracker] Finished.')
            logger.info('[Tracker] Runtime information: \n'
                        'Total runtime: {:.2f}[s]\n'
                        'FPS: {:.2f}fps\n'
                        'Get url capture object: {:.2f}[s]\n'
                        'Initial set frame: {:.2f}[s]\n'
                        'Total track time: {:.2f}[s]\n'
                        'Mean load per frame: {:.2f}\n'
                        'Mean track per frame: {:.2f}'.format(
                            runtime_total, fps, runtime_get_cap,
                            runtime_first_frame_set,
                            np.sum(runtime_load_frame) + np.sum(runtime_track),
                            np.mean(runtime_load_frame),
                            np.mean(runtime_track)))
            annotation.delete()
            item.annotations.upload(annotations=[annotation])
            progress.update(status='success')
        except Exception:
            logger.exception('Failed during track:')
            raise