File size: 16,944 Bytes
3752cdf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
{
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>file</th>\n",
" <th>root</th>\n",
" <th>obj_num</th>\n",
" <th>full_path</th>\n",
" <th>new_root</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1985.15.68.jpg</td>\n",
" <td>data/raw/images/fulling_mill/1985</td>\n",
" <td>durma.1985.15.68</td>\n",
" <td>data/raw/images/fulling_mill/1985/1985.15.68.jpg</td>\n",
" <td>data/processed/OM_images_white/fulling_mill/1985</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1985.52.37.ff2.jpg</td>\n",
" <td>data/raw/images/fulling_mill/1985</td>\n",
" <td>durma.1985.52.37</td>\n",
" <td>data/raw/images/fulling_mill/1985/1985.52.37.f...</td>\n",
" <td>data/processed/OM_images_white/fulling_mill/1985</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1985.81.4496 d2.jpg</td>\n",
" <td>data/raw/images/fulling_mill/1985</td>\n",
" <td>durma.1985.81.4496</td>\n",
" <td>data/raw/images/fulling_mill/1985/1985.81.4496...</td>\n",
" <td>data/processed/OM_images_white/fulling_mill/1985</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1985.9.1.1-d4.jpg</td>\n",
" <td>data/raw/images/fulling_mill/1985</td>\n",
" <td>durma.1985.9.1</td>\n",
" <td>data/raw/images/fulling_mill/1985/1985.9.1.1-d...</td>\n",
" <td>data/processed/OM_images_white/fulling_mill/1985</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1985.52.37.sf2.jpg</td>\n",
" <td>data/raw/images/fulling_mill/1985</td>\n",
" <td>durma.1985.52.37</td>\n",
" <td>data/raw/images/fulling_mill/1985/1985.52.37.s...</td>\n",
" <td>data/processed/OM_images_white/fulling_mill/1985</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39239</th>\n",
" <td>2014.1.2 bb.jpg</td>\n",
" <td>data/raw/images/egyptian/2014</td>\n",
" <td>durom.2014.1.2</td>\n",
" <td>data/raw/images/egyptian/2014/2014.1.2 bb.jpg</td>\n",
" <td>data/processed/OM_images_white/egyptian/2014</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39240</th>\n",
" <td>2014.1.71 ll.jpg</td>\n",
" <td>data/raw/images/egyptian/2014</td>\n",
" <td>durom.2014.1.71</td>\n",
" <td>data/raw/images/egyptian/2014/2014.1.71 ll.jpg</td>\n",
" <td>data/processed/OM_images_white/egyptian/2014</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39241</th>\n",
" <td>2014.1.2 rr.jpg</td>\n",
" <td>data/raw/images/egyptian/2014</td>\n",
" <td>durom.2014.1.2</td>\n",
" <td>data/raw/images/egyptian/2014/2014.1.2 rr.jpg</td>\n",
" <td>data/processed/OM_images_white/egyptian/2014</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39242</th>\n",
" <td>1963.4.jpg</td>\n",
" <td>data/raw/images/egyptian/1963</td>\n",
" <td>durom.1963.4</td>\n",
" <td>data/raw/images/egyptian/1963/1963.4.jpg</td>\n",
" <td>data/processed/OM_images_white/egyptian/1963</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39243</th>\n",
" <td>1963.4.2.jpg</td>\n",
" <td>data/raw/images/egyptian/1963</td>\n",
" <td>durom.1963.4</td>\n",
" <td>data/raw/images/egyptian/1963/1963.4.2.jpg</td>\n",
" <td>data/processed/OM_images_white/egyptian/1963</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>39244 rows × 5 columns</p>\n",
"</div>"
],
"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
}
|