Beispiel #1
0
 def _model_out_obj_tags(self):
     tag_meta_dict = sly.TagMetaCollection()
     return tag_meta_dict.add(self.confidence_tag_meta)
Beispiel #2
0
def convert():
    settings = json.load(open(sly.TaskPaths.TASK_CONFIG_PATH))
    tag_metas = sly.TagMetaCollection()

    out_project = sly.Project(
        os.path.join(sly.TaskPaths.RESULTS_DIR,
                     settings['res_names']['project']), sly.OpenMode.CREATE)
    requested_tags = settings.get("options", {}).get("tags", [])

    if len(requested_tags) != len(set(requested_tags)):
        raise ValueError(
            'Duplicate values detected in the list of requested tags: {}'.
            format(requested_tags))

    skipped_count = 0
    samples_count = 0

    for ds_name, folder_path in get_datasets_names_and_paths():
        dataset = None

        # Process all files in current folder
        filenames_in_folder = sly.fs.list_files(folder_path)
        dataset_progress = sly.Progress('Dataset {!r}'.format(ds_name),
                                        len(filenames_in_folder))

        for dicom_filename in filenames_in_folder:
            try:
                # Read DICOM file
                dicom_obj = pydicom.dcmread(dicom_filename)

                # Extract tags
                tags_and_metas = get_tags_from_dicom_object(
                    dicom_obj, requested_tags)

                # Extract images (DICOM file may contain few images)
                base_name = os.path.basename(dicom_filename)
                images = extract_images_from_dicom(dicom_obj)

                for image_index, image in enumerate(images):
                    sample_name = base_name
                    if len(images) > 1:
                        sample_name = base_name + '__{:04d}'.format(
                            image_index)

                    samples_count += 1
                    ann = sly.Annotation(img_size=image.shape[:2])

                    # Save tags
                    for tag, tag_meta in tags_and_metas:
                        ann = ann.add_tag(tag)
                        if tag_meta not in tag_metas:
                            tag_metas = tag_metas.add(tag_meta)

                    # Save annotations
                    if dataset is None:
                        dataset = out_project.create_dataset(ds_name)
                    dataset.add_item_np(sample_name + '.png', image, ann=ann)

            except Exception as e:
                exc_str = str(e)
                sly.logger.warn(
                    'Input sample skipped due to error: {}'.format(exc_str),
                    exc_info=True,
                    extra={
                        'exc_str': exc_str,
                        'dataset_name': ds_name,
                        'image_name': dicom_filename,
                    })
                skipped_count += 1

            dataset_progress.iter_done_report()

    sly.logger.info('Processed.',
                    extra={
                        'samples': samples_count,
                        'skipped': skipped_count
                    })

    if out_project.total_items == 0:
        raise RuntimeError(
            'Result project is empty! All input DICOM files have unreadable format!'
        )

    out_meta = sly.ProjectMeta(tag_metas=tag_metas)
    out_project.set_meta(out_meta)
def transform(api: sly.Api, task_id, context, state, app_logger):
    storage_dir = my_app.data_dir

    project = api.project.create(WORKSPACE_ID,
                                 PROJECT_NAME,
                                 change_name_if_conflict=True)
    dataset = api.dataset.create(project.id,
                                 DATASET_NAME,
                                 change_name_if_conflict=True)

    local_file = os.path.join(storage_dir,
                              sly.fs.get_file_name_with_ext(INPUT_FILE))
    api.file.download(TEAM_ID, INPUT_FILE, local_file)

    tag_names = set()
    movies_info = []
    with open(local_file, encoding="ISO-8859-1") as f:
        reader = csv.DictReader(f)
        for row in reader:
            movies_info.append(row)
            tag_names.update(parse_genres(row["Genre"]))

    tags_arr = [
        sly.TagMeta(name=tag_name, value_type=sly.TagValueType.NONE)
        for tag_name in tag_names
    ]
    project_meta = sly.ProjectMeta(tag_metas=sly.TagMetaCollection(
        items=tags_arr))
    api.project.update_meta(project.id, project_meta.to_json())
    movies_info_len = len(movies_info)
    movies_info_len_digits = len(str(movies_info_len))
    batch_size = 50

    progress = sly.Progress('Uploading images', movies_info_len, app_logger)
    for batch_idx, batch in enumerate(
            sly._utils.batched(movies_info, batch_size)):
        image_paths = []
        image_names = []
        image_metas = []
        csv_rows = []
        for idx, csv_row in enumerate(batch):
            image_url = csv_row["Poster"]
            cur_img_ext = os.path.splitext(image_url)[1]
            cur_img_idx = str(batch_idx * batch_size + idx + 1).rjust(
                movies_info_len_digits, '0')
            image_name = f"{cur_img_idx}{cur_img_ext}"
            local_path = os.path.join(storage_dir, image_name)

            try:
                download_file(image_url, local_path, app_logger,
                              batch_idx * batch_size + idx, movies_info_len)
            except:
                app_logger.warn(
                    f"Couldn't download image:(row={batch_idx*batch_size+idx}, url={image_url}"
                )
                continue

            csv_rows.append(csv_row)
            image_paths.append(local_path)
            image_names.append(image_name)
            image_metas.append({
                "Title":
                csv_row["Title"],
                "imdbId":
                csv_row["imdbId"],
                "IMDB Score":
                csv_row["IMDB Score"],
                "Imdb Link":
                csv_row["Imdb Link"].replace('title/tt', 'title/tt0')
            })

        images = api.image.upload_paths(dataset.id,
                                        image_names,
                                        image_paths,
                                        metas=image_metas)
        cur_anns = []
        for image, csv_row in zip(images, csv_rows):
            tags_arr = []
            image_tags = parse_genres(csv_row["Genre"])
            if len(image_tags) == 0:
                continue

            for image_tag in image_tags:
                tag_meta = project_meta.get_tag_meta(image_tag)
                tags_arr.append(sly.Tag(tag_meta))

            tags_arr = sly.TagCollection(items=tags_arr)
            ann = sly.Annotation(img_size=(image.height, image.width),
                                 img_tags=tags_arr)
            cur_anns.append((image.id, ann))

        if len(cur_anns) > 0:
            img_ids = [img_id for img_id, ann in cur_anns]
            anns = [ann for img_id, ann in cur_anns]
            api.annotation.upload_anns(img_ids, anns)

        progress.iters_done_report(len(batch))
    api.task.set_output_project(task_id, project.id, project.name)
    my_app.stop()
Beispiel #4
0
 def _model_out_tags(self):
     return sly.TagMetaCollection(items=[self.confidence_tag_meta])
Beispiel #5
0
 def __init__(self):
     self.settings = json.load(open(sly.TaskPaths.SETTINGS_PATH))
     self.obj_classes = sly.ObjClassCollection()
     self.imgtag_metas = sly.TagMetaCollection()