reworked to work with processed_players

This commit is contained in:
Karl Hudgell 2024-12-16 15:56:39 +00:00
parent 907a30f72f
commit 81d05515b3

View File

@ -3,22 +3,25 @@ from PIL import Image
from tqdm import tqdm from tqdm import tqdm
import os import os
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import onnxruntime as ort import onnxruntime as ort
def process_images_in_batch(batch, use_gpu): def process_images_in_batch(batch, directory, use_gpu):
""" """
Process a batch of images: remove their backgrounds and save the results. Process a batch of images: remove their backgrounds and save the results.
Args: Args:
batch (list): List of tuples (input_path, output_path). batch (list): List of filenames to process (without path or extension).
directory (str): Base directory to locate input files.
use_gpu (bool): Whether to enable GPU support. use_gpu (bool): Whether to enable GPU support.
Returns: Returns:
int: Number of images successfully processed in this batch. int: Number of images successfully processed in this batch.
""" """
success_count = 0 success_count = 0
for input_path, output_path in batch: for filename in batch:
input_path = os.path.join(directory, f"{filename}.png")
output_path = os.path.join(directory, f"{filename}.png")
try: try:
with Image.open(input_path) as img: with Image.open(input_path) as img:
# Initialize ONNX session options with GPU support if required # Initialize ONNX session options with GPU support if required
@ -29,17 +32,20 @@ def process_images_in_batch(batch, use_gpu):
# Initialize the rembg remove function with appropriate providers # Initialize the rembg remove function with appropriate providers
output = remove(img, session_options=session_options, providers=providers) output = remove(img, session_options=session_options, providers=providers)
output.save(output_path) output.save(output_path)
success_count += 1 success_count += 1
except Exception as e: except Exception as e:
print(f"Error processing {input_path}: {str(e)}") print(f"Error processing {input_path}: {str(e)}")
return success_count return success_count
def remove_bg_from_files_in_dir(directory, max_workers=2, batch_size=3, use_gpu=False): def remove_bg_from_file_list(directory, filenames, max_workers=2, batch_size=2, use_gpu=False):
""" """
Process all JPG, JPEG, and PNG images in the given directory and its subfolders using parallel processing and GPU. Process a list of specified filenames: remove their backgrounds and save the results.
Args: Args:
directory (str): Path to the directory containing images. directory (str): Path to the directory containing images.
filenames (list): List of filenames (without path or extension) to process.
max_workers (int): Maximum number of threads to use for parallel processing. max_workers (int): Maximum number of threads to use for parallel processing.
batch_size (int): Number of images to process per batch. batch_size (int): Number of images to process per batch.
use_gpu (bool): Whether to enable GPU support. use_gpu (bool): Whether to enable GPU support.
@ -47,26 +53,15 @@ def remove_bg_from_files_in_dir(directory, max_workers=2, batch_size=3, use_gpu=
Returns: Returns:
int: The number of images successfully processed. int: The number of images successfully processed.
""" """
files_to_process = []
# Gather all the image files to process
for subdir, dirs, files in os.walk(directory):
for file in files:
if file.lower().endswith(('.jpg', '.jpeg', '.png')):
input_path = os.path.join(subdir, file)
output_filename = os.path.splitext(file)[0] + '.png'
output_path = os.path.join(subdir, output_filename)
files_to_process.append((input_path, output_path))
processed_count = 0 processed_count = 0
# Divide files into batches # Divide filenames into batches
batches = [files_to_process[i:i + batch_size] for i in range(0, len(files_to_process), batch_size)] batches = [filenames[i:i + batch_size] for i in range(0, len(filenames), batch_size)]
with ThreadPoolExecutor(max_workers=max_workers) as executor: with ThreadPoolExecutor(max_workers=max_workers) as executor:
with tqdm(total=len(files_to_process), desc="Removing Backgrounds", unit="image") as pbar: with tqdm(total=len(filenames), desc="Removing Backgrounds", unit="image") as pbar:
futures = { futures = {
executor.submit(process_images_in_batch, batch, use_gpu): batch executor.submit(process_images_in_batch, batch, directory, use_gpu): batch
for batch in batches for batch in batches
} }