dattarij commited on
Commit
4f9f914
·
verified ·
1 Parent(s): d0fe791

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -41
app.py CHANGED
@@ -2,54 +2,112 @@ import os
2
  import gradio as gr
3
  from PIL import Image
4
 
5
- # Paths for predefined dipole images
6
- BASE_PATH = "demo_images"
7
- IMAGES_DIR = os.path.join(BASE_PATH, "paths_images")
8
- ORIGINAL_IMAGE = os.path.join(BASE_PATH, "original_image.jpg")
9
-
10
- # Predefined dipole options based on folder structure
11
- dipole_options = sorted(
12
- [f"Path {i}" for i in range(len(os.listdir(IMAGES_DIR)))]
13
- )
14
 
15
- # Display edited images based on user-selected dipole and slider strength
16
- def edit_image(attribute: str, slider_value: int):
17
- index = dipole_options.index(attribute)
18
- image_dir = os.path.join(IMAGES_DIR, f"path_{index:03d}")
19
 
20
- # Determine frame from slider value
21
- frame_path = os.path.join(image_dir, f"{slider_value:06d}.jpg")
 
 
 
 
 
 
22
 
23
- # Load and return the image
24
- frame_image = Image.open(frame_path)
25
- return frame_image
 
 
 
 
 
 
 
26
 
27
- # Display the original image
28
- def display_original_image():
29
- return Image.open(ORIGINAL_IMAGE)
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Gradio Interface
32
  def build_interface():
33
- with gr.Row():
34
- gr.Markdown("# ContraCLIP Predefined Editing Demo")
35
-
36
- with gr.Row():
37
- gr.Image(display_original_image(), label="Original Image", type="pil")
38
-
39
- demo = gr.Interface(
40
- fn=edit_image,
41
- inputs=[
42
- gr.Dropdown(dipole_options, label="Select Attribute"),
43
- gr.Slider(0, 32, step=1, label="Frame Index"), # Adjust slider range based on actual frames
44
- ],
45
- outputs=gr.Image(type="pil"),
46
- live=True,
47
- title="Predefined ContraCLIP Image Editing",
48
- description="Select an attribute and adjust the frame index to view the edited image."
49
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Launch the Interface
52
- demo.launch()
53
 
54
  if __name__ == "__main__":
55
- build_interface()
 
 
2
  import gradio as gr
3
  from PIL import Image
4
 
5
+ # Define global paths
6
+ BASE_PATH = "disentangled-image-editing-final-project/ContraCLIP/experiments/wip"
7
+ LATENT_CODES_DIR = os.path.join(BASE_PATH, "results")
8
+ SEMANTIC_DIPOLES_FILE = os.path.join(LATENT_CODES_DIR, "semantic_dipoles.json")
9
+ DEFAULT_IMAGE = "original_image.jpg"
 
 
 
 
10
 
11
+ # Load semantic dipoles
12
+ with open(SEMANTIC_DIPOLES_FILE, "r") as f:
13
+ semantic_dipoles = json.load(f)
 
14
 
15
+ # Helper to list all latent code folders
16
+ latent_code_folders = sorted(
17
+ [
18
+ folder
19
+ for folder in os.listdir(LATENT_CODES_DIR)
20
+ if os.path.isdir(os.path.join(LATENT_CODES_DIR, folder))
21
+ ]
22
+ )
23
 
24
+ # Display predefined image paths based on semantic dipole index
25
+ def load_dipole_paths(latent_code):
26
+ latent_path = os.path.join(LATENT_CODES_DIR, latent_code, "paths_images")
27
+ paths = sorted(
28
+ [
29
+ f"path_{i:03d}"
30
+ for i in range(len(os.listdir(latent_path)))
31
+ ]
32
+ )
33
+ return paths
34
 
35
+ # Function to display images
36
+ def display_image(latent_code, semantic_dipole, frame_idx):
37
+ index = semantic_dipoles.index(semantic_dipole)
38
+ path_dir = os.path.join(LATENT_CODES_DIR, latent_code, "paths_images", f"path_{index:03d}")
39
+ frame_image_path = os.path.join(path_dir, f"{frame_idx:06d}.jpg")
40
+ if not os.path.exists(frame_image_path):
41
+ return f"Image not found: {frame_image_path}"
42
+ return Image.open(frame_image_path)
43
+
44
+ # Function to display GAN latent space interactive plot
45
+ def display_interactive_plot(latent_code):
46
+ html_file = os.path.join(LATENT_CODES_DIR, latent_code, "interactive_latent_space.html")
47
+ if not os.path.exists(html_file):
48
+ return f"Interactive file not found: {html_file}"
49
+ with open(html_file, "r") as file:
50
+ return file.read()
51
 
52
  # Gradio Interface
53
  def build_interface():
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown("# ContraCLIP-based Image Editing and Visualization Demo")
56
+
57
+ with gr.Row():
58
+ with gr.Column():
59
+ gr.Markdown("### Select Latent Code and Semantic Dipole")
60
+ latent_code_dropdown = gr.Dropdown(
61
+ latent_code_folders,
62
+ label="Latent Code",
63
+ value=latent_code_folders[0],
64
+ )
65
+ semantic_dipole_dropdown = gr.Dropdown(
66
+ semantic_dipoles,
67
+ label="Semantic Dipole",
68
+ value=semantic_dipoles[0],
69
+ )
70
+ frame_slider = gr.Slider(
71
+ 0, 32, step=1, label="Frame Index"
72
+ )
73
+
74
+ with gr.Column():
75
+ image_display = gr.Image(label="Image Preview")
76
+ html_display = gr.HTML(label="Interactive Latent Space")
77
+
78
+ # Update image based on latent code, semantic dipole, and frame index
79
+ def update_image(latent_code, semantic_dipole, frame_idx):
80
+ return display_image(latent_code, semantic_dipole, frame_idx)
81
+
82
+ # Update HTML display for the selected latent code
83
+ def update_html(latent_code):
84
+ return display_interactive_plot(latent_code)
85
+
86
+ # Link dropdowns and slider
87
+ frame_slider.change(
88
+ update_image,
89
+ [latent_code_dropdown, semantic_dipole_dropdown, frame_slider],
90
+ [image_display],
91
+ )
92
+ latent_code_dropdown.change(
93
+ update_html, [latent_code_dropdown], [html_display]
94
+ )
95
+
96
+ # Set up initial values
97
+ demo.load(
98
+ lambda: display_image(latent_code_folders[0], semantic_dipoles[0], 0),
99
+ inputs=[],
100
+ outputs=[image_display],
101
+ )
102
+ demo.load(
103
+ lambda: display_interactive_plot(latent_code_folders[0]),
104
+ inputs=[],
105
+ outputs=[html_display],
106
+ )
107
+
108
+ return demo
109
 
 
 
110
 
111
  if __name__ == "__main__":
112
+ interface = build_interface()
113
+ interface.launch()