|
| 1 | +"""Shared stateless utility function library for chipping images""" |
| 2 | + |
| 3 | +import io |
| 4 | +import json |
| 5 | +import os |
| 6 | +from itertools import product |
| 7 | +from typing import Dict, List |
| 8 | + |
| 9 | +import boto3 |
| 10 | +import numpy as np |
| 11 | +from botocore.exceptions import ClientError |
| 12 | +from PIL import Image |
| 13 | + |
| 14 | +from .constants import ( |
| 15 | + ANNOTATION_LOCATION_KEY, |
| 16 | + BOX_TYPE, |
| 17 | + GEOMETRY_KEY, |
| 18 | + HEIGHT_KEY, |
| 19 | + IMAGE_LOCATION_KEY, |
| 20 | + LABEL_KEY, |
| 21 | + TYPE_KEY, |
| 22 | + WIDTH_KEY, |
| 23 | + X_KEY, |
| 24 | + Y_KEY, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def split_s3_bucket_key(s3_path: str): |
| 29 | + s3_bucket, s3_key = s3_path.split("//", 1)[-1].split("/", 1) |
| 30 | + return s3_bucket, s3_key |
| 31 | + |
| 32 | + |
| 33 | +def fetch_image(s3_url: str): |
| 34 | + s3_bucket, s3_key = split_s3_bucket_key(s3_url) |
| 35 | + image = Image.open( |
| 36 | + boto3.resource("s3").Bucket(s3_bucket).Object(s3_key).get()["Body"] |
| 37 | + ) |
| 38 | + return image |
| 39 | + |
| 40 | + |
| 41 | +def fetch_chip(ref_id: str): |
| 42 | + """ |
| 43 | + Fetches the locations of the image and its corresponding annotations. |
| 44 | +
|
| 45 | + This function checks if the reference ID starts with "s3" to determine if the |
| 46 | + image and annotations are stored on S3, otherwise it checks the local filesystem. |
| 47 | + If the image or annotations do not exist, it returns None for their locations. |
| 48 | +
|
| 49 | + Args: |
| 50 | + ref_id (str): The reference ID for the image and annotations. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + A tuple containing the location of the image and the annotations. |
| 54 | + If either is not found, None is returned in their place. |
| 55 | + """ |
| 56 | + image_loc = None |
| 57 | + annotation_loc = None |
| 58 | + if ref_id.startswith("s3"): |
| 59 | + s3_bucket, s3_key = split_s3_bucket_key(ref_id) |
| 60 | + try: |
| 61 | + boto3.resource("s3").Bucket(s3_bucket).Object( |
| 62 | + s3_key + ".jpeg" |
| 63 | + ).load() |
| 64 | + image_loc = ref_id + ".jpeg" |
| 65 | + except ClientError: |
| 66 | + return None, None |
| 67 | + try: |
| 68 | + boto3.resource("s3").Bucket(s3_bucket).Object( |
| 69 | + s3_key + ".json" |
| 70 | + ).load() |
| 71 | + annotation_loc = ref_id + ".json" |
| 72 | + except ClientError: |
| 73 | + return image_loc, None |
| 74 | + else: |
| 75 | + if os.path.exists(ref_id + ".jpeg"): |
| 76 | + image_loc = ref_id + ".jpeg" |
| 77 | + if os.path.exists(ref_id + ".json"): |
| 78 | + annotation_loc = ref_id + ".json" |
| 79 | + return image_loc, annotation_loc |
| 80 | + |
| 81 | + |
| 82 | +def write_chip( |
| 83 | + ref_id: str, image: Image.Image, annotations: List[Dict[str, str]] |
| 84 | +): |
| 85 | + if ref_id.startswith("s3"): |
| 86 | + s3_bucket, s3_key = split_s3_bucket_key(ref_id) |
| 87 | + byteio = io.BytesIO() |
| 88 | + image.save(byteio, format="jpeg") |
| 89 | + byteio.seek(0) |
| 90 | + boto3.resource("s3").Bucket(s3_bucket).Object( |
| 91 | + s3_key + ".jpeg" |
| 92 | + ).upload_fileobj(byteio) |
| 93 | + annotation_loc = None |
| 94 | + if len(annotations) > 0: |
| 95 | + boto3.resource("s3").Bucket(s3_bucket).Object( |
| 96 | + s3_key + ".json" |
| 97 | + ).put( |
| 98 | + Body=json.dumps(annotations, ensure_ascii=False).encode( |
| 99 | + "UTF-8" |
| 100 | + ), |
| 101 | + ContentType="application/json", |
| 102 | + ) |
| 103 | + annotation_loc = ref_id + ".json" |
| 104 | + return ref_id + ".jpeg", annotation_loc |
| 105 | + else: |
| 106 | + dirs = ref_id.rsplit("/", 1)[0] |
| 107 | + os.makedirs(dirs, exist_ok=True) |
| 108 | + image_loc = ref_id + ".jpeg" |
| 109 | + annotation_loc = None |
| 110 | + image.save(image_loc) |
| 111 | + if len(annotations) > 0: |
| 112 | + annotation_loc = ref_id + ".json" |
| 113 | + with open(annotation_loc, "w", encoding="utf-8") as f: |
| 114 | + json.dump(annotations, f, ensure_ascii=False) |
| 115 | + return image_loc, annotation_loc |
| 116 | + |
| 117 | + |
| 118 | +def generate_offsets(w: int, h: int, chip_size: int, stride_size: int): |
| 119 | + xs = np.arange(0, w - stride_size, chip_size - stride_size) |
| 120 | + ys = np.arange(0, h - stride_size, chip_size - stride_size) |
| 121 | + if len(xs) > 1: |
| 122 | + xs = np.round(xs * (w - chip_size) / xs[-1]).astype(int) |
| 123 | + if len(ys) > 1: |
| 124 | + ys = np.round(ys * (h - chip_size) / ys[-1]).astype(int) |
| 125 | + yield from product(ys, xs) |
| 126 | + |
| 127 | + |
| 128 | +def chip_annotations(data, x0: int, y0: int, x1: int, y1: int): |
| 129 | + """ |
| 130 | + Adjusts the annotations to fit within the chip defined by the rectangle |
| 131 | + with top-left corner (x0, y0) and bottom-right corner (x1, y1). |
| 132 | +
|
| 133 | + Parameters: |
| 134 | + data: List of annotation dictionaries to be adjusted. |
| 135 | + x0: The x-coordinate of the top-left corner of the chip. |
| 136 | + y0: The y-coordinate of the top-left corner of the chip. |
| 137 | + x1: The x-coordinate of the bottom-right corner of the chip. |
| 138 | + y1: The y-coordinate of the bottom-right corner of the chip. |
| 139 | +
|
| 140 | + Returns: |
| 141 | + A list of adjusted annotation dictionaries that fit within the chip. |
| 142 | + """ |
| 143 | + annotations = [] |
| 144 | + X_1_KEY = "x1" |
| 145 | + Y_1_KEY = "y1" |
| 146 | + for annotation in data: |
| 147 | + geometry = annotation[GEOMETRY_KEY].copy() |
| 148 | + geometry[X_1_KEY] = geometry[X_KEY] + geometry[WIDTH_KEY] |
| 149 | + geometry[Y_1_KEY] = geometry[Y_KEY] + geometry[HEIGHT_KEY] |
| 150 | + geometry[X_KEY] = max(min(geometry[X_KEY], x1), x0) - x0 |
| 151 | + geometry[X_1_KEY] = max(min(geometry[X_1_KEY], x1), x0) - x0 |
| 152 | + geometry[Y_KEY] = max(min(geometry[Y_KEY], y1), y0) - y0 |
| 153 | + geometry[Y_1_KEY] = max(min(geometry[Y_1_KEY], y1), y0) - y0 |
| 154 | + geometry[WIDTH_KEY] = geometry[X_1_KEY] - geometry[X_KEY] |
| 155 | + geometry[HEIGHT_KEY] = geometry[Y_1_KEY] - geometry[Y_KEY] |
| 156 | + geometry["area"] = geometry[WIDTH_KEY] * geometry[HEIGHT_KEY] |
| 157 | + if geometry["area"] > 0: |
| 158 | + annotations.append( |
| 159 | + { |
| 160 | + LABEL_KEY: annotation[LABEL_KEY], |
| 161 | + TYPE_KEY: BOX_TYPE, |
| 162 | + GEOMETRY_KEY: { |
| 163 | + X_KEY: geometry[X_KEY], |
| 164 | + Y_KEY: geometry[Y_KEY], |
| 165 | + WIDTH_KEY: geometry[WIDTH_KEY], |
| 166 | + HEIGHT_KEY: geometry[HEIGHT_KEY], |
| 167 | + }, |
| 168 | + } |
| 169 | + ) |
| 170 | + return annotations |
| 171 | + |
| 172 | + |
| 173 | +def process_chip(chip_arg): |
| 174 | + ( |
| 175 | + offset, |
| 176 | + chip_size, |
| 177 | + w, |
| 178 | + h, |
| 179 | + item_ref_id, |
| 180 | + cache_directory, |
| 181 | + image, |
| 182 | + annotations, |
| 183 | + ) = chip_arg |
| 184 | + x0, y0 = map(int, offset) |
| 185 | + x1 = min(x0 + chip_size, w) |
| 186 | + y1 = min(y0 + chip_size, h) |
| 187 | + ref_id = f"{cache_directory}/{item_ref_id}_{x0}_{y0}_{x1}_{y1}" |
| 188 | + chipped_image_loc, chipped_annotation_loc = fetch_chip(ref_id) |
| 189 | + if chipped_image_loc: |
| 190 | + return { |
| 191 | + IMAGE_LOCATION_KEY: chipped_image_loc, |
| 192 | + ANNOTATION_LOCATION_KEY: chipped_annotation_loc, |
| 193 | + } |
| 194 | + chipped_image = image.crop((x0, y0, x1, y1)) |
| 195 | + chipped_annotations = chip_annotations(annotations, x0, y0, x1, y1) |
| 196 | + chipped_image_loc, chipped_annotation_loc = write_chip( |
| 197 | + ref_id, chipped_image, chipped_annotations |
| 198 | + ) |
| 199 | + return { |
| 200 | + IMAGE_LOCATION_KEY: chipped_image_loc, |
| 201 | + ANNOTATION_LOCATION_KEY: chipped_annotation_loc, |
| 202 | + } |
0 commit comments