Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Code doc.docx +0 -0
- app.py +120 -0
- requirements.txt +10 -0
Code doc.docx
ADDED
Binary file (27.9 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
from transformers import AutoModelForImageSegmentation
|
7 |
+
import torch
|
8 |
+
from torchvision import transforms
|
9 |
+
import spaces # Import ZeroGPU support
|
10 |
+
|
11 |
+
# Detect if CUDA is available; otherwise, fallback to CPU
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
# Load BiRefNet model
|
15 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
16 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
17 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
18 |
+
)
|
19 |
+
birefnet.to(device)
|
20 |
+
|
21 |
+
# Image transformation pipeline
|
22 |
+
transform_image = transforms.Compose(
|
23 |
+
[
|
24 |
+
transforms.Resize((1024, 1024)),
|
25 |
+
transforms.ToTensor(),
|
26 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
27 |
+
]
|
28 |
+
)
|
29 |
+
|
30 |
+
@spaces.GPU(duration=70) # Decorate to ensure GPU is allocated only during model loading
|
31 |
+
|
32 |
+
# Function to extract the subject using BiRefNet and create a mask
|
33 |
+
def create_mask(image):
|
34 |
+
image_size = image.size
|
35 |
+
input_images = transform_image(image).unsqueeze(0).to(device)
|
36 |
+
|
37 |
+
with torch.no_grad():
|
38 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu() # Always move results to CPU for processing
|
39 |
+
|
40 |
+
pred = preds[0].squeeze()
|
41 |
+
mask_pil = transforms.ToPILImage()(pred)
|
42 |
+
mask = mask_pil.resize(image_size)
|
43 |
+
|
44 |
+
return mask
|
45 |
+
|
46 |
+
# Function to apply the pink filter-like color change
|
47 |
+
def apply_filter(image, mask=None, apply_to_subject=True):
|
48 |
+
# Convert image to numpy array
|
49 |
+
image_np = np.array(image.convert("RGBA"))
|
50 |
+
|
51 |
+
# Define the pink color in RGBA
|
52 |
+
pink_color = np.array([255, 0, 255, 128]) # Pink color with transparency (alpha = 128)
|
53 |
+
|
54 |
+
if apply_to_subject and mask is not None:
|
55 |
+
# Convert mask to numpy array
|
56 |
+
mask_np = np.array(mask)
|
57 |
+
|
58 |
+
# Blend the original image with the pink color where the mask is applied
|
59 |
+
for i in range(image_np.shape[0]):
|
60 |
+
for j in range(image_np.shape[1]):
|
61 |
+
if mask_np[i, j] > 128: # Check if the mask value indicates subject presence
|
62 |
+
image_np[i, j] = (image_np[i, j] * 0.5 + pink_color * 0.5).astype(np.uint8)
|
63 |
+
else:
|
64 |
+
# Apply the pink filter to the whole image if no subject is detected or if chosen by user
|
65 |
+
image_np = (image_np * 0.5 + pink_color * 0.5).astype(np.uint8)
|
66 |
+
|
67 |
+
# Convert back to PIL image
|
68 |
+
result_image = Image.fromarray(image_np)
|
69 |
+
|
70 |
+
return result_image
|
71 |
+
|
72 |
+
# Main processing function for Gradio
|
73 |
+
def process(input_image, subject_choice):
|
74 |
+
if input_image is None:
|
75 |
+
raise gr.Error('Please upload an input image')
|
76 |
+
|
77 |
+
# Convert input image to PIL image
|
78 |
+
original_image = Image.fromarray(input_image)
|
79 |
+
|
80 |
+
# Default mask is None
|
81 |
+
mask = None
|
82 |
+
|
83 |
+
# Generate mask using BiRefNet if the user selected "Subject Only"
|
84 |
+
if subject_choice == "Subject Only":
|
85 |
+
mask = create_mask(original_image)
|
86 |
+
|
87 |
+
# Apply pink filter based on user choice
|
88 |
+
apply_to_subject = (subject_choice == "Subject Only" and mask is not None)
|
89 |
+
result_image = apply_filter(original_image, mask, apply_to_subject)
|
90 |
+
|
91 |
+
return result_image
|
92 |
+
|
93 |
+
# Define Gradio Interface
|
94 |
+
block = gr.Blocks()
|
95 |
+
|
96 |
+
with block:
|
97 |
+
with gr.Row():
|
98 |
+
gr.Markdown("Apply Pink Filter Effect to Subject or Full Image")
|
99 |
+
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column():
|
102 |
+
input_image = gr.Image(type="numpy", label="Input Image", height=640)
|
103 |
+
subject_choice = gr.Radio(
|
104 |
+
choices=["Subject Only", "Full Image"],
|
105 |
+
value="Subject Only",
|
106 |
+
label="Apply Pink Filter to:"
|
107 |
+
)
|
108 |
+
run_button = gr.Button("Run")
|
109 |
+
|
110 |
+
with gr.Column():
|
111 |
+
output_image = gr.Image(label="Output Image")
|
112 |
+
|
113 |
+
# Set the processing function
|
114 |
+
run_button.click(
|
115 |
+
fn=process,
|
116 |
+
inputs=[input_image, subject_choice],
|
117 |
+
outputs=output_image
|
118 |
+
)
|
119 |
+
|
120 |
+
block.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.0.1
|
2 |
+
torchvision==0.15.2
|
3 |
+
opencv-python==4.9.0.80
|
4 |
+
tqdm==4.66.2
|
5 |
+
timm==0.9.16
|
6 |
+
prettytable==3.10.0
|
7 |
+
scipy==1.12.0
|
8 |
+
scikit-image==0.22.0
|
9 |
+
kornia==0.7.1
|
10 |
+
transformers
|