Example #1
0
def deposit_weedcoco(weedcoco_path, dataset_dir, image_dir, image_hash):
    with open(weedcoco_path) as f:
        weedcoco = json.load(f)
    for image_reference in weedcoco["images"]:
        file_name = image_reference["file_name"].split("/")[-1]
        if file_name in image_hash:
            image_reference["file_name"] = "images/" + image_hash[file_name]

    validate(weedcoco, image_dir)
    new_dataset_dir = dataset_dir / "weedcoco.json"
    with (new_dataset_dir).open("w") as out:
        json.dump(weedcoco, out, indent=4)
Example #2
0
def main(args=None):
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument("--voc-dir", required=True, default=".", type=Path)
    ap.add_argument("--image-dir", required=True, type=Path)
    ap.add_argument(
        "--category-name-map",
        type=Path,
        help="JSON or YAML mapping of VOC names to WeedCOCO category names",
    )
    ap.add_argument("--agcontext-path", type=Path)
    ap.add_argument("--collection-path", type=Path)
    ap.add_argument("--validate", action="store_true", default=False)
    ap.add_argument("-o",
                    "--out-path",
                    default="coco_from_voc.json",
                    type=Path)
    args = ap.parse_args(args)

    if args.category_name_map:
        category_name_map = load_json_or_yaml(args.category_name_map)
        keys, values = zip(*category_name_map.items())
        categories = [{
            "id": i,
            "name": value
        } for i, value in enumerate(values)]
        category_mapping = {key: i for i, key in enumerate(keys)}
    else:
        categories = None
        category_mapping = None

    coco = voc_to_coco(args.voc_dir,
                       args.image_dir,
                       category_mapping=category_mapping)

    if not coco["images"]:
        ap.error(f"Found no .xml files in {args.voc_dir}")

    if categories is not None:
        coco["categories"] = categories
    if args.agcontext_path:
        add_agcontext_from_file(coco, args.agcontext_path)
    if args.collection_path:
        add_collection_from_file(coco, args.collection_path)

    if args.validate:
        validate(coco)

    with args.out_path.open("w") as out:
        json.dump(coco, out, indent=4)
Example #3
0
def main(args=None):
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument("--image-dir", required=True, type=Path)
    ap.add_argument(
        "--mask-dir",
        required=True,
        type=Path,
        help=
        "Filenames should be identical to those in image-dir, with extensions replaced by PNG",
    )
    ap.add_argument("-P", "--path-to-mask-pattern", default=None)
    ap.add_argument(
        "-C",
        "--category-map",
        required=True,
        type=Path,
        help=
        ("JSON or YAML mapping of colors (RRGGBB) to WeedCOCO category names. "
         "The background color should not be mapped."),
    )
    ap.add_argument("--agcontext-path", type=Path)
    ap.add_argument("--collection-path", type=Path)
    ap.add_argument("--validate", action="store_true", default=False)
    ap.add_argument("-o",
                    "--out-path",
                    default="coco_from_mask.json",
                    type=Path)
    args = ap.parse_args(args)

    color_to_category_map = load_json_or_yaml(args.category_map)
    coco = masks_to_coco(
        args.image_dir,
        args.mask_dir,
        color_to_category_map,
        image_to_mask_pattern=args.path_to_mask_pattern,
    )

    if args.agcontext_path:
        add_agcontext_from_file(coco, args.agcontext_path)
    if args.collection_path:
        add_collection_from_file(coco, args.collection_path)

    if args.validate:
        validate(coco)

    with args.out_path.open("w") as out:
        json.dump(coco, out, indent=4)
Example #4
0
def upload(request):
    if request.method == "POST":
        user_id = request.user.id
        images = []
        file_weedcoco = request.FILES["weedcoco"]
        weedcoco_json = json.load(file_weedcoco)
        validate(weedcoco_json)
        for image_reference in weedcoco_json["images"]:
            images.append(image_reference["file_name"].split("/")[-1])
        upload_dir, upload_id = setup_upload_dir(
            os.path.join(UPLOAD_DIR, str(user_id)))
        weedcoco_path = store_tmp_weedcoco(file_weedcoco, upload_dir)
        create_upload_entity(weedcoco_path, upload_id, user_id)
        return HttpResponse(
            json.dumps({
                "upload_id": upload_id,
                "images": images
            }))
    else:
        return HttpResponse("Only support POST request")
Example #5
0
 def submit(cls, request):
     if not request.method == "POST":
         return HttpResponseNotAllowed(request.method)
     user = request.user
     if not (user and user.is_authenticated):
         return HttpResponseForbidden("You dont have access to proceed")
     try:
         store_id = request.POST[cls.id_name]
         if "/" in store_id:
             return HttpResponseBadRequest("Bad id")
         images = []
         coco_json = cls.convert_to_coco(
             Path(os.path.join(UPLOAD_DIR, str(user.id), store_id)), request
         )
         validate(coco_json, schema="coco")
         for image_reference in coco_json["images"]:
             images.append(image_reference["file_name"].split("/")[-1])
         categories = [
             parse_category_name(category) for category in coco_json["categories"]
         ]
         upload_dir, upload_id = setup_upload_dir(
             os.path.join(UPLOAD_DIR, str(user.id))
         )
         store_tmp_voc_coco(coco_json, upload_dir)
         create_upload_entity(upload_id, user.id)
     except JsonValidationError as e:
         traceback.print_exc()
         return json_validation_response(e)
     except Exception as e:
         traceback.print_exc()
         return HttpResponseBadRequest(str(e))
     else:
         return HttpResponse(
             json.dumps(
                 {"upload_id": upload_id, "images": images, "categories": categories}
             )
         )
Example #6
0
def main(args=None):
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument(
        "--mask-dir",
        required=True,
        type=Path,
        help=
        "Filenames should be identical to those in image-dir, with extensions replaced by PNG",
    )
    image_dir_meg = ap.add_mutually_exclusive_group(required=True)
    image_dir_meg.add_argument("--image-dir", type=Path)
    image_dir_meg.add_argument(
        "--image-ext",
        type=str,
        help=("The filename extension (excl .) to apply to mask file names "
              "to generate an image file name."),
    )

    cat_group = ap.add_mutually_exclusive_group(required=True)
    cat_group.add_argument(
        "-C",
        "--category-map",
        type=Path,
        help=
        ("JSON or YAML mapping of colors (RRGGBB) to WeedCOCO category names. "
         "The background color should not be mapped."),
    )
    cat_group.add_argument(
        "-U",
        "--unmapped-on-black",
        action="store_const",
        dest="background_if_unmapped",
        const="000000",
        help=(
            "Do not map the colours, and output hex colours as COCO category "
            "names. Assume the background is black."),
    )

    ap.add_argument("--agcontext-path", type=Path)
    ap.add_argument("--metadata-path", type=Path)
    ap.add_argument("--validate", action="store_true", default=False)
    ap.add_argument("-o",
                    "--out-path",
                    default="coco_from_mask.json",
                    type=Path)
    ap.add_argument(
        "-P",
        "--path-to-mask-pattern",
        default=None,
        help=
        "Pattern to extract mask name from image path when using --image-dir",
    )
    ap.add_argument(
        "--on-missing-mask",
        choices={"skip", "warn", "error"},
        help=("Whether to 'skip', 'warn', or 'error' if the matching "
              "mask can not be found when --image-dir is used"),
    )
    args = ap.parse_args(args)

    if args.image_ext:
        path_pairs = generate_paths_from_mask_only(args.mask_dir,
                                                   args.image_ext)
        check_consistent_dimensions = False
    else:
        path_pairs = generate_mask_and_image_paths(
            args.image_dir,
            args.mask_dir,
            image_to_mask_pattern=args.path_to_mask_pattern,
            on_missing_mask=args.on_missing_mask,
        )
        check_consistent_dimensions = True

    if args.category_map is None:
        color_to_category_map = None
    else:
        color_to_category_map = load_json_or_yaml(args.category_map)
    coco = masks_to_coco(
        path_pairs,
        color_to_category_map,
        check_consistent_dimensions=check_consistent_dimensions,
        background_if_unmapped=args.background_if_unmapped,
    )

    if args.agcontext_path:
        add_agcontext_from_file(coco, args.agcontext_path)
    if args.metadata_path:
        add_metadata_from_file(coco, args.metadata_path)

    if args.validate:
        validate(coco)

    with args.out_path.open("w") as out:
        json.dump(coco, out, indent=4)