|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "!pip install ibm-cos-sdk" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import os\n", |
| 19 | + "import shutil\n", |
| 20 | + "import json\n", |
| 21 | + "import uuid\n", |
| 22 | + "\n", |
| 23 | + "import ibm_boto3" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "bucket = os.getenv(\"BUCKET\", \"\")\n", |
| 33 | + "access_key_id = os.getenv(\"ACCESS_KEY_ID\", \"\")\n", |
| 34 | + "secret_access_key = os.getenv(\"SECRET_ACCESS_KEY\", \"\")\n", |
| 35 | + "endpoint_url = os.getenv(\"ENDPOINT_URL\", \"\")" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": null, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "cos = ibm_boto3.resource(\"s3\",\n", |
| 45 | + " aws_access_key_id=access_key_id,\n", |
| 46 | + " aws_secret_access_key=secret_access_key,\n", |
| 47 | + " endpoint_url=endpoint_url\n", |
| 48 | + ")\n", |
| 49 | + "\n", |
| 50 | + "# load the annotations\n", |
| 51 | + "try:\n", |
| 52 | + " annotations = json.loads(cos.Object(bucket, \"_annotations.json\").get()[\"Body\"].read())[\"annotations\"]\n", |
| 53 | + "except Exception as e:\n", |
| 54 | + " print(\"Unable to retrieve annotations: {}\".format(e))" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "data_dir = \"data\"\n", |
| 64 | + "os.makedirs(data_dir)\n", |
| 65 | + "\n", |
| 66 | + "# create a set of labels and then turn it into a list to remove dupelicates\n", |
| 67 | + "labels = list({annotation[\"label\"] for image in annotations.values() for annotation in image})\n", |
| 68 | + "\n", |
| 69 | + "for label in labels:\n", |
| 70 | + " # find a list of images with the given label\n", |
| 71 | + " image_list = [image_name for image_name in annotations.keys() for annotation in annotations[image_name] if annotation[\"label\"] == label]\n", |
| 72 | + "\n", |
| 73 | + " # make directory for the label to store images in\n", |
| 74 | + " train_label_dir = os.path.join(data_dir, label)\n", |
| 75 | + " os.makedirs(train_label_dir)\n", |
| 76 | + "\n", |
| 77 | + " # move images to the their label folder\n", |
| 78 | + " for im in image_list:\n", |
| 79 | + " try:\n", |
| 80 | + " extension = os.path.splitext(im)[1]\n", |
| 81 | + " cos.meta.client.download_file(bucket, im, os.path.join(train_label_dir, str(uuid.uuid4()) + extension))\n", |
| 82 | + " except Exception as e:\n", |
| 83 | + " print(\"Error: {}, skipping {}...\".format(e, im))" |
| 84 | + ] |
| 85 | + } |
| 86 | + ], |
| 87 | + "metadata": { |
| 88 | + "kernelspec": { |
| 89 | + "display_name": "Python 3", |
| 90 | + "language": "python", |
| 91 | + "name": "python3" |
| 92 | + }, |
| 93 | + "language_info": { |
| 94 | + "codemirror_mode": { |
| 95 | + "name": "ipython", |
| 96 | + "version": 3 |
| 97 | + }, |
| 98 | + "file_extension": ".py", |
| 99 | + "mimetype": "text/x-python", |
| 100 | + "name": "python", |
| 101 | + "nbconvert_exporter": "python", |
| 102 | + "pygments_lexer": "ipython3", |
| 103 | + "version": "3.7.9" |
| 104 | + } |
| 105 | + }, |
| 106 | + "nbformat": 4, |
| 107 | + "nbformat_minor": 4 |
| 108 | +} |
0 commit comments