|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import random |
| 5 | +import shutil |
| 6 | +import threading |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +import superannotate as sa |
| 10 | +from PIL import Image, ImageDraw, ImageFont |
| 11 | + |
| 12 | +random.seed(datetime.now()) |
| 13 | + |
| 14 | + |
| 15 | +def return_json(): |
| 16 | + """returns dict""" |
| 17 | + data_json = { |
| 18 | + 'metadata': |
| 19 | + { |
| 20 | + 'lastAction': |
| 21 | + { |
| 22 | + 'email': 'davita@superannotate.com', |
| 23 | + 'timestamp': 1615381797112 |
| 24 | + }, |
| 25 | + 'width': 1600, |
| 26 | + 'height': 1600, |
| 27 | + 'name': '*.jpg' |
| 28 | + }, |
| 29 | + 'comments': [], |
| 30 | + 'tags': [], |
| 31 | + 'instances': |
| 32 | + [ |
| 33 | + { |
| 34 | + 'type': 'cuboid', |
| 35 | + 'classId': 466471, |
| 36 | + 'className': 'CUBOID', |
| 37 | + 'probability': 100, |
| 38 | + 'points': |
| 39 | + { |
| 40 | + 'f1': |
| 41 | + { |
| 42 | + 'x': random.uniform(300, 1400), |
| 43 | + 'y': random.uniform(300, 1400) |
| 44 | + }, |
| 45 | + 'f2': |
| 46 | + { |
| 47 | + 'x': random.uniform(300, 1400), |
| 48 | + 'y': random.uniform(300, 1400) |
| 49 | + }, |
| 50 | + 'r1': |
| 51 | + { |
| 52 | + 'x': random.uniform(300, 1400), |
| 53 | + 'y': random.uniform(300, 1400) |
| 54 | + }, |
| 55 | + 'r2': |
| 56 | + { |
| 57 | + 'x': random.uniform(300, 1400), |
| 58 | + 'y': random.uniform(300, 1400) |
| 59 | + } |
| 60 | + }, |
| 61 | + 'groupId': 0, |
| 62 | + 'pointLabels': {}, |
| 63 | + 'locked': False, |
| 64 | + 'visible': True, |
| 65 | + 'attributes': [], |
| 66 | + 'trackingId': None, |
| 67 | + 'error': None, |
| 68 | + 'createdAt': '2021-03-10T15: 05: 39.200Z', |
| 69 | + 'createdBy': |
| 70 | + { |
| 71 | + 'email': 'davita@superannotate.com', |
| 72 | + 'role': 'Admin' |
| 73 | + }, |
| 74 | + 'creationType': 'Manual', |
| 75 | + 'updatedAt': '2021-03-10T15: 05: 47.495Z', |
| 76 | + 'updatedBy': |
| 77 | + { |
| 78 | + 'email': 'davita@superannotate.com', |
| 79 | + 'role': 'Admin' |
| 80 | + } |
| 81 | + }, { |
| 82 | + 'type': 'ellipse', |
| 83 | + 'classId': 466473, |
| 84 | + 'className': 'ELLIPSE', |
| 85 | + 'probability': 100, |
| 86 | + 'cx': random.uniform(300, 1400), |
| 87 | + 'cy': random.uniform(300, 1400), |
| 88 | + 'rx': random.uniform(300, 1400), |
| 89 | + 'ry': random.uniform(300, 1400), |
| 90 | + 'angle': 0, |
| 91 | + 'groupId': 0, |
| 92 | + 'pointLabels': {}, |
| 93 | + 'locked': False, |
| 94 | + 'visible': True, |
| 95 | + 'attributes': [], |
| 96 | + 'trackingId': None, |
| 97 | + 'error': None, |
| 98 | + 'createdAt': '2021-03-10T15: 05: 26.962Z', |
| 99 | + 'createdBy': |
| 100 | + { |
| 101 | + 'email': 'davita@superannotate.com', |
| 102 | + 'role': 'Admin' |
| 103 | + }, |
| 104 | + 'creationType': 'Manual', |
| 105 | + 'updatedAt': '2021-03-10T15: 05: 30.690Z', |
| 106 | + 'updatedBy': |
| 107 | + { |
| 108 | + 'email': 'davita@superannotate.com', |
| 109 | + 'role': 'Admin' |
| 110 | + } |
| 111 | + }, { |
| 112 | + 'type': 'bbox', |
| 113 | + 'classId': 466172, |
| 114 | + 'className': 'BBOX', |
| 115 | + 'probability': 100, |
| 116 | + 'points': |
| 117 | + { |
| 118 | + 'x1': random.uniform(300, 1400), |
| 119 | + 'x2': random.uniform(300, 1400), |
| 120 | + 'y1': random.uniform(300, 1400), |
| 121 | + 'y2': random.uniform(300, 1400) |
| 122 | + }, |
| 123 | + 'groupId': 0, |
| 124 | + 'pointLabels': {}, |
| 125 | + 'locked': False, |
| 126 | + 'visible': True, |
| 127 | + 'attributes': [], |
| 128 | + 'trackingId': None, |
| 129 | + 'error': None, |
| 130 | + 'createdAt': '2021-03-10T15: 05: 11.085Z', |
| 131 | + 'createdBy': |
| 132 | + { |
| 133 | + 'email': 'davita@superannotate.com', |
| 134 | + 'role': 'Admin' |
| 135 | + }, |
| 136 | + 'creationType': 'Manual', |
| 137 | + 'updatedAt': '2021-03-10T15: 05: 23.677Z', |
| 138 | + 'updatedBy': |
| 139 | + { |
| 140 | + 'email': 'davita@superannotate.com', |
| 141 | + 'role': 'Admin' |
| 142 | + } |
| 143 | + }, { |
| 144 | + 'type': 'polygon', |
| 145 | + 'classId': 466173, |
| 146 | + 'className': 'POLYGON', |
| 147 | + 'probability': 100, |
| 148 | + 'points': |
| 149 | + [ |
| 150 | + random.uniform(300, 1400), |
| 151 | + random.uniform(300, 1400), |
| 152 | + random.uniform(300, 1400), |
| 153 | + random.uniform(300, 1400), |
| 154 | + random.uniform(300, 1400), |
| 155 | + random.uniform(300, 1400) |
| 156 | + ], |
| 157 | + 'groupId': 0, |
| 158 | + 'pointLabels': {}, |
| 159 | + 'locked': False, |
| 160 | + 'visible': True, |
| 161 | + 'attributes': [], |
| 162 | + 'trackingId': None, |
| 163 | + 'error': None, |
| 164 | + 'createdAt': '2021-03-10T15: 04: 57.385Z', |
| 165 | + 'createdBy': |
| 166 | + { |
| 167 | + 'email': 'davita@superannotate.com', |
| 168 | + 'role': 'Admin' |
| 169 | + }, |
| 170 | + 'creationType': 'Manual', |
| 171 | + 'updatedAt': '2021-03-10T15: 05: 06.794Z', |
| 172 | + 'updatedBy': |
| 173 | + { |
| 174 | + 'email': 'davita@superannotate.com', |
| 175 | + 'role': 'Admin' |
| 176 | + } |
| 177 | + }, { |
| 178 | + 'type': 'polyline', |
| 179 | + 'classId': 466175, |
| 180 | + 'className': 'POLYLINE', |
| 181 | + 'probability': 100, |
| 182 | + 'points': |
| 183 | + [ |
| 184 | + random.uniform(300, 1400), |
| 185 | + random.uniform(300, 1400), |
| 186 | + random.uniform(300, 1400), |
| 187 | + random.uniform(300, 1400), |
| 188 | + random.uniform(300, 1400), |
| 189 | + random.uniform(300, 1400), |
| 190 | + random.uniform(300, 1400), |
| 191 | + random.uniform(300, 1400), |
| 192 | + random.uniform(300, 1400), |
| 193 | + random.uniform(300, 1400), |
| 194 | + random.uniform(300, 1400), |
| 195 | + random.uniform(300, 1400) |
| 196 | + ], |
| 197 | + 'groupId': 0, |
| 198 | + 'pointLabels': {}, |
| 199 | + 'locked': False, |
| 200 | + 'visible': True, |
| 201 | + 'attributes': [], |
| 202 | + 'trackingId': None, |
| 203 | + 'error': None, |
| 204 | + 'createdAt': '2021-03-10T15: 04: 37.760Z', |
| 205 | + 'createdBy': |
| 206 | + { |
| 207 | + 'email': 'davita@superannotate.com', |
| 208 | + 'role': 'Admin' |
| 209 | + }, |
| 210 | + 'creationType': 'Manual', |
| 211 | + 'updatedAt': '2021-03-10T15: 04: 50.880Z', |
| 212 | + 'updatedBy': |
| 213 | + { |
| 214 | + 'email': 'davita@superannotate.com', |
| 215 | + 'role': 'Admin' |
| 216 | + } |
| 217 | + } |
| 218 | + ] |
| 219 | + } |
| 220 | + return data_json |
| 221 | + |
| 222 | + |
| 223 | +def draw_rectangle(draw, width, height, fnt): |
| 224 | + """Gets draw object fills with rectangles""" |
| 225 | + for s_h in range(0, height - 100, 200): |
| 226 | + for s_w in range(0, width - 100, 200): |
| 227 | + draw.rectangle( |
| 228 | + ((s_w + 50, s_h), (s_w + 150, s_h + 100)), fill="yellow" |
| 229 | + ) |
| 230 | + |
| 231 | + |
| 232 | +def create_json(img_name): |
| 233 | + """Creates json for particular image""" |
| 234 | + data_json = return_json() |
| 235 | + data_json['metadata']['name'] = img_name |
| 236 | + with open('annotations/%s___objects.json' % img_name, 'w') as out: |
| 237 | + json.dump(data_json, out) |
| 238 | + |
| 239 | + |
| 240 | +def create_img(width, height, ind, fnt): |
| 241 | + """Creates Image""" |
| 242 | + img = Image.new( |
| 243 | + 'RGB', (height, width), |
| 244 | + color=( |
| 245 | + random.randint(0, 255), random.randint(0, |
| 246 | + 255), random.randint(0, 255) |
| 247 | + ) |
| 248 | + ) |
| 249 | + draw = ImageDraw.Draw(img) |
| 250 | + draw_rectangle(draw, width, height, fnt) |
| 251 | + text_w, text_h = draw.textsize(str(ind)) |
| 252 | + draw.text( |
| 253 | + ((height - text_h) / 4, (width - text_w) / 2), |
| 254 | + "%s" % ind, |
| 255 | + font=fnt, |
| 256 | + fill=(0, 0, 0) |
| 257 | + ) |
| 258 | + img_name = '%s.jpg' % ind |
| 259 | + img.save('images/%s' % img_name) |
| 260 | + create_json(img_name) |
| 261 | + |
| 262 | + |
| 263 | +def generate_images(width, height, count): |
| 264 | + """Main function""" |
| 265 | + fnt = ImageFont.load_default() |
| 266 | + thread_list = [] |
| 267 | + for ind in range(count): |
| 268 | + thread = threading.Thread( |
| 269 | + target=create_img, args=(height, width, ind, fnt) |
| 270 | + ) |
| 271 | + thread_list.append(thread) |
| 272 | + for thread in thread_list: |
| 273 | + thread.start() |
| 274 | + for thread in thread_list: |
| 275 | + thread.join() |
| 276 | + #for ind in range(1000000, count): |
| 277 | + # create_img(height, width, ind, fnt) |
| 278 | + |
| 279 | + |
| 280 | +def sdk_uplaod(project_name, image_path): |
| 281 | + """Upload Images via SA sdk""" |
| 282 | + project_metadata = sa.get_project_metadata(project_name) |
| 283 | + _uploaded, _skipped, _duplicate = sa.upload_images_from_folder_to_project( |
| 284 | + project=project_metadata["name"], folder_path="./images" |
| 285 | + ) |
| 286 | + sa.upload_annotations_from_folder_to_project(project_name, "./annotations") |
| 287 | + |
| 288 | + |
| 289 | +if __name__ == "__main__": |
| 290 | + height = 1600 |
| 291 | + width = 1600 |
| 292 | + count = 100 |
| 293 | + project_name = "Large Dataset2" |
| 294 | + image_path = "./images" |
| 295 | + sa.create_project(project_name, "x", "Vector") |
| 296 | + sa.create_annotation_class(project_name, "CUBOID", "#FFFFFF") |
| 297 | + sa.create_annotation_class(project_name, "BBOX", "#FFFFFF") |
| 298 | + sa.create_annotation_class(project_name, "POLYGON", "#FFFFFF") |
| 299 | + sa.create_annotation_class(project_name, "POLYLINE", "#FFFFFF") |
| 300 | + sa.create_annotation_class(project_name, "ELLIPSE", "#FFFFFF") |
| 301 | + if os.path.exists("./images"): |
| 302 | + shutil.rmtree("./images") |
| 303 | + if os.path.exists("./annotations"): |
| 304 | + shutil.rmtree("./annotations") |
| 305 | + pathlib.Path("./images").mkdir(exist_ok=True) |
| 306 | + pathlib.Path("./annotations").mkdir(exist_ok=True) |
| 307 | + generate_images(width, height, count) |
| 308 | + sdk_uplaod(project_name, image_path) |
0 commit comments