{ "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", " | file | \n", "root | \n", "obj_num | \n", "full_path | \n", "new_root | \n", "
---|---|---|---|---|---|
0 | \n", "1985.15.68.jpg | \n", "data/raw/images/fulling_mill/1985 | \n", "durma.1985.15.68 | \n", "data/raw/images/fulling_mill/1985/1985.15.68.jpg | \n", "data/processed/OM_images_white/fulling_mill/1985 | \n", "
1 | \n", "1985.52.37.ff2.jpg | \n", "data/raw/images/fulling_mill/1985 | \n", "durma.1985.52.37 | \n", "data/raw/images/fulling_mill/1985/1985.52.37.f... | \n", "data/processed/OM_images_white/fulling_mill/1985 | \n", "
2 | \n", "1985.81.4496 d2.jpg | \n", "data/raw/images/fulling_mill/1985 | \n", "durma.1985.81.4496 | \n", "data/raw/images/fulling_mill/1985/1985.81.4496... | \n", "data/processed/OM_images_white/fulling_mill/1985 | \n", "
3 | \n", "1985.9.1.1-d4.jpg | \n", "data/raw/images/fulling_mill/1985 | \n", "durma.1985.9.1 | \n", "data/raw/images/fulling_mill/1985/1985.9.1.1-d... | \n", "data/processed/OM_images_white/fulling_mill/1985 | \n", "
4 | \n", "1985.52.37.sf2.jpg | \n", "data/raw/images/fulling_mill/1985 | \n", "durma.1985.52.37 | \n", "data/raw/images/fulling_mill/1985/1985.52.37.s... | \n", "data/processed/OM_images_white/fulling_mill/1985 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
39239 | \n", "2014.1.2 bb.jpg | \n", "data/raw/images/egyptian/2014 | \n", "durom.2014.1.2 | \n", "data/raw/images/egyptian/2014/2014.1.2 bb.jpg | \n", "data/processed/OM_images_white/egyptian/2014 | \n", "
39240 | \n", "2014.1.71 ll.jpg | \n", "data/raw/images/egyptian/2014 | \n", "durom.2014.1.71 | \n", "data/raw/images/egyptian/2014/2014.1.71 ll.jpg | \n", "data/processed/OM_images_white/egyptian/2014 | \n", "
39241 | \n", "2014.1.2 rr.jpg | \n", "data/raw/images/egyptian/2014 | \n", "durom.2014.1.2 | \n", "data/raw/images/egyptian/2014/2014.1.2 rr.jpg | \n", "data/processed/OM_images_white/egyptian/2014 | \n", "
39242 | \n", "1963.4.jpg | \n", "data/raw/images/egyptian/1963 | \n", "durom.1963.4 | \n", "data/raw/images/egyptian/1963/1963.4.jpg | \n", "data/processed/OM_images_white/egyptian/1963 | \n", "
39243 | \n", "1963.4.2.jpg | \n", "data/raw/images/egyptian/1963 | \n", "durom.1963.4 | \n", "data/raw/images/egyptian/1963/1963.4.2.jpg | \n", "data/processed/OM_images_white/egyptian/1963 | \n", "
39244 rows × 5 columns
\n", "