{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Install python 3.9 because library rembg require modern python version\n\n!conda create -n py39 python=3.9 anaconda --yes\n!source /opt/conda/bin/activate py39 && conda install -c py39 python -y\n!sudo rm /opt/conda/bin/python3\n!sudo ln -sf /opt/conda/envs/py39/bin/python3 /opt/conda/bin/python3\n!sudo rm /opt/conda/bin/python3.7\n!sudo ln -sf /opt/conda/envs/py39/bin/python3 /opt/conda/bin/python3.7\n!sudo rm /opt/conda/bin/python\n!sudo ln -s /opt/conda/envs/py39/bin/python3 /opt/conda/bin/python\n!python --version","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install python library rembg \n!pip install -q rembg","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import io\nimport math\nimport copy\nimport zipfile\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom rembg import remove, new_session","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_pair(path):\n    plt.figure(figsize=(15, 8))\n    image = cv2.imread(path)\n    image_without_background = copy.copy(remove(image))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image_without_background = cv2.cvtColor(image_without_background, cv2.COLOR_BGRA2RGBA)\n\n    alpha = image_without_background[:,:,3]\n    green = np.array([0, 255, 0, 255], dtype=np.uint8)\n    mask = image_without_background[:,:,3] < 128\n    image_without_background[mask] = green\n\n    plt.subplot(1, 2, 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n\n    plt.subplot(1, 2, 2)\n    plt.imshow(image_without_background)\n    plt.axis(\"off\")\n\nimages = ['0000ae6cbdb1', '000b92c3b098', '0a05cd953754', '0bb6f402ce00']\n    \nfor image in images:\n    show_pair(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{image}.jpg\")","metadata":{},"execution_count":null,"outputs":[]}]}