Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,9 +36,9 @@ pipe_depth = StableDiffusionControlNetPipeline.from_pretrained(
|
|
| 36 |
"runwayml/stable-diffusion-v1-5", controlnet=controlnet_depth, torch_dtype=torch.float16).to("cuda")
|
| 37 |
|
| 38 |
# Load a quick, highly accurate MiDaS model pipeline via torch hub
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
|
| 44 |
@spaces.GPU
|
|
@@ -49,6 +49,7 @@ def greet(n):
|
|
| 49 |
|
| 50 |
IMAGE_FOLDER = "./images" # Change this to your folder path
|
| 51 |
|
|
|
|
| 52 |
def generate_edgeImage(image_path):
|
| 53 |
# Convert image to numpy array to generate Canny edges
|
| 54 |
init_image = Image.open(image_path).convert("RGB")
|
|
@@ -59,7 +60,8 @@ def generate_edgeImage(image_path):
|
|
| 59 |
# Convert back to PIL format for the pipeline
|
| 60 |
canny_image = Image.fromarray(edges).convert("RGB")
|
| 61 |
return canny_image
|
| 62 |
-
|
|
|
|
| 63 |
def get_florence_segmentation(image, task_prompt="<REFERRING_EXPRESSION_SEGMENTATION>", text_input="the main object"):
|
| 64 |
"""
|
| 65 |
Prompts Florence-2 to isolate an object and map out a clean segmentation mask.
|
|
@@ -104,6 +106,7 @@ def get_florence_segmentation(image, task_prompt="<REFERRING_EXPRESSION_SEGMENTA
|
|
| 104 |
return mask
|
| 105 |
|
| 106 |
|
|
|
|
| 107 |
def generate_segmt_img(image_path):
|
| 108 |
init_image = Image.open(image_path).convert("RGB")
|
| 109 |
image_np = np.array(init_image)
|
|
@@ -113,6 +116,7 @@ def generate_segmt_img(image_path):
|
|
| 113 |
text_input="the center object square")
|
| 114 |
return conditioning_image
|
| 115 |
|
|
|
|
| 116 |
def extract_depth_map(image_path):
|
| 117 |
"""
|
| 118 |
Utilizes a lightweight MiDaS pipeline to extract depth estimation layouts.
|
|
@@ -139,7 +143,7 @@ def extract_depth_map(image_path):
|
|
| 139 |
depth_rgb = np.concatenate([normalized_depth[:, :, None]] * 3, axis=2)
|
| 140 |
return Image.fromarray(depth_rgb)
|
| 141 |
|
| 142 |
-
|
| 143 |
def get_image_list(folder):
|
| 144 |
if not os.path.exists(folder):
|
| 145 |
return []
|
|
@@ -148,12 +152,14 @@ def get_image_list(folder):
|
|
| 148 |
|
| 149 |
image_files = get_image_list(IMAGE_FOLDER)
|
| 150 |
|
|
|
|
| 151 |
def load_image(selected_file):
|
| 152 |
if not selected_file:
|
| 153 |
return None
|
| 154 |
return os.path.join(IMAGE_FOLDER, selected_file)
|
| 155 |
|
| 156 |
# generate input image
|
|
|
|
| 157 |
def generate_input_image(image_path,input_choice):
|
| 158 |
print(f"image_path: '{image_path}'")
|
| 159 |
print(f"input_choice: '{input_choice}'")
|
|
@@ -166,7 +172,8 @@ def generate_input_image(image_path,input_choice):
|
|
| 166 |
else:
|
| 167 |
conditioning_image = extract_depth_map(image_path)
|
| 168 |
return conditioning_image
|
| 169 |
-
|
|
|
|
| 170 |
def generate_output_image(prompt,input_choice):
|
| 171 |
#---------------------
|
| 172 |
if input_choice == "Edge image":
|
|
@@ -193,6 +200,7 @@ def generate_output_image(prompt,input_choice):
|
|
| 193 |
|
| 194 |
|
| 195 |
# Generation pipeline function using both the source image and text prompt
|
|
|
|
| 196 |
def process_and_generate(selected_file, prompt):
|
| 197 |
if not selected_file:
|
| 198 |
return None, "Please select an input image first."
|
|
|
|
| 36 |
"runwayml/stable-diffusion-v1-5", controlnet=controlnet_depth, torch_dtype=torch.float16).to("cuda")
|
| 37 |
|
| 38 |
# Load a quick, highly accurate MiDaS model pipeline via torch hub
|
| 39 |
+
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small").to(device).eval()
|
| 40 |
+
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
|
| 41 |
+
transform_depth = midas_transforms.flag_transform if hasattr(midas_transforms, 'flag_transform') else midas_transforms.dpt_transform
|
| 42 |
|
| 43 |
|
| 44 |
@spaces.GPU
|
|
|
|
| 49 |
|
| 50 |
IMAGE_FOLDER = "./images" # Change this to your folder path
|
| 51 |
|
| 52 |
+
@spaces.GPU
|
| 53 |
def generate_edgeImage(image_path):
|
| 54 |
# Convert image to numpy array to generate Canny edges
|
| 55 |
init_image = Image.open(image_path).convert("RGB")
|
|
|
|
| 60 |
# Convert back to PIL format for the pipeline
|
| 61 |
canny_image = Image.fromarray(edges).convert("RGB")
|
| 62 |
return canny_image
|
| 63 |
+
|
| 64 |
+
@spaces.GPU
|
| 65 |
def get_florence_segmentation(image, task_prompt="<REFERRING_EXPRESSION_SEGMENTATION>", text_input="the main object"):
|
| 66 |
"""
|
| 67 |
Prompts Florence-2 to isolate an object and map out a clean segmentation mask.
|
|
|
|
| 106 |
return mask
|
| 107 |
|
| 108 |
|
| 109 |
+
@spaces.GPU
|
| 110 |
def generate_segmt_img(image_path):
|
| 111 |
init_image = Image.open(image_path).convert("RGB")
|
| 112 |
image_np = np.array(init_image)
|
|
|
|
| 116 |
text_input="the center object square")
|
| 117 |
return conditioning_image
|
| 118 |
|
| 119 |
+
@spaces.GPU
|
| 120 |
def extract_depth_map(image_path):
|
| 121 |
"""
|
| 122 |
Utilizes a lightweight MiDaS pipeline to extract depth estimation layouts.
|
|
|
|
| 143 |
depth_rgb = np.concatenate([normalized_depth[:, :, None]] * 3, axis=2)
|
| 144 |
return Image.fromarray(depth_rgb)
|
| 145 |
|
| 146 |
+
@spaces.GPU
|
| 147 |
def get_image_list(folder):
|
| 148 |
if not os.path.exists(folder):
|
| 149 |
return []
|
|
|
|
| 152 |
|
| 153 |
image_files = get_image_list(IMAGE_FOLDER)
|
| 154 |
|
| 155 |
+
@spaces.GPU
|
| 156 |
def load_image(selected_file):
|
| 157 |
if not selected_file:
|
| 158 |
return None
|
| 159 |
return os.path.join(IMAGE_FOLDER, selected_file)
|
| 160 |
|
| 161 |
# generate input image
|
| 162 |
+
@spaces.GPU
|
| 163 |
def generate_input_image(image_path,input_choice):
|
| 164 |
print(f"image_path: '{image_path}'")
|
| 165 |
print(f"input_choice: '{input_choice}'")
|
|
|
|
| 172 |
else:
|
| 173 |
conditioning_image = extract_depth_map(image_path)
|
| 174 |
return conditioning_image
|
| 175 |
+
|
| 176 |
+
@spaces.GPU
|
| 177 |
def generate_output_image(prompt,input_choice):
|
| 178 |
#---------------------
|
| 179 |
if input_choice == "Edge image":
|
|
|
|
| 200 |
|
| 201 |
|
| 202 |
# Generation pipeline function using both the source image and text prompt
|
| 203 |
+
@spaces.GPU
|
| 204 |
def process_and_generate(selected_file, prompt):
|
| 205 |
if not selected_file:
|
| 206 |
return None, "Please select an input image first."
|