Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -7,9 +7,13 @@ Original file is located at
|
|
| 7 |
https://colab.research.google.com/drive/1GoN403JoCKCLavITeCYoMzFXKctQ83en
|
| 8 |
"""
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
from PIL import Image
|
| 12 |
import numpy as np
|
|
|
|
| 13 |
|
| 14 |
def sanitize_hex_color(color):
|
| 15 |
"""
|
|
@@ -35,7 +39,8 @@ def sanitize_hex_color(color):
|
|
| 35 |
|
| 36 |
def remove_background_color(image, hex_color, tolerance):
|
| 37 |
"""
|
| 38 |
-
Remove pixels of a specified hex color from an image
|
|
|
|
| 39 |
"""
|
| 40 |
if isinstance(image, np.ndarray):
|
| 41 |
image = Image.fromarray(image)
|
|
@@ -47,25 +52,20 @@ def remove_background_color(image, hex_color, tolerance):
|
|
| 47 |
raise ValueError(str(e))
|
| 48 |
data = np.array(image)
|
| 49 |
mask = np.all(np.abs(data[:,:,:3] - target_color) <= tolerance, axis=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
alpha_channel = np.where(mask, 0, 255)
|
| 51 |
data[:, :, 3] = alpha_channel
|
| 52 |
return data
|
| 53 |
|
| 54 |
-
|
| 55 |
-
def color_remover_interface(image, hex_color, tolerance, _=None):
|
| 56 |
-
"""
|
| 57 |
-
Gradio interface function for color removal.
|
| 58 |
-
"""
|
| 59 |
-
try:
|
| 60 |
-
if image is None:
|
| 61 |
-
raise ValueError("Please upload an image")
|
| 62 |
-
hex_color = sanitize_hex_color(hex_color)
|
| 63 |
-
processed_image = remove_background_color(image, hex_color, tolerance)
|
| 64 |
-
return processed_image # Return only the processed image
|
| 65 |
-
except Exception as e:
|
| 66 |
-
gr.Warning(str(e))
|
| 67 |
-
return image # Return original image in case of error
|
| 68 |
-
|
| 69 |
# Deploy using Gradio Client
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=color_remover_interface,
|
|
|
|
| 7 |
https://colab.research.google.com/drive/1GoN403JoCKCLavITeCYoMzFXKctQ83en
|
| 8 |
"""
|
| 9 |
|
| 10 |
+
from IPython import get_ipython
|
| 11 |
+
from IPython.display import display
|
| 12 |
+
# %%
|
| 13 |
import gradio as gr
|
| 14 |
from PIL import Image
|
| 15 |
import numpy as np
|
| 16 |
+
import cv2 # Import OpenCV
|
| 17 |
|
| 18 |
def sanitize_hex_color(color):
|
| 19 |
"""
|
|
|
|
| 39 |
|
| 40 |
def remove_background_color(image, hex_color, tolerance):
|
| 41 |
"""
|
| 42 |
+
Remove pixels of a specified hex color from an image,
|
| 43 |
+
with edge detection for refinement.
|
| 44 |
"""
|
| 45 |
if isinstance(image, np.ndarray):
|
| 46 |
image = Image.fromarray(image)
|
|
|
|
| 52 |
raise ValueError(str(e))
|
| 53 |
data = np.array(image)
|
| 54 |
mask = np.all(np.abs(data[:,:,:3] - target_color) <= tolerance, axis=2)
|
| 55 |
+
|
| 56 |
+
# Edge detection using Canny
|
| 57 |
+
edges = cv2.Canny(cv2.cvtColor(data, cv2.COLOR_RGBA2GRAY), 100, 200)
|
| 58 |
+
# Adjust thresholds (100, 200) as needed
|
| 59 |
+
|
| 60 |
+
# Refine mask based on edges
|
| 61 |
+
kernel = np.ones((3, 3), np.uint8)
|
| 62 |
+
dilated_edges = cv2.dilate(edges, kernel, iterations=1)
|
| 63 |
+
mask = np.logical_or(mask, dilated_edges)
|
| 64 |
+
|
| 65 |
alpha_channel = np.where(mask, 0, 255)
|
| 66 |
data[:, :, 3] = alpha_channel
|
| 67 |
return data
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Deploy using Gradio Client
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=color_remover_interface,
|