Spaces:
Build error
Build error
change thread logic
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
import shutil
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
from rembg import remove
|
| 7 |
import gradio as gr
|
| 8 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
def resize_and_crop_image(image_path, target_size=(1080, 1080), crop_mode='center'):
|
| 12 |
-
print(f"Resizing and cropping image: {image_path}")
|
| 13 |
with Image.open(image_path) as img:
|
| 14 |
width, height = img.size
|
| 15 |
|
|
@@ -45,7 +45,6 @@ def resize_and_crop_image(image_path, target_size=(1080, 1080), crop_mode='cente
|
|
| 45 |
return cropped_img
|
| 46 |
|
| 47 |
def remove_background_rembg(input_path):
|
| 48 |
-
print(f"Removing background using rembg: {input_path}")
|
| 49 |
with open(input_path, 'rb') as i:
|
| 50 |
input_image = i.read()
|
| 51 |
output_image = remove(input_image)
|
|
@@ -53,7 +52,6 @@ def remove_background_rembg(input_path):
|
|
| 53 |
return img
|
| 54 |
|
| 55 |
def remove_background_bria(input_path):
|
| 56 |
-
print(f"Removing background using bria: {input_path}")
|
| 57 |
pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
|
| 58 |
pillow_image = pipe(input_path) # applies mask on input and returns a pillow image
|
| 59 |
return pillow_image
|
|
@@ -61,7 +59,6 @@ def remove_background_bria(input_path):
|
|
| 61 |
def process_single_image(image_path, output_folder, crop_mode, remove_bg, bg_method, output_format, bg_choice, watermark_path=None):
|
| 62 |
filename = os.path.basename(image_path)
|
| 63 |
try:
|
| 64 |
-
print(f"Processing image: {filename}")
|
| 65 |
if remove_bg == 'yes':
|
| 66 |
if bg_method == 'rembg':
|
| 67 |
# Remove background using rembg
|
|
@@ -72,13 +69,11 @@ def process_single_image(image_path, output_folder, crop_mode, remove_bg, bg_met
|
|
| 72 |
|
| 73 |
temp_image_path = os.path.join(output_folder, f"temp_{filename}")
|
| 74 |
image_with_no_bg.save(temp_image_path, format='PNG')
|
| 75 |
-
print(f"Background removed and saved temporary image: {temp_image_path}")
|
| 76 |
else:
|
| 77 |
temp_image_path = image_path
|
| 78 |
|
| 79 |
# Resize and crop the image with or without background removal
|
| 80 |
new_image = resize_and_crop_image(temp_image_path, crop_mode=crop_mode)
|
| 81 |
-
print(f"Resized and cropped image: {temp_image_path}")
|
| 82 |
|
| 83 |
# Save both versions of the image (with and without watermark)
|
| 84 |
images_paths = []
|
|
@@ -91,7 +86,6 @@ def process_single_image(image_path, output_folder, crop_mode, remove_bg, bg_met
|
|
| 91 |
else:
|
| 92 |
new_image.save(output_path_without_watermark, format='PNG')
|
| 93 |
images_paths.append(output_path_without_watermark)
|
| 94 |
-
print(f"Saved final image without watermark: {output_path_without_watermark}")
|
| 95 |
|
| 96 |
# Apply watermark if provided and save the version with watermark
|
| 97 |
if watermark_path:
|
|
@@ -104,12 +98,10 @@ def process_single_image(image_path, output_folder, crop_mode, remove_bg, bg_met
|
|
| 104 |
else:
|
| 105 |
new_image_with_watermark.save(output_path_with_watermark, format='PNG')
|
| 106 |
images_paths.append(output_path_with_watermark)
|
| 107 |
-
print(f"Saved final image with watermark: {output_path_with_watermark}")
|
| 108 |
|
| 109 |
if remove_bg == 'yes':
|
| 110 |
# Remove the temporary file
|
| 111 |
os.remove(temp_image_path)
|
| 112 |
-
print(f"Removed temporary file: {temp_image_path}")
|
| 113 |
|
| 114 |
return images_paths
|
| 115 |
|
|
@@ -117,8 +109,9 @@ def process_single_image(image_path, output_folder, crop_mode, remove_bg, bg_met
|
|
| 117 |
print(f"Error processing {filename}: {e}")
|
| 118 |
return None
|
| 119 |
|
| 120 |
-
def process_images(zip_file, crop_mode='center', remove_bg='yes', bg_method='rembg', watermark_path=None, output_format='PNG', bg_choice='transparent', progress=gr.Progress()):
|
| 121 |
-
|
|
|
|
| 122 |
# Create a temporary directory
|
| 123 |
input_folder = "temp_input"
|
| 124 |
output_folder = "temp_output"
|
|
@@ -130,25 +123,23 @@ def process_images(zip_file, crop_mode='center', remove_bg='yes', bg_method='rem
|
|
| 130 |
os.makedirs(output_folder)
|
| 131 |
|
| 132 |
# Extract the zip file
|
| 133 |
-
print(f"Extracting zip file: {zip_file}")
|
| 134 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
| 135 |
zip_ref.extractall(input_folder)
|
| 136 |
|
| 137 |
processed_images = []
|
| 138 |
image_files = [os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
|
| 139 |
total_images = len(image_files)
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
print(f"Processed {idx + 1}/{total_images} images")
|
| 152 |
|
| 153 |
# Create a zip file of the processed images
|
| 154 |
output_zip_path = "processed_images.zip"
|
|
@@ -159,15 +150,16 @@ def process_images(zip_file, crop_mode='center', remove_bg='yes', bg_method='rem
|
|
| 159 |
else:
|
| 160 |
zipf.write(file, os.path.join("without_watermark", os.path.basename(file)))
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
def gradio_interface(zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice):
|
| 167 |
-
print("Initializing Gradio interface processing...")
|
| 168 |
progress = gr.Progress() # Initialize progress
|
| 169 |
watermark_path = watermark.name if watermark else None
|
| 170 |
-
return process_images(zip_file.name, crop_mode, remove_bg, bg_method, watermark_path, output_format, bg_choice, progress)
|
| 171 |
|
| 172 |
def show_bg_choice(remove_bg, output_format):
|
| 173 |
if remove_bg == 'yes' and output_format == 'PNG':
|
|
@@ -192,7 +184,8 @@ with gr.Blocks() as iface:
|
|
| 192 |
crop_mode = gr.Radio(choices=["center", "top", "bottom", "left", "right"], label="Crop Mode", value="center")
|
| 193 |
remove_bg = gr.Radio(choices=["yes", "no"], label="Remove Background", value="yes")
|
| 194 |
|
| 195 |
-
output_format = gr.Radio(choices=["PNG", "JPG"], label="Output Format", value="PNG")
|
|
|
|
| 196 |
|
| 197 |
with gr.Row():
|
| 198 |
bg_method = gr.Radio(choices=["bria", "rembg"], label="Background Removal Method", value="bria", visible=True)
|
|
@@ -204,13 +197,14 @@ with gr.Blocks() as iface:
|
|
| 204 |
|
| 205 |
gallery = gr.Gallery(label="Processed Images")
|
| 206 |
output_zip = gr.File(label="Download Processed Images as ZIP")
|
|
|
|
| 207 |
|
| 208 |
-
def process(zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice):
|
| 209 |
-
processed_images, zip_path = gradio_interface(zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice)
|
| 210 |
-
return processed_images, zip_path
|
| 211 |
|
| 212 |
process_button = gr.Button("Process Images")
|
| 213 |
-
process_button.click(process, inputs=[zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice], outputs=[gallery, output_zip])
|
| 214 |
|
| 215 |
# Launch the interface
|
| 216 |
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
import shutil
|
| 4 |
+
import time
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
from rembg import remove
|
| 8 |
import gradio as gr
|
| 9 |
+
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
|
| 10 |
from transformers import pipeline
|
| 11 |
|
| 12 |
def resize_and_crop_image(image_path, target_size=(1080, 1080), crop_mode='center'):
|
|
|
|
| 13 |
with Image.open(image_path) as img:
|
| 14 |
width, height = img.size
|
| 15 |
|
|
|
|
| 45 |
return cropped_img
|
| 46 |
|
| 47 |
def remove_background_rembg(input_path):
|
|
|
|
| 48 |
with open(input_path, 'rb') as i:
|
| 49 |
input_image = i.read()
|
| 50 |
output_image = remove(input_image)
|
|
|
|
| 52 |
return img
|
| 53 |
|
| 54 |
def remove_background_bria(input_path):
|
|
|
|
| 55 |
pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
|
| 56 |
pillow_image = pipe(input_path) # applies mask on input and returns a pillow image
|
| 57 |
return pillow_image
|
|
|
|
| 59 |
def process_single_image(image_path, output_folder, crop_mode, remove_bg, bg_method, output_format, bg_choice, watermark_path=None):
|
| 60 |
filename = os.path.basename(image_path)
|
| 61 |
try:
|
|
|
|
| 62 |
if remove_bg == 'yes':
|
| 63 |
if bg_method == 'rembg':
|
| 64 |
# Remove background using rembg
|
|
|
|
| 69 |
|
| 70 |
temp_image_path = os.path.join(output_folder, f"temp_{filename}")
|
| 71 |
image_with_no_bg.save(temp_image_path, format='PNG')
|
|
|
|
| 72 |
else:
|
| 73 |
temp_image_path = image_path
|
| 74 |
|
| 75 |
# Resize and crop the image with or without background removal
|
| 76 |
new_image = resize_and_crop_image(temp_image_path, crop_mode=crop_mode)
|
|
|
|
| 77 |
|
| 78 |
# Save both versions of the image (with and without watermark)
|
| 79 |
images_paths = []
|
|
|
|
| 86 |
else:
|
| 87 |
new_image.save(output_path_without_watermark, format='PNG')
|
| 88 |
images_paths.append(output_path_without_watermark)
|
|
|
|
| 89 |
|
| 90 |
# Apply watermark if provided and save the version with watermark
|
| 91 |
if watermark_path:
|
|
|
|
| 98 |
else:
|
| 99 |
new_image_with_watermark.save(output_path_with_watermark, format='PNG')
|
| 100 |
images_paths.append(output_path_with_watermark)
|
|
|
|
| 101 |
|
| 102 |
if remove_bg == 'yes':
|
| 103 |
# Remove the temporary file
|
| 104 |
os.remove(temp_image_path)
|
|
|
|
| 105 |
|
| 106 |
return images_paths
|
| 107 |
|
|
|
|
| 109 |
print(f"Error processing {filename}: {e}")
|
| 110 |
return None
|
| 111 |
|
| 112 |
+
def process_images(zip_file, crop_mode='center', remove_bg='yes', bg_method='rembg', watermark_path=None, output_format='PNG', bg_choice='transparent', num_workers=4, progress=gr.Progress()):
|
| 113 |
+
start_time = time.time()
|
| 114 |
+
|
| 115 |
# Create a temporary directory
|
| 116 |
input_folder = "temp_input"
|
| 117 |
output_folder = "temp_output"
|
|
|
|
| 123 |
os.makedirs(output_folder)
|
| 124 |
|
| 125 |
# Extract the zip file
|
|
|
|
| 126 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
| 127 |
zip_ref.extractall(input_folder)
|
| 128 |
|
| 129 |
processed_images = []
|
| 130 |
image_files = [os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
|
| 131 |
total_images = len(image_files)
|
| 132 |
+
|
| 133 |
+
# Process images using ThreadPoolExecutor for I/O-bound tasks and ProcessPoolExecutor for CPU-bound tasks
|
| 134 |
+
with ThreadPoolExecutor(max_workers=num_workers) as thread_executor:
|
| 135 |
+
with ProcessPoolExecutor(max_workers=num_workers) as process_executor:
|
| 136 |
+
future_to_image = {thread_executor.submit(process_single_image, image_path, output_folder, crop_mode, remove_bg, bg_method, output_format, bg_choice, watermark_path): image_path for image_path in image_files}
|
| 137 |
+
for idx, future in enumerate(future_to_image):
|
| 138 |
+
result = future.result()
|
| 139 |
+
if result:
|
| 140 |
+
processed_images.extend(result)
|
| 141 |
+
# Update progress
|
| 142 |
+
progress((idx + 1) / total_images, f"{idx + 1}/{total_images} images processed")
|
|
|
|
| 143 |
|
| 144 |
# Create a zip file of the processed images
|
| 145 |
output_zip_path = "processed_images.zip"
|
|
|
|
| 150 |
else:
|
| 151 |
zipf.write(file, os.path.join("without_watermark", os.path.basename(file)))
|
| 152 |
|
| 153 |
+
end_time = time.time()
|
| 154 |
+
processing_time = end_time - start_time
|
| 155 |
+
|
| 156 |
+
# Return the images, the zip file path, and processing time
|
| 157 |
+
return processed_images, output_zip_path, processing_time
|
| 158 |
|
| 159 |
+
def gradio_interface(zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice, num_workers):
|
|
|
|
| 160 |
progress = gr.Progress() # Initialize progress
|
| 161 |
watermark_path = watermark.name if watermark else None
|
| 162 |
+
return process_images(zip_file.name, crop_mode, remove_bg, bg_method, watermark_path, output_format, bg_choice, num_workers, progress)
|
| 163 |
|
| 164 |
def show_bg_choice(remove_bg, output_format):
|
| 165 |
if remove_bg == 'yes' and output_format == 'PNG':
|
|
|
|
| 184 |
crop_mode = gr.Radio(choices=["center", "top", "bottom", "left", "right"], label="Crop Mode", value="center")
|
| 185 |
remove_bg = gr.Radio(choices=["yes", "no"], label="Remove Background", value="yes")
|
| 186 |
|
| 187 |
+
output_format = gr.Radio(choices=["PNG", "JPG"], label="Output Format", value="PNG")
|
| 188 |
+
num_workers = gr.Slider(minimum=1, maximum=16, step=1, label="Number of Workers", value=4)
|
| 189 |
|
| 190 |
with gr.Row():
|
| 191 |
bg_method = gr.Radio(choices=["bria", "rembg"], label="Background Removal Method", value="bria", visible=True)
|
|
|
|
| 197 |
|
| 198 |
gallery = gr.Gallery(label="Processed Images")
|
| 199 |
output_zip = gr.File(label="Download Processed Images as ZIP")
|
| 200 |
+
processing_time = gr.Textbox(label="Processing Time (seconds)")
|
| 201 |
|
| 202 |
+
def process(zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice, num_workers):
|
| 203 |
+
processed_images, zip_path, time_taken = gradio_interface(zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice, num_workers)
|
| 204 |
+
return processed_images, zip_path, f"{time_taken:.2f} seconds"
|
| 205 |
|
| 206 |
process_button = gr.Button("Process Images")
|
| 207 |
+
process_button.click(process, inputs=[zip_file, crop_mode, remove_bg, bg_method, watermark, output_format, bg_choice, num_workers], outputs=[gallery, output_zip, processing_time])
|
| 208 |
|
| 209 |
# Launch the interface
|
| 210 |
iface.launch()
|