{ "cells": [ { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np\n", "\n", "from PIL import Image\n", "from transparent_background import Remover\n", "import pandas as pd\n", "from tqdm import tqdm\n", "import os" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Settings -> Mode=base-nightly, Device=cuda:0, Torchscript=disabled\n" ] } ], "source": [ "# Load model\n", "# remover = Remover() # default setting\n", "# remover = Remover(mode='fast', jit=True, device='cuda:0', ckpt='~/latest.pth', url=\"https://drive.google.com/file/d/13oBl5MTVcWER3YU4fSxW3ATlVfueFQPY/view?usp=share_link\", ckpt_name=\"ckpt_base.pth\")\n", "remover = Remover(mode=\"base-nightly\") # nightly release checkpoint" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Usage for image\n", "img = Image.open(\"../data/raw/images/egyptian/1953/1953.1-tt.jpg\").convert(\"RGB\") # read image\n", "\n", "out = remover.process(img) # default setting - transparent background\n", "# out = remover.process(img, type='rgba') # same as above\n", "# out = remover.process(img, type='map') # object map only\n", "# out = remover.process(img, type='green') # image matting - green screen\n", "# out = remover.process(img, type='white') # change backround with white color\n", "# out = remover.process(img, type=[255, 0, 0]) # change background with color code [255, 0, 0]\n", "# out = remover.process(img, type='blur') # blur background\n", "# out = remover.process(img, type='overlay') # overlay object map onto the image\n", "# out = remover.process(img, type='samples/background.jpg') # use another image as a background\n", "\n", "# out = remover.process(img, threshold=0.5) # use threhold parameter for hard prediction.\n", "\n", "out.save(\"output.png\") # save result" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "img_df = pd.read_csv(\"../data/processed/OM_file_to_obj.csv\")\n", "img_df[\"full_path\"] = img_df.apply(lambda row: os.path.join(row[\"root\"], row[\"file\"]), axis=1)\n", "img_df[\"new_root\"] = img_df[\"root\"].apply(\n", " lambda x: x.replace(\"data/raw/images/\", \"data/processed/OM_images_white/\")\n", ")\n", "img_df[\"new_full_path\"] = img_df.apply(lambda row: os.path.join(row[\"new_root\"], row[\"file\"]), axis=1)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
filerootobj_numfull_pathnew_root
01985.15.68.jpgdata/raw/images/fulling_mill/1985durma.1985.15.68data/raw/images/fulling_mill/1985/1985.15.68.jpgdata/processed/OM_images_white/fulling_mill/1985
11985.52.37.ff2.jpgdata/raw/images/fulling_mill/1985durma.1985.52.37data/raw/images/fulling_mill/1985/1985.52.37.f...data/processed/OM_images_white/fulling_mill/1985
21985.81.4496 d2.jpgdata/raw/images/fulling_mill/1985durma.1985.81.4496data/raw/images/fulling_mill/1985/1985.81.4496...data/processed/OM_images_white/fulling_mill/1985
31985.9.1.1-d4.jpgdata/raw/images/fulling_mill/1985durma.1985.9.1data/raw/images/fulling_mill/1985/1985.9.1.1-d...data/processed/OM_images_white/fulling_mill/1985
41985.52.37.sf2.jpgdata/raw/images/fulling_mill/1985durma.1985.52.37data/raw/images/fulling_mill/1985/1985.52.37.s...data/processed/OM_images_white/fulling_mill/1985
..................
392392014.1.2 bb.jpgdata/raw/images/egyptian/2014durom.2014.1.2data/raw/images/egyptian/2014/2014.1.2 bb.jpgdata/processed/OM_images_white/egyptian/2014
392402014.1.71 ll.jpgdata/raw/images/egyptian/2014durom.2014.1.71data/raw/images/egyptian/2014/2014.1.71 ll.jpgdata/processed/OM_images_white/egyptian/2014
392412014.1.2 rr.jpgdata/raw/images/egyptian/2014durom.2014.1.2data/raw/images/egyptian/2014/2014.1.2 rr.jpgdata/processed/OM_images_white/egyptian/2014
392421963.4.jpgdata/raw/images/egyptian/1963durom.1963.4data/raw/images/egyptian/1963/1963.4.jpgdata/processed/OM_images_white/egyptian/1963
392431963.4.2.jpgdata/raw/images/egyptian/1963durom.1963.4data/raw/images/egyptian/1963/1963.4.2.jpgdata/processed/OM_images_white/egyptian/1963
\n", "

39244 rows × 5 columns

\n", "
" ], "text/plain": [ " file root \\\n", "0 1985.15.68.jpg data/raw/images/fulling_mill/1985 \n", "1 1985.52.37.ff2.jpg data/raw/images/fulling_mill/1985 \n", "2 1985.81.4496 d2.jpg data/raw/images/fulling_mill/1985 \n", "3 1985.9.1.1-d4.jpg data/raw/images/fulling_mill/1985 \n", "4 1985.52.37.sf2.jpg data/raw/images/fulling_mill/1985 \n", "... ... ... \n", "39239 2014.1.2 bb.jpg data/raw/images/egyptian/2014 \n", "39240 2014.1.71 ll.jpg data/raw/images/egyptian/2014 \n", "39241 2014.1.2 rr.jpg data/raw/images/egyptian/2014 \n", "39242 1963.4.jpg data/raw/images/egyptian/1963 \n", "39243 1963.4.2.jpg data/raw/images/egyptian/1963 \n", "\n", " obj_num full_path \\\n", "0 durma.1985.15.68 data/raw/images/fulling_mill/1985/1985.15.68.jpg \n", "1 durma.1985.52.37 data/raw/images/fulling_mill/1985/1985.52.37.f... \n", "2 durma.1985.81.4496 data/raw/images/fulling_mill/1985/1985.81.4496... \n", "3 durma.1985.9.1 data/raw/images/fulling_mill/1985/1985.9.1.1-d... \n", "4 durma.1985.52.37 data/raw/images/fulling_mill/1985/1985.52.37.s... \n", "... ... ... \n", "39239 durom.2014.1.2 data/raw/images/egyptian/2014/2014.1.2 bb.jpg \n", "39240 durom.2014.1.71 data/raw/images/egyptian/2014/2014.1.71 ll.jpg \n", "39241 durom.2014.1.2 data/raw/images/egyptian/2014/2014.1.2 rr.jpg \n", "39242 durom.1963.4 data/raw/images/egyptian/1963/1963.4.jpg \n", "39243 durom.1963.4 data/raw/images/egyptian/1963/1963.4.2.jpg \n", "\n", " new_root \n", "0 data/processed/OM_images_white/fulling_mill/1985 \n", "1 data/processed/OM_images_white/fulling_mill/1985 \n", "2 data/processed/OM_images_white/fulling_mill/1985 \n", "3 data/processed/OM_images_white/fulling_mill/1985 \n", "4 data/processed/OM_images_white/fulling_mill/1985 \n", "... ... \n", "39239 data/processed/OM_images_white/egyptian/2014 \n", "39240 data/processed/OM_images_white/egyptian/2014 \n", "39241 data/processed/OM_images_white/egyptian/2014 \n", "39242 data/processed/OM_images_white/egyptian/1963 \n", "39243 data/processed/OM_images_white/egyptian/1963 \n", "\n", "[39244 rows x 5 columns]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img_df" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 0%| | 84/39244 [00:06<52:59, 12.32it/s] \n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[26], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m index, row \u001b[38;5;129;01min\u001b[39;00m tqdm(img_df\u001b[38;5;241m.\u001b[39miterrows(), total\u001b[38;5;241m=\u001b[39mimg_df\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m0\u001b[39m]):\n\u001b[1;32m 2\u001b[0m img \u001b[38;5;241m=\u001b[39m Image\u001b[38;5;241m.\u001b[39mopen(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m../\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m+\u001b[39mrow[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfull_path\u001b[39m\u001b[38;5;124m'\u001b[39m])\u001b[38;5;241m.\u001b[39mconvert(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mRGB\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;66;03m# read image\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mremover\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprocess\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mtype\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mwhite\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# change backround with white color\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# make sure the directory exists\u001b[39;00m\n\u001b[1;32m 5\u001b[0m os\u001b[38;5;241m.\u001b[39mmakedirs(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m../\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m+\u001b[39mrow[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnew_root\u001b[39m\u001b[38;5;124m'\u001b[39m], exist_ok\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", "File \u001b[0;32m~/.virtualenvs/ArtifactClassification/lib/python3.10/site-packages/transparent_background/Remover.py:154\u001b[0m, in \u001b[0;36mRemover.process\u001b[0;34m(self, img, type, threshold)\u001b[0m\n\u001b[1;32m 137\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n\u001b[1;32m 139\u001b[0m \u001b[38;5;124;03m img (PIL.Image): input image as PIL.Image type\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 151\u001b[0m \n\u001b[1;32m 152\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 153\u001b[0m shape \u001b[38;5;241m=\u001b[39m img\u001b[38;5;241m.\u001b[39msize[::\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m--> 154\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtransform\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 155\u001b[0m x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39munsqueeze(\u001b[38;5;241m0\u001b[39m)\n\u001b[1;32m 156\u001b[0m x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39mto(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdevice)\n", "File \u001b[0;32m~/.virtualenvs/ArtifactClassification/lib/python3.10/site-packages/torchvision/transforms/transforms.py:95\u001b[0m, in \u001b[0;36mCompose.__call__\u001b[0;34m(self, img)\u001b[0m\n\u001b[1;32m 93\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, img):\n\u001b[1;32m 94\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m t \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtransforms:\n\u001b[0;32m---> 95\u001b[0m img \u001b[38;5;241m=\u001b[39m \u001b[43mt\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 96\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m img\n", "File \u001b[0;32m~/.virtualenvs/ArtifactClassification/lib/python3.10/site-packages/transparent_background/utils.py:105\u001b[0m, in \u001b[0;36mnormalize.__call__\u001b[0;34m(self, img)\u001b[0m\n\u001b[1;32m 103\u001b[0m img \u001b[38;5;241m/\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdiv\n\u001b[1;32m 104\u001b[0m img \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmean\n\u001b[0;32m--> 105\u001b[0m img \u001b[38;5;241m/\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstd\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m img\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "for index, row in tqdm(img_df.iterrows(), total=img_df.shape[0]):\n", " img = Image.open('../' + row['full_path']).convert('RGB') # read image\n", " out = remover.process(img, type='white') # change backround with white color\n", " # make sure the directory exists\n", " os.makedirs('../' + row['new_root'], exist_ok=True)\n", " out.save('../' + row['new_full_path']) # save result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "ArtifactClassification", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }