Spaces:
Runtime error
Runtime error
Commit ·
12a67b3
1
Parent(s): 0f25777
fix: photoshoot image path
Browse files
app.py
CHANGED
|
@@ -13,19 +13,22 @@ genai.configure(api_key=os.getenv("GENAI_API_KEY"))
|
|
| 13 |
|
| 14 |
def remove_background(image):
|
| 15 |
if isinstance(image, np.ndarray):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
def load_background_images(folder_path):
|
|
@@ -36,16 +39,33 @@ def load_background_images(folder_path):
|
|
| 36 |
if file.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 37 |
|
| 38 |
|
| 39 |
-
def replace_background(
|
| 40 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return None
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
if foreground is None:
|
| 45 |
return None
|
| 46 |
|
| 47 |
width, height = foreground.size
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
background = background.resize((width, height))
|
| 50 |
|
| 51 |
background.paste(foreground, (0, 0), foreground)
|
|
@@ -126,6 +146,12 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red)) as dem
|
|
| 126 |
value="Reset"
|
| 127 |
)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
with gr.Tab("Product Description"):
|
| 130 |
with gr.Row():
|
| 131 |
description_input = gr.Image(label="Product Image", type='filepath')
|
|
@@ -141,11 +167,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red)) as dem
|
|
| 141 |
inputs=description_input,
|
| 142 |
outputs=description_output
|
| 143 |
)
|
| 144 |
-
submit_btn.click(
|
| 145 |
-
fn=replace_background,
|
| 146 |
-
inputs=[input_image, background_image],
|
| 147 |
-
outputs=output_image
|
| 148 |
-
)
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
demo.launch(share=True, debug=True)
|
|
|
|
| 13 |
|
| 14 |
def remove_background(image):
|
| 15 |
if isinstance(image, np.ndarray):
|
| 16 |
+
try:
|
| 17 |
+
img = Image.fromarray(image)
|
| 18 |
+
buffered = BytesIO()
|
| 19 |
+
img.save(buffered, format="PNG")
|
| 20 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 21 |
+
|
| 22 |
+
output = replicate.run(
|
| 23 |
+
"cjwbw/rembg:fb8af171cfa1616ddcf1242c093f9c46bcada5ad4cf6f2fbe8b81b330ec5c003",
|
| 24 |
+
input={"image": f"data:image/png;base64,{img_base64}"}
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
response = requests.get(output)
|
| 28 |
+
return Image.open(BytesIO(response.content))
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error removing background: {str(e)}")
|
| 31 |
+
return None
|
| 32 |
|
| 33 |
|
| 34 |
def load_background_images(folder_path):
|
|
|
|
| 39 |
if file.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 40 |
|
| 41 |
|
| 42 |
+
def replace_background(input_image_path, background_image):
|
| 43 |
+
if input_image_path is None or background_image is None:
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
input_img = Image.open(input_image_path)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error loading input image: {e}")
|
| 50 |
return None
|
| 51 |
|
| 52 |
+
input_img_np = np.array(input_img)
|
| 53 |
+
|
| 54 |
+
foreground = remove_background(input_img_np)
|
| 55 |
if foreground is None:
|
| 56 |
return None
|
| 57 |
|
| 58 |
width, height = foreground.size
|
| 59 |
+
|
| 60 |
+
if isinstance(background_image, np.ndarray):
|
| 61 |
+
background = Image.fromarray(background_image)
|
| 62 |
+
else:
|
| 63 |
+
try:
|
| 64 |
+
background = Image.open(background_image) if isinstance(background_image, str) else background_image
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Error loading background image: {e}")
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
background = background.resize((width, height))
|
| 70 |
|
| 71 |
background.paste(foreground, (0, 0), foreground)
|
|
|
|
| 146 |
value="Reset"
|
| 147 |
)
|
| 148 |
|
| 149 |
+
submit_btn.click(
|
| 150 |
+
fn=replace_background,
|
| 151 |
+
inputs=[input_image, background_image],
|
| 152 |
+
outputs=output_image
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
with gr.Tab("Product Description"):
|
| 156 |
with gr.Row():
|
| 157 |
description_input = gr.Image(label="Product Image", type='filepath')
|
|
|
|
| 167 |
inputs=description_input,
|
| 168 |
outputs=description_output
|
| 169 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
demo.launch(share=True, debug=True)
|