Spaces:
Running
on
Zero
Running
on
Zero
Update app_3.py
Browse files
app_3.py
CHANGED
@@ -542,7 +542,6 @@ def preprocess_image(image: Image.Image, height=768, width=768):
|
|
542 |
image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
|
543 |
image = (image * 255).clip(0, 255).astype(np.uint8)
|
544 |
image = Image.fromarray(image)
|
545 |
-
|
546 |
return image
|
547 |
|
548 |
|
@@ -996,6 +995,7 @@ def use_orientation(selected_image:gr.SelectData):
|
|
996 |
@torch.inference_mode
|
997 |
def process_image(input_image, input_text):
|
998 |
"""Main processing function for the Gradio interface"""
|
|
|
999 |
if isinstance(input_image, Image.Image):
|
1000 |
input_image = np.array(input_image)
|
1001 |
|
@@ -1085,14 +1085,34 @@ def process_image(input_image, input_text):
|
|
1085 |
# Get original RGB image
|
1086 |
img = input_image.copy()
|
1087 |
|
1088 |
-
# rgba = preprocess_image(img)
|
1089 |
|
1090 |
-
|
1091 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1092 |
# get the bounding box of alpha
|
1093 |
-
y, x = np.where(alpha)
|
1094 |
y0, y1 = max(y.min() - 1, 0), min(y.max() + 1, H)
|
1095 |
x0, x1 = max(x.min() - 1, 0), min(x.max() + 1, W)
|
|
|
1096 |
image_center = img[y0:y1, x0:x1]
|
1097 |
# resize the longer side to H * 0.9
|
1098 |
H, W, _ = image_center.shape
|
@@ -1102,6 +1122,7 @@ def process_image(input_image, input_text):
|
|
1102 |
else:
|
1103 |
H = int(H * (width * 0.9) / W)
|
1104 |
W = int(width * 0.9)
|
|
|
1105 |
image_center = np.array(Image.fromarray(image_center).resize((W, H)))
|
1106 |
# pad to H, W
|
1107 |
start_h = (height - H) // 2
|
@@ -1113,24 +1134,6 @@ def process_image(input_image, input_text):
|
|
1113 |
image = (image * 255).clip(0, 255).astype(np.uint8)
|
1114 |
image = Image.fromarray(image)
|
1115 |
|
1116 |
-
# H, W, C = img.shape
|
1117 |
-
|
1118 |
-
# # Create RGBA image
|
1119 |
-
# alpha = np.zeros((H, W, 1), dtype=np.uint8)
|
1120 |
-
# alpha[first_mask] = 255
|
1121 |
-
# rgba = np.dstack((img, alpha)).astype(np.uint8)
|
1122 |
-
|
1123 |
-
# Crop to mask bounds to minimize image size
|
1124 |
-
# y_indices, x_indices = np.where(first_mask)
|
1125 |
-
# y_min, y_max = y_indices.min(), y_indices.max()
|
1126 |
-
# x_min, x_max = x_indices.min(), x_indices.max()
|
1127 |
-
|
1128 |
-
# Crop the RGBA image
|
1129 |
-
#cropped_rgba = rgba[y_min:y_max+1, x_min:x_max+1]
|
1130 |
-
|
1131 |
-
# Set extracted foreground for mask mover
|
1132 |
-
# mask_mover.set_extracted_fg(cropped_rgba)
|
1133 |
-
|
1134 |
return annotated_frame, image, gr.update(visible=False), gr.update(visible=False)
|
1135 |
|
1136 |
|
@@ -1244,23 +1247,57 @@ def process_image(input_image, input_text):
|
|
1244 |
img = input_image.copy()
|
1245 |
H, W, C = img.shape
|
1246 |
|
|
|
|
|
|
|
|
|
1247 |
# Create RGBA image
|
1248 |
alpha = np.zeros((H, W, 1), dtype=np.uint8)
|
|
|
1249 |
alpha[first_mask] = 255
|
1250 |
-
|
|
|
1251 |
|
1252 |
# Crop to mask bounds to minimize image size
|
1253 |
-
y_indices, x_indices = np.where(first_mask)
|
1254 |
-
y_min, y_max = y_indices.min(), y_indices.max()
|
1255 |
-
x_min, x_max = x_indices.min(), x_indices.max()
|
1256 |
|
1257 |
# Crop the RGBA image
|
1258 |
-
cropped_rgba = rgba[y_min:y_max+1, x_min:x_max+1]
|
1259 |
|
1260 |
# Set extracted foreground for mask mover
|
1261 |
# mask_mover.set_extracted_fg(cropped_rgba)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1262 |
|
1263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1264 |
return annotated_frame, None, gr.update(visible=False), gr.update(visible=False)
|
1265 |
|
1266 |
|
@@ -1317,9 +1354,9 @@ with block:
|
|
1317 |
bg_source = gr.Radio(choices=[e.value for e in list(BGSource)[2:]],
|
1318 |
value=BGSource.LEFT.value,
|
1319 |
label="Lighting Preference (Initial Latent)", type='value')
|
1320 |
-
|
1321 |
-
|
1322 |
-
|
1323 |
with gr.Row():
|
1324 |
relight_button = gr.Button(value="Relight")
|
1325 |
|
|
|
542 |
image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
|
543 |
image = (image * 255).clip(0, 255).astype(np.uint8)
|
544 |
image = Image.fromarray(image)
|
|
|
545 |
return image
|
546 |
|
547 |
|
|
|
995 |
@torch.inference_mode
|
996 |
def process_image(input_image, input_text):
|
997 |
"""Main processing function for the Gradio interface"""
|
998 |
+
|
999 |
if isinstance(input_image, Image.Image):
|
1000 |
input_image = np.array(input_image)
|
1001 |
|
|
|
1085 |
# Get original RGB image
|
1086 |
img = input_image.copy()
|
1087 |
|
|
|
1088 |
|
1089 |
+
H, W, C = img.shape
|
1090 |
+
|
1091 |
+
# Create RGBA image
|
1092 |
+
alpha = np.zeros((H, W, 1), dtype=np.uint8)
|
1093 |
+
|
1094 |
+
alpha[first_mask] = 255
|
1095 |
+
|
1096 |
+
# rgba = np.dstack((img, alpha)).astype(np.uint8)
|
1097 |
+
|
1098 |
+
# Crop to mask bounds to minimize image size
|
1099 |
+
# y_indices, x_indices = np.where(first_mask)
|
1100 |
+
# y_min, y_max = y_indices.min(), y_indices.max()
|
1101 |
+
# x_min, x_max = x_indices.min(), x_indices.max()
|
1102 |
+
|
1103 |
+
# Crop the RGBA image
|
1104 |
+
# cropped_rgba = rgba[y_min:y_max+1, x_min:x_max+1]
|
1105 |
+
|
1106 |
+
# Set extracted foreground for mask mover
|
1107 |
+
# mask_mover.set_extracted_fg(cropped_rgba)
|
1108 |
+
|
1109 |
+
# alpha = img[..., 3] > 0
|
1110 |
+
H, W = alpha[first_mask].shape
|
1111 |
# get the bounding box of alpha
|
1112 |
+
y, x = np.where(alpha[first_mask])
|
1113 |
y0, y1 = max(y.min() - 1, 0), min(y.max() + 1, H)
|
1114 |
x0, x1 = max(x.min() - 1, 0), min(x.max() + 1, W)
|
1115 |
+
|
1116 |
image_center = img[y0:y1, x0:x1]
|
1117 |
# resize the longer side to H * 0.9
|
1118 |
H, W, _ = image_center.shape
|
|
|
1122 |
else:
|
1123 |
H = int(H * (width * 0.9) / W)
|
1124 |
W = int(width * 0.9)
|
1125 |
+
|
1126 |
image_center = np.array(Image.fromarray(image_center).resize((W, H)))
|
1127 |
# pad to H, W
|
1128 |
start_h = (height - H) // 2
|
|
|
1134 |
image = (image * 255).clip(0, 255).astype(np.uint8)
|
1135 |
image = Image.fromarray(image)
|
1136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1137 |
return annotated_frame, image, gr.update(visible=False), gr.update(visible=False)
|
1138 |
|
1139 |
|
|
|
1247 |
img = input_image.copy()
|
1248 |
H, W, C = img.shape
|
1249 |
|
1250 |
+
first_mask = detections.mask[0]
|
1251 |
+
|
1252 |
+
|
1253 |
+
|
1254 |
# Create RGBA image
|
1255 |
alpha = np.zeros((H, W, 1), dtype=np.uint8)
|
1256 |
+
|
1257 |
alpha[first_mask] = 255
|
1258 |
+
|
1259 |
+
# rgba = np.dstack((img, alpha)).astype(np.uint8)
|
1260 |
|
1261 |
# Crop to mask bounds to minimize image size
|
1262 |
+
# y_indices, x_indices = np.where(first_mask)
|
1263 |
+
# y_min, y_max = y_indices.min(), y_indices.max()
|
1264 |
+
# x_min, x_max = x_indices.min(), x_indices.max()
|
1265 |
|
1266 |
# Crop the RGBA image
|
1267 |
+
# cropped_rgba = rgba[y_min:y_max+1, x_min:x_max+1]
|
1268 |
|
1269 |
# Set extracted foreground for mask mover
|
1270 |
# mask_mover.set_extracted_fg(cropped_rgba)
|
1271 |
+
|
1272 |
+
# alpha = img[..., 3] > 0
|
1273 |
+
H, W = alpha[first_mask].shape
|
1274 |
+
# get the bounding box of alpha
|
1275 |
+
y, x = np.where(alpha[first_mask])
|
1276 |
+
y0, y1 = max(y.min() - 1, 0), min(y.max() + 1, H)
|
1277 |
+
x0, x1 = max(x.min() - 1, 0), min(x.max() + 1, W)
|
1278 |
|
1279 |
+
image_center = img[y0:y1, x0:x1]
|
1280 |
+
# resize the longer side to H * 0.9
|
1281 |
+
H, W, _ = image_center.shape
|
1282 |
+
if H > W:
|
1283 |
+
W = int(W * (height * 0.9) / H)
|
1284 |
+
H = int(height * 0.9)
|
1285 |
+
else:
|
1286 |
+
H = int(H * (width * 0.9) / W)
|
1287 |
+
W = int(width * 0.9)
|
1288 |
+
|
1289 |
+
image_center = np.array(Image.fromarray(image_center).resize((W, H)))
|
1290 |
+
# pad to H, W
|
1291 |
+
start_h = (height - H) // 2
|
1292 |
+
start_w = (width - W) // 2
|
1293 |
+
image = np.zeros((height, width, 4), dtype=np.uint8)
|
1294 |
+
image[start_h : start_h + H, start_w : start_w + W] = image_center
|
1295 |
+
image = image.astype(np.float32) / 255.0
|
1296 |
+
image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
|
1297 |
+
image = (image * 255).clip(0, 255).astype(np.uint8)
|
1298 |
+
image = Image.fromarray(image)
|
1299 |
+
|
1300 |
+
return annotated_frame, image, gr.update(visible=False), gr.update(visible=False)
|
1301 |
return annotated_frame, None, gr.update(visible=False), gr.update(visible=False)
|
1302 |
|
1303 |
|
|
|
1354 |
bg_source = gr.Radio(choices=[e.value for e in list(BGSource)[2:]],
|
1355 |
value=BGSource.LEFT.value,
|
1356 |
label="Lighting Preference (Initial Latent)", type='value')
|
1357 |
+
|
1358 |
+
example_quick_subjects = gr.Dataset(samples=quick_subjects, label='Subject Quick List', samples_per_page=1000, components=[prompt])
|
1359 |
+
example_quick_prompts = gr.Dataset(samples=quick_prompts, label='Lighting Quick List', samples_per_page=1000, components=[prompt])
|
1360 |
with gr.Row():
|
1361 |
relight_button = gr.Button(value="Relight")
|
1362 |
|