Spaces:
Sleeping
Sleeping
mansi.modi@streebo.com commited on
Commit ·
8456379
1
Parent(s): 71fd256
mirror sticker
Browse files
app.py
CHANGED
|
@@ -170,13 +170,43 @@ def generate_3d_anaglyph_dynamic(person_img, side_by_side_bg, shift, scale_facto
|
|
| 170 |
final_rgb = cv2.cvtColor(final_bgr, cv2.COLOR_BGR2RGB)
|
| 171 |
|
| 172 |
# Create a full-image binary mask from the sticker's alpha channel.
|
| 173 |
-
# region_coords = (x_offset, y_offset, w, h)
|
| 174 |
x, y, w, h = region_coords
|
| 175 |
sticker_mask = (scaled_sticker[..., 3] > 128).astype(np.uint8) # 1 inside sticker, 0 outside
|
| 176 |
full_mask = np.zeros(final_rgb.shape[:2], dtype=np.uint8)
|
| 177 |
full_mask[y:y+h, x:x+w] = sticker_mask
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
return final_rgb, full_mask
|
| 181 |
|
| 182 |
def adjust_bg_brightness(anaglyph_rgb, sticker_mask, brightness_factor):
|
|
@@ -218,10 +248,11 @@ Adjust:
|
|
| 218 |
|
| 219 |
After the 3D image is generated, use the Background Brightness slider to adjust the brightness
|
| 220 |
of the background only (the sticker remains unchanged).
|
|
|
|
| 221 |
"""
|
| 222 |
|
| 223 |
with gr.Blocks() as demo:
|
| 224 |
-
gr.Markdown(f"# 3D Anaglyph Generator with Background Brightness
|
| 225 |
|
| 226 |
with gr.Row():
|
| 227 |
person_image = gr.Image(type="numpy", label="Upload Person Image")
|
|
@@ -257,5 +288,20 @@ with gr.Blocks() as demo:
|
|
| 257 |
inputs=[anaglyph_state, mask_state, brightness_slider],
|
| 258 |
outputs=output_image
|
| 259 |
)
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
demo.launch()
|
|
|
|
| 170 |
final_rgb = cv2.cvtColor(final_bgr, cv2.COLOR_BGR2RGB)
|
| 171 |
|
| 172 |
# Create a full-image binary mask from the sticker's alpha channel.
|
|
|
|
| 173 |
x, y, w, h = region_coords
|
| 174 |
sticker_mask = (scaled_sticker[..., 3] > 128).astype(np.uint8) # 1 inside sticker, 0 outside
|
| 175 |
full_mask = np.zeros(final_rgb.shape[:2], dtype=np.uint8)
|
| 176 |
full_mask[y:y+h, x:x+w] = sticker_mask
|
| 177 |
+
return final_rgb, full_mask
|
| 178 |
+
|
| 179 |
+
def generate_3d_anaglyph_dynamic_mirrored(person_img, side_by_side_bg, shift, scale_factor, x_frac, y_frac):
|
| 180 |
+
"""
|
| 181 |
+
Generate a 3D anaglyph image with the sticker mirrored horizontally.
|
| 182 |
+
The procedure is identical to generate_3d_anaglyph_dynamic except that the sticker is flipped.
|
| 183 |
+
Returns:
|
| 184 |
+
- final_rgb: The final composite (RGB).
|
| 185 |
+
- full_mask: The binary sticker mask.
|
| 186 |
+
"""
|
| 187 |
+
sticker = generate_sticker(person_img)
|
| 188 |
+
if sticker is None:
|
| 189 |
+
error_img = side_by_side_bg.copy()
|
| 190 |
+
cv2.putText(error_img, "No person detected.", (30, 30),
|
| 191 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
| 192 |
+
return error_img, None
|
| 193 |
+
|
| 194 |
+
# Mirror the sticker horizontally.
|
| 195 |
+
mirrored_sticker = cv2.flip(sticker, 1)
|
| 196 |
+
scaled_sticker = scale_sticker(mirrored_sticker, scale_factor)
|
| 197 |
+
left_bg, right_bg = split_side_by_side(side_by_side_bg)
|
| 198 |
+
|
| 199 |
+
left_overlay, region_coords = overlay_sticker_dynamic(left_bg.copy(), scaled_sticker, shift=-shift, x_frac=x_frac, y_frac=y_frac)
|
| 200 |
+
right_overlay, _ = overlay_sticker_dynamic(right_bg.copy(), scaled_sticker, shift=shift, x_frac=x_frac, y_frac=y_frac)
|
| 201 |
|
| 202 |
+
anaglyph = create_anaglyph(left_overlay, right_overlay)
|
| 203 |
+
final_bgr = anaglyph
|
| 204 |
+
final_rgb = cv2.cvtColor(final_bgr, cv2.COLOR_BGR2RGB)
|
| 205 |
+
|
| 206 |
+
x, y, w, h = region_coords
|
| 207 |
+
sticker_mask = (scaled_sticker[..., 3] > 128).astype(np.uint8)
|
| 208 |
+
full_mask = np.zeros(final_rgb.shape[:2], dtype=np.uint8)
|
| 209 |
+
full_mask[y:y+h, x:x+w] = sticker_mask
|
| 210 |
return final_rgb, full_mask
|
| 211 |
|
| 212 |
def adjust_bg_brightness(anaglyph_rgb, sticker_mask, brightness_factor):
|
|
|
|
| 248 |
|
| 249 |
After the 3D image is generated, use the Background Brightness slider to adjust the brightness
|
| 250 |
of the background only (the sticker remains unchanged).
|
| 251 |
+
You can also mirror the sticker using the 'Mirror Sticker' button.
|
| 252 |
"""
|
| 253 |
|
| 254 |
with gr.Blocks() as demo:
|
| 255 |
+
gr.Markdown(f"# 3D Anaglyph Generator with Background Brightness and Mirror Sticker Option\n\n{description}")
|
| 256 |
|
| 257 |
with gr.Row():
|
| 258 |
person_image = gr.Image(type="numpy", label="Upload Person Image")
|
|
|
|
| 288 |
inputs=[anaglyph_state, mask_state, brightness_slider],
|
| 289 |
outputs=output_image
|
| 290 |
)
|
| 291 |
+
|
| 292 |
+
mirror_btn = gr.Button("Mirror Sticker")
|
| 293 |
+
# When mirror_btn is clicked, regenerate the composite with the sticker mirrored.
|
| 294 |
+
mirror_btn.click(
|
| 295 |
+
generate_3d_anaglyph_dynamic_mirrored,
|
| 296 |
+
inputs=[person_image, side_by_side_bg, shift_slider, scale_slider, offset_x_slider, offset_y_slider],
|
| 297 |
+
outputs=[output_image, mask_state]
|
| 298 |
+
).then(
|
| 299 |
+
lambda x: x,
|
| 300 |
+
inputs=output_image,
|
| 301 |
+
outputs=anaglyph_state
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
# Place brightness slider and mirror button together.
|
| 305 |
+
gr.Markdown("Adjust the background brightness using the slider above, or mirror the sticker with the button below.")
|
| 306 |
+
|
| 307 |
demo.launch()
|