|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import json\n", |
| 10 | + "import numpy as np\n", |
| 11 | + "import imageio\n", |
| 12 | + "import os\n", |
| 13 | + "import tqdm\n", |
| 14 | + "from glob import glob\n", |
| 15 | + "import shutil\n", |
| 16 | + "import matplotlib.pyplot as plt\n", |
| 17 | + "plt.rcParams['figure.figsize']=[12, 12]\n", |
| 18 | + "from matplotlib.patches import Polygon\n", |
| 19 | + "from PIL import Image, ImageDraw, ImageOps\n", |
| 20 | + "import cv2 as cv2\n", |
| 21 | + "import json\n", |
| 22 | + "import requests\n", |
| 23 | + "from datetime import datetime" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": 132, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "def find_poly_in_instance(img_filename, label_dict):\n", |
| 33 | + " shape_attributes = label_dict.get(img_filename)\n", |
| 34 | + " polygons = []\n", |
| 35 | + " for sh_attr in shape_attributes:\n", |
| 36 | + " xy = list(zip(sh_attr.get('shape_attributes')['all_points_x'], sh_attr.get('shape_attributes')['all_points_y']))\n", |
| 37 | + " polygons.append(xy)\n", |
| 38 | + "\n", |
| 39 | + " return polygons" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 130, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "url = \" \"\n", |
| 49 | + "response = requests.get(url)\n", |
| 50 | + "data = response.json()" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 4, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [ |
| 58 | + { |
| 59 | + "data": { |
| 60 | + "text/plain": [ |
| 61 | + "27" |
| 62 | + ] |
| 63 | + }, |
| 64 | + "execution_count": 4, |
| 65 | + "metadata": {}, |
| 66 | + "output_type": "execute_result" |
| 67 | + } |
| 68 | + ], |
| 69 | + "source": [ |
| 70 | + "len(data)" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": 111, |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "datetime.now()\n", |
| 80 | + "now = datetime.now()\n", |
| 81 | + "dt_string = now.strftime(\"%Y%m%d-%H%M%S\")\n", |
| 82 | + "\n", |
| 83 | + "folder_name = dt_string\n", |
| 84 | + "export_path = os.path.join('/media/scanvan/web/public/reporting', dt_string)\n", |
| 85 | + "os.makedirs(export_path, exist_ok=True)" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 126, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [ |
| 93 | + { |
| 94 | + "name": "stderr", |
| 95 | + "output_type": "stream", |
| 96 | + "text": [ |
| 97 | + "<ipython-input-126-9d5b9b80684a>:4: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n", |
| 98 | + "Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n", |
| 99 | + " for i in tqdm.tqdm_notebook(range(len(data))):\n" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "data": { |
| 104 | + "application/vnd.jupyter.widget-view+json": { |
| 105 | + "model_id": "ae407ffa21ca49b092f834ca4d784ace", |
| 106 | + "version_major": 2, |
| 107 | + "version_minor": 0 |
| 108 | + }, |
| 109 | + "text/plain": [ |
| 110 | + "HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=27.0), HTML(value='')))" |
| 111 | + ] |
| 112 | + }, |
| 113 | + "metadata": {}, |
| 114 | + "output_type": "display_data" |
| 115 | + }, |
| 116 | + { |
| 117 | + "name": "stdout", |
| 118 | + "output_type": "stream", |
| 119 | + "text": [ |
| 120 | + "\n" |
| 121 | + ] |
| 122 | + } |
| 123 | + ], |
| 124 | + "source": [ |
| 125 | + "root_path = '/media/scanvan/web/public/dev/'\n", |
| 126 | + "SRCS = []\n", |
| 127 | + "MASKS = []\n", |
| 128 | + "for i in tqdm.tqdm_notebook(range(len(data))):\n", |
| 129 | + " \n", |
| 130 | + " im_path = root_path + data[i]['filepath']\n", |
| 131 | + " SRCS.append(im_path)\n", |
| 132 | + " im = Image.open(im_path)\n", |
| 133 | + " width, height = im.size\n", |
| 134 | + " \n", |
| 135 | + " poly = []\n", |
| 136 | + " for order in data[i]['pitchYawOrders']:\n", |
| 137 | + " y = height * order['pitch'] / np.pi\n", |
| 138 | + " x = width * order['yaw'] / ( 2 * np.pi )\n", |
| 139 | + " poly.append( ( x , y ) )\n", |
| 140 | + " \n", |
| 141 | + " mask = Image.new('L', (width, height), 0)\n", |
| 142 | + " ImageDraw.Draw(mask).polygon(poly, outline=1, fill=1)\n", |
| 143 | + " mask_inv = cv2.bitwise_not(np.array(mask) * 255)\n", |
| 144 | + " \n", |
| 145 | + " mask_path = os.path.join('/media/scanvan/web/public/reporting', dt_string, data[i]['viewpointId'] + \"-mask.jpg\")\n", |
| 146 | + " MASKS.append(mask_path)\n", |
| 147 | + " imageio.imwrite(mask_path, mask_inv)\n", |
| 148 | + " \n", |
| 149 | + " # images are re-written at the same path\n", |
| 150 | + " !/home/descombe/Sources/image-suite/bin/image-missing-pixel -s $im_path -m $mask_path -e $im_path -k 16\n", |
| 151 | + " " |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": null, |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [], |
| 159 | + "source": [ |
| 160 | + "# useful to check annotations\n", |
| 161 | + "extent = 0, width, 0, height\n", |
| 162 | + "fig = plt.figure(frameon=False, figsize = (23,23))\n", |
| 163 | + "\n", |
| 164 | + "im1 = plt.imshow(im,\n", |
| 165 | + " extent=extent)\n", |
| 166 | + "\n", |
| 167 | + "im2 = plt.imshow(mask,\n", |
| 168 | + " extent=extent, alpha=0.5)\n", |
| 169 | + "\n", |
| 170 | + "plt.show()" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": null, |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [] |
| 179 | + } |
| 180 | + ], |
| 181 | + "metadata": { |
| 182 | + "kernelspec": { |
| 183 | + "display_name": "Python 3", |
| 184 | + "language": "python", |
| 185 | + "name": "python3" |
| 186 | + }, |
| 187 | + "language_info": { |
| 188 | + "codemirror_mode": { |
| 189 | + "name": "ipython", |
| 190 | + "version": 3 |
| 191 | + }, |
| 192 | + "file_extension": ".py", |
| 193 | + "mimetype": "text/x-python", |
| 194 | + "name": "python", |
| 195 | + "nbconvert_exporter": "python", |
| 196 | + "pygments_lexer": "ipython3", |
| 197 | + "version": "3.8.5" |
| 198 | + } |
| 199 | + }, |
| 200 | + "nbformat": 4, |
| 201 | + "nbformat_minor": 4 |
| 202 | +} |
0 commit comments