Replies: 2 comments
-
Then I got AI to add batch processing. import os --- SETUP ---output folderOUTPUT_DIR = r"E:\backgroundRemover\Outputs" pick GPU if availabledevice = "cuda" if torch.cuda.is_available() else "cpu" initialize CarveKitprint("Initializing CarveKit interface...") --- HELPER FUNCTIONS ---def clear_image(): def pick_folder(): def clear_folder(): def batch_process(img: np.ndarray, folder_path: str):
--- GRADIO UI ---CSS: left-align rows, auto-size buttons, hide flag iconcustom_css = """ with gr.Blocks(css=custom_css, title="AI Background Remover") as demo:
if name == "main": |
Beta Was this translation helpful? Give feedback.
-
0.6 seconds per image on a 4090. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
import os
import time
import gradio as gr
import torch
from carvekit.api.high import HiInterface
from PIL import Image
import numpy as np
where to save outputs
OUTPUT_DIR = r"E:\backgroundRemover\Outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)
pick GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
initialize CarveKit
interface = HiInterface(
object_type="hairs-like",
batch_size_seg=1,
batch_size_matting=1,
device=device,
seg_mask_size=320,
matting_mask_size=2048,
trimap_prob_threshold=231,
trimap_dilation=30,
trimap_erosion_iters=5,
fp16=False
)
def remove_background(img: np.ndarray) -> Image.Image:
pil_img = Image.fromarray(img)
result = interface([pil_img])[0]
timestamp = int(time.time())
out_path = os.path.join(OUTPUT_DIR, f"transparent_{timestamp}.png")
result.save(out_path)
return result
demo = gr.Interface(
fn=remove_background,
inputs=gr.Image(type="numpy"),
outputs=gr.Image(type="pil"),
title="AI Background Remover",
description="Upload a photo and download a PNG with a transparent background"
)
if name == "main":
demo.launch(server_name="127.0.0.1", server_port=7860)
obviously there is a hard coded path in there "E:\backgroundRemover\Outputs" but if someone wanted I'm sure they could change that.
Beta Was this translation helpful? Give feedback.
All reactions