File size: 4,693 Bytes
964bace ddc2866 964bace 4f9f914 16dc00b c428c2d c837d24 4f9f914 964bace 4f9f914 0c16b58 4590ef9 964bace 4f9f914 964bace 4f9f914 964bace 0c16b58 4f9f914 0c16b58 4f9f914 0c16b58 4f9f914 83f30ea 0c16b58 964bace 83f30ea 964bace 83f30ea 4f9f914 0c16b58 4f9f914 0c16b58 4f9f914 964bace 4f9f914 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import os
import gradio as gr
from PIL import Image
import json
# Define global paths
BASE_PATH = "ContraCLIP/experiments/wip/"
EXPERIMENT_PATH = os.path.join(BASE_PATH, "ContraCLIP_stylegan2_ffhq1024-W-K21-D128-lss_beta_0.1-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.5-5000-expressions")
LATENT_CODES_DIR = os.path.join(EXPERIMENT_PATH, "results/stylegan2_ffhq1024-4/32_0.2_6.4")
SEMANTIC_DIPOLES_FILE = os.path.join(LATENT_CODES_DIR, "semantic_dipoles.json")
DEFAULT_IMAGE = "original_image.jpg"
# Load semantic dipoles
with open(SEMANTIC_DIPOLES_FILE, "r") as f:
semantic_dipoles = json.load(f)
# Transform semantic_dipoles into "A -> B" format
formatted_dipoles = [f"{pair[1]} -> {pair[0]}" for pair in semantic_dipoles]
# Helper to list all latent code folders
latent_code_folders = sorted(
[
folder
for folder in os.listdir(LATENT_CODES_DIR)
if os.path.isdir(os.path.join(LATENT_CODES_DIR, folder))
]
)
# Display predefined image paths based on semantic dipole index
def load_dipole_paths(latent_code):
latent_path = os.path.join(LATENT_CODES_DIR, latent_code, "paths_images")
paths = sorted(
[
f"path_{i:03d}"
for i in range(len(os.listdir(latent_path)))
]
)
return paths
def display_image(latent_code, formatted_dipole, frame_idx):
# Reverse-map "A -> B" format back to the index in semantic_dipoles
try:
index = formatted_dipoles.index(formatted_dipole)
except ValueError:
return f"Error: Semantic dipole '{formatted_dipole}' not found in the list."
path_dir = os.path.join(
LATENT_CODES_DIR, latent_code, "paths_images", f"path_{index:03d}"
)
frame_image_path = os.path.join(path_dir, f"{frame_idx:06d}.jpg")
if not os.path.exists(frame_image_path):
return f"Image not found: {frame_image_path}."
return Image.open(frame_image_path)
# Function to display GAN latent space interactive plot
def display_interactive_plot(latent_code):
# file_path = f"files/{LATENT_CODES_DIR}/{latent_code}/interactive_latent_space_{latent_code}.html"
file_path = f"/file/ContraCLIP/experiments/wip/ContraCLIP_stylegan2_ffhq1024-W-K21-D128-lss_beta_0.1-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.5-5000-expressions/results/stylegan2_ffhq1024-4/32_0.2_6.4/{latent_code}/interactive_latent_space_{latent_code}.html"
iframe_html = f'<iframe src="{file_path}" width="800" height="600" frameborder="0"></iframe>'
return iframe_html
# Gradio Interface
def build_interface():
with gr.Blocks() as demo:
gr.Markdown("# ContraCLIP-based Image Editing and Visualization Demo")
with gr.Row():
with gr.Column():
gr.Markdown("### Select Latent Code and Semantic Dipole")
latent_code_dropdown = gr.Dropdown(
latent_code_folders,
label="Latent Code",
value=latent_code_folders[0],
)
semantic_dipole_dropdown = gr.Dropdown(
formatted_dipoles,
label="Semantic Dipole",
value=formatted_dipoles[0], # Set default value
)
frame_slider = gr.Slider(
0, 32, step=1, label="Frame Index"
)
with gr.Column():
image_display = gr.Image(label="Image Preview")
html_display = gr.HTML(label="Interactive Latent Space")
# Update image based on latent code, semantic dipole, and frame index
def update_image(latent_code, semantic_dipole, frame_idx):
return display_image(latent_code, semantic_dipole, frame_idx)
# Update HTML display for the selected latent code
def update_html(latent_code):
return display_interactive_plot(latent_code)
# Link dropdowns and slider
frame_slider.change(
update_image,
[latent_code_dropdown, semantic_dipole_dropdown, frame_slider],
[image_display],
)
latent_code_dropdown.change(
update_html, [latent_code_dropdown], [html_display]
)
# Set up initial values
demo.load(
lambda: display_image(latent_code_folders[0], semantic_dipoles[0], 0),
inputs=[],
outputs=[image_display],
)
demo.load(
lambda: display_interactive_plot(latent_code_folders[0]),
inputs=[],
outputs=[html_display],
)
return demo
if __name__ == "__main__":
interface = build_interface()
interface.launch() |