mirror of
https://github.com/Laurent2916/REVA-QCAV.git
synced 2024-11-08 22:42:02 +00:00
178 lines
785 KiB
Plaintext
178 lines
785 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from PIL import Image\n",
|
||
|
"import numpy as np\n",
|
||
|
"\n",
|
||
|
"import albumentations as A\n",
|
||
|
"\n",
|
||
|
"%config InlineBackend.figure_formats = ['svg']\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"%matplotlib inline\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"957.104545pt\" height=\"578.444422pt\" viewBox=\"0 0 957.104545 578.444422\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n <cc:Work>\n <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n <dc:date>2022-07-07T10:16:03.003643</dc:date>\n <dc:format>image/svg+xml</dc:format>\n <dc:creator>\n <cc:Agent>\n <dc:title>Matplotlib v3.5.2, https://matplotlib.org/</dc:title>\n </cc:Agent>\n </dc:creator>\n </cc:Work>\n </rdf:RDF>\n </metadata>\n <defs>\n <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 578.444422 \nL 957.104545 578.444422 \nL 957.104545 0 \nL 0 0 \nL 0 578.444422 \nz\n\" style=\"fill: none\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 39.65 554.566297 \nL 402.05 554.566297 \nL 402.05 10.966297 \nL 39.65 10.966297 \nz\n\" style=\"fill: #ffffff\"/>\n </g>\n <g clip-path=\"url(#p37db7c6a2f)\">\n <image xlink:href=\"data:image/png;base64,\niVBORw0KGgoAAAANSUhEUgAAAWsAAAIgCAYAAABDMUhKAAEAAElEQVR4nGT9V49lWZbfCf62OFpcZdLdwyMiMyOrkiwxzW6yR7DBt+bLzNcdDBoYEGj0NCiaNSxWsVJEhvBwd9N25dFni3nYN4NiDIgwRMDc3OzefdZe66+W+H/81Xu/rErsPLGsaxCS7W4HQvPN+1t++PgZJxXXF2ted0eWVc7xeMQ5y6oumYxDSsk0zzjn+fL2ipf9kSpP2Z9akJKX/QkvFN57qjRCKo3AgwAlJWVZcDg0XC8LJgNJrHjZHVBpwbLM+e23f2C0jpvVEmMtszHUZUVd5LR9y6ntmSwoCWmcsChSkIos0XRtS9O2vB4b6sWaP//yljRWdNPI//Jv/xNd1/Obd5dIFSOFYJonjHVopfE4xsnzi3dXeGfZnlq2xxNNN5BGijyLsU5ys8jRUhFnKf/733/H/+0vfomW8N3TAWMdmRJMs6EZRv7Zn32NnQfarudff/sTb6+u+L//i3+G946H7Y7f/f4P/Otv77ndbPj65oKyKDicThybBoSkSBN0pDh1I5kW7PZH/vKbr6nKgp8envhXf/t7kiTjy8slxswkccI0z+RpTDsatJQcm4Yvri+IlMAYy9P+BM5wUWfMTnO5KGmGgcOp4Ve/+AXT0NG0Lb/79ESWZFSJoi4S5nlmNp46i9Fa8rtPj3STxzvPX/7yHfM0M1iPVpI0jlnmMfvDgR7Nv/hnf0kcxZhp5H/7m7/nb/7wgfeXG1IFSitG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PVIqkjji/fUFx35i185kacxgDO9vr0iTlGmaSdKCh13H+zdvqPOYOI44tCNVWZPFmmmeGIYB5xxf3txwc7GmmSxWwOw8SikircgizeWiQnrDZAweiJKEN1drutmRxxqkYDCwqgsiKRjGkdl6Xo89D7uGQ9sTxRFSQD8OnJqOIot5e7FCCEkkPUkccXOx4Xqz5g93r4xW8PXNitfjiX/3h5/43/7ud/x0f0/bnNACyjQiVo7H1z2jsdRFzu7UM44TiyLj3c01k/Xsm55hmjm0Pe0w8HwaSGONEAIjNNeXa7z3CKXOf7Zg3wzsuomfnvdYD7OZmIzh3UVNXZY4B955pmnk5XCizpNwiTmPtXD/9MrTbs80D1yvl5R5wbJMKfKM531LHGkulxWbqgQc0luEnxnGiXVd88XFgtk4tJJ0fceh7dk3Dc/7E8+7PVpJ1suaKi/I0xTnHKmWdJOhnwyTtZymmaIoiJOUi9WaRVkxGMthMMxmZrVYcOwmHg8tSknKNOJ2s+DNZsGiLNBK0fYdp66nGyauLjZEWiJwFElMFivyrGB36tgfjxy7kXkaKbOEIktIonDZd8PIskhpuoFj1zMZy2AcSaQBiZCSMkvC7zoatocW5x1dP9FPc6jM3vNffgigTiOUlBy7ESEk02wYp5Ei1WgJ7zY1v769YJGloYnzAg9M1qMF7E4ntscjPz5tGaeRy2XO++sFsZIM04Q3hkSFulJlOess5vv7V767e8YLQaw1Si
|
||
|
"text/plain": [
|
||
|
"<Figure size 1296x720 with 2 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"img = Image.open(\"/tmp/extract/photo.jpg\").convert(\"RGBA\")\n",
|
||
|
"mask = Image.open(\"/tmp/extract/MASK.PNG\").convert(\"LA\")\n",
|
||
|
"\n",
|
||
|
"plt.figure(figsize=(18, 10))\n",
|
||
|
"\n",
|
||
|
"plt.subplot(1, 2, 1)\n",
|
||
|
"plt.imshow(img)\n",
|
||
|
"\n",
|
||
|
"plt.subplot(1, 2, 2)\n",
|
||
|
"plt.imshow(mask)\n",
|
||
|
"\n",
|
||
|
"ax = plt.gca()\n",
|
||
|
"ax.set_facecolor('xkcd:salmon')\n",
|
||
|
"\n",
|
||
|
"plt.show()\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"840.665468pt\" height=\"578.186649pt\" viewBox=\"0 0 840.665468 578.186649\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n <cc:Work>\n <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n <dc:date>2022-07-07T10:22:39.796028</dc:date>\n <dc:format>image/svg+xml</dc:format>\n <dc:creator>\n <cc:Agent>\n <dc:title>Matplotlib v3.5.2, https://matplotlib.org/</dc:title>\n </cc:Agent>\n </dc:creator>\n </cc:Work>\n </rdf:RDF>\n </metadata>\n <defs>\n <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 578.186649 \nL 840.665468 578.186649 \nL 840.665468 0 \nL 0 0 \nL 0 578.186649 \nz\n\" style=\"fill: none\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 33.2875 257.799433 \nL 285.610922 257.799433 \nL 285.610922 10.708524 \nL 33.2875 10.708524 \nz\n\" style=\"fill: #ffffff\"/>\n </g>\n <g clip-path=\"url(#p3abbf107f0)\">\n <image xlink:href=\"data:image/png;base64,\niVBORw0KGgoAAAANSUhEUgAAAP0AAAD4CAYAAAAn+OBPAAEAAElEQVR4nJz9WZckS3ImiH0iqma+xJZ511pQAAaN7pkh55CHh+ThE///yzxMnx42gW6sVaiqu+Yem7upCh/kE1H1yIt5YFTlzYwId3MzVVk++WRR+e2vvjBVg6HBDOjdsPWO3g29GVozdAAwg4jAAAj8PwIZPzdDfJkBHQYzwLrBuqGboXfAYBAAZh3jy6/qvwFgHSICQKAqgChqFSxLRa0rlmXBulasa0FRRa0VogpAAQAi4u83g/EPwJ8JYL3D4nMNgMQ9+U9V/XcqgML8GdFhraP3M7btjL41WO/oAFrv6B2xMnweAKYwPls3g+VrOiRWgm/xNZTx1lhOsXyNAjCpec9FlVdT+Mp2qCggBaLCt5uvrCoEimYNgo6ODjHJ++y8kkFg3T/SRNBbR+dexT2pKMyAJgYxA0wgKtwDrvf0p5aC/d73TMV8l8zXwGXP+JkCkQ4RYEPD1eGIv/7d7/Dr33yFKh2n0yOqCATd79War4qoL5v43llrEDMupfDeFaICFeFn+71C/Xq+9wWtd+4FYB0wNNRSuaeznEjKlJnvUbxXxKDiew4YXOzUxY2yBvP3Cqb7pAwIOkzG54kWoCuX09dM9PI9ZuaiokAR/7ep6yu6wKBQNSgENaTMlSzUT1MpRZALaObCo35bk8ra9F5xQU1ZnR5LQh9eGgrJ/1q87sXPJf4t8M1TgYhCRdNA5OXDNPE6RUreQ08DcPERs3hQuP05/H5ceI1PHYuP7gLb+3QFi7U0v1mbFDg1fPxl033Pz61huMQ32L8HRAqf1Q2ciqCLudLF9yaUB4GYca0EMEGXwh3prmQQGmN/9s5nExOKue+vwZ2AGyZX9M2M9+xmpBSFmaG3DnT//KUW7NaK3VpdLnpHGFS3XX6NTkML6ygq+ObrL/EXv/0L3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kUvfPM+vovWGDoEiDbS44ubihZgY3CpI+ND1chrMZn3UaTRoWjZjVF683F9xOC97hGw/GtS5Xwnh3PK+IQotACcudo6DnVoXSm0AEpSgKEYHro0C1QtRQ1Vclfg51hKBSfHMJ5YTPCkI6/65C0FA0PLfAxGiQCgoC+tObW4eaw9iGhm5uOKy7EWvdFSg8mqGjSKFBKyhaiAY2SHfn0GlBOj1X7/7ethmOhwN+85uv8KtvvsJ+V2C9AehEWRFjx/5w31ShZmjUoiaBIjrljHuKsR4uE87D9N4hxb24WXODwmvVUgAx9L65Y1NNOC8mGZerumF0hOW8laMKQLG4MZGWBlWpIB3DwRhl0sOpgUQM6nyJiK+DOpqiDfHXEcHA/F78ig0qC70vEQSmdZOOCqTj9odx8+Pwog2PeQm+B2xJ8i1fFuCc/02MPhx0fIbZ9Ms0FIFxIr4ZRiG+eje0bYMUIbHhC1v4/Ys7SLfuJE5goOmSNCRiTiRBDErysah/Xu+d1k/zHgMmG41R8g7qm6AkW0qttP6CQuuuVXnPbhQCElcpCI4GRWks/D
|
||
|
"text/plain": [
|
||
|
"<Figure size 1296x720 with 4 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"(434, 425)\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"box = mask.getbbox()\n",
|
||
|
"\n",
|
||
|
"crop_img = img.crop(box)\n",
|
||
|
"crop_mask = mask.crop(box)\n",
|
||
|
"\n",
|
||
|
"plt.figure(figsize=(18, 10))\n",
|
||
|
"\n",
|
||
|
"plt.subplot(2, 2, 1)\n",
|
||
|
"plt.imshow(crop_img)\n",
|
||
|
"\n",
|
||
|
"plt.subplot(2, 2, 2)\n",
|
||
|
"plt.imshow(crop_mask)\n",
|
||
|
"\n",
|
||
|
"ax = plt.gca()\n",
|
||
|
"ax.set_facecolor('xkcd:salmon')\n",
|
||
|
"\n",
|
||
|
"empty = Image.fromarray(np.zeros(crop_img.size), \"RGBA\")\n",
|
||
|
"empty.paste(crop_img, crop_mask)\n",
|
||
|
"\n",
|
||
|
"plt.subplot(2, 2, 3)\n",
|
||
|
"plt.imshow(empty.resize((100, 100)))\n",
|
||
|
"\n",
|
||
|
"plt.subplot(2, 2, 4)\n",
|
||
|
"plt.imshow(crop_mask.resize((100, 100)))\n",
|
||
|
"\n",
|
||
|
"ax = plt.gca()\n",
|
||
|
"ax.set_facecolor('xkcd:salmon')\n",
|
||
|
"\n",
|
||
|
"plt.show()\n",
|
||
|
"\n",
|
||
|
"print(crop_img.size)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 43,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from pathlib import Path\n",
|
||
|
"\n",
|
||
|
"spheres_to_extract_dir = \"/home/lilian/data_disk/lfainsin/test/\"\n",
|
||
|
"\n",
|
||
|
"spheres = list(Path(spheres_to_extract_dir).glob(\"**/*.jpg\"))\n",
|
||
|
"\n",
|
||
|
"parents = [path.parent for path in spheres]\n",
|
||
|
"parents = set(parents)\n",
|
||
|
"\n",
|
||
|
"for parent in parents:\n",
|
||
|
" mask_path = parent.joinpath(\"MASK.PNG\")\n",
|
||
|
" mask = Image.open(mask_path).convert(\"LA\")\n",
|
||
|
" box = mask.getbbox()\n",
|
||
|
" crop_mask = mask.crop(box)\n",
|
||
|
"\n",
|
||
|
" filename = Path(\"/tmp/saves/\" + str(mask_path).strip(spheres_to_extract_dir))\n",
|
||
|
" filename.parent.mkdir(parents=True, exist_ok=True)\n",
|
||
|
" crop_mask.save(filename)\n",
|
||
|
"\n",
|
||
|
" spheres = list(parent.glob(\"*.jpg\"))\n",
|
||
|
" for sphere in spheres:\n",
|
||
|
" img = Image.open(sphere).convert(\"RGB\")\n",
|
||
|
" crop_img = img.crop(box)\n",
|
||
|
"\n",
|
||
|
" filename = Path(\"/tmp/saves/\" + str(sphere).strip(spheres_to_extract_dir))\n",
|
||
|
" filename.parent.mkdir(parents=True, exist_ok=True)\n",
|
||
|
" crop_img.save(filename)\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3.8.0 ('.venv': poetry)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.8.0"
|
||
|
},
|
||
|
"orig_nbformat": 4,
|
||
|
"vscode": {
|
||
|
"interpreter": {
|
||
|
"hash": "dc80d2c03865715c8671359a6bf138f6c8ae4e26ae025f2543e0980b8db0ed7e"
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|