Spaces:
Sleeping
Sleeping
mahan_ym
commited on
Commit
·
79b337b
1
Parent(s):
4255d22
Add detection threshold slider and update privacy preservation functions. added owl2
Browse files- src/app.py +14 -3
- src/modal_app.py +99 -6
- src/tools.py +5 -0
src/app.py
CHANGED
|
@@ -107,6 +107,14 @@ privacy_preserve_tool = gr.Interface(
|
|
| 107 |
step=1,
|
| 108 |
info="Higher values result in stronger blurring.",
|
| 109 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
],
|
| 111 |
outputs=gr.Image(label="Output Image"),
|
| 112 |
title="Privacy Preserving Tool",
|
|
@@ -114,18 +122,21 @@ privacy_preserve_tool = gr.Interface(
|
|
| 114 |
examples=[
|
| 115 |
[
|
| 116 |
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_3.jpg",
|
| 117 |
-
"license plate
|
| 118 |
10,
|
|
|
|
| 119 |
],
|
| 120 |
[
|
| 121 |
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_8.jpg",
|
| 122 |
-
"face
|
| 123 |
15,
|
|
|
|
| 124 |
],
|
| 125 |
[
|
| 126 |
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_6.jpg",
|
| 127 |
-
"face
|
| 128 |
20,
|
|
|
|
| 129 |
],
|
| 130 |
],
|
| 131 |
)
|
|
|
|
| 107 |
step=1,
|
| 108 |
info="Higher values result in stronger blurring.",
|
| 109 |
),
|
| 110 |
+
gr.Slider(
|
| 111 |
+
label="Detection Threshold",
|
| 112 |
+
minimum=0.01,
|
| 113 |
+
maximum=0.99,
|
| 114 |
+
value=0.2,
|
| 115 |
+
step=0.01,
|
| 116 |
+
info="Model threshold for detecting objects.",
|
| 117 |
+
),
|
| 118 |
],
|
| 119 |
outputs=gr.Image(label="Output Image"),
|
| 120 |
title="Privacy Preserving Tool",
|
|
|
|
| 122 |
examples=[
|
| 123 |
[
|
| 124 |
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_3.jpg",
|
| 125 |
+
"license plate",
|
| 126 |
10,
|
| 127 |
+
0.5,
|
| 128 |
],
|
| 129 |
[
|
| 130 |
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_8.jpg",
|
| 131 |
+
"face",
|
| 132 |
15,
|
| 133 |
+
0.1,
|
| 134 |
],
|
| 135 |
[
|
| 136 |
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_6.jpg",
|
| 137 |
+
"face",
|
| 138 |
20,
|
| 139 |
+
0.1,
|
| 140 |
],
|
| 141 |
],
|
| 142 |
)
|
src/modal_app.py
CHANGED
|
@@ -39,6 +39,7 @@ image = (
|
|
| 39 |
"numpy",
|
| 40 |
"transformers",
|
| 41 |
"opencv-contrib-python-headless",
|
|
|
|
| 42 |
gpu="A10G",
|
| 43 |
)
|
| 44 |
.pip_install(
|
|
@@ -102,6 +103,47 @@ def prompt_segment(
|
|
| 102 |
volumes={volume_path: volume},
|
| 103 |
timeout=60 * 3,
|
| 104 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def sam2(image_pil: Image.Image, boxes: list[np.ndarray]) -> list[dict]:
|
| 106 |
import torch
|
| 107 |
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
@@ -121,7 +163,56 @@ def sam2(image_pil: Image.Image, boxes: list[np.ndarray]) -> list[dict]:
|
|
| 121 |
|
| 122 |
@app.function(
|
| 123 |
image=image,
|
| 124 |
-
gpu="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
volumes={volume_path: volume},
|
| 126 |
timeout=60 * 3,
|
| 127 |
)
|
|
@@ -234,7 +325,7 @@ def clip(
|
|
| 234 |
|
| 235 |
|
| 236 |
@app.function(
|
| 237 |
-
gpu="
|
| 238 |
image=image,
|
| 239 |
volumes={volume_path: volume},
|
| 240 |
timeout=60 * 3,
|
|
@@ -332,7 +423,7 @@ def change_image_objects_hsv(
|
|
| 332 |
|
| 333 |
|
| 334 |
@app.function(
|
| 335 |
-
gpu="
|
| 336 |
image=image,
|
| 337 |
volumes={volume_path: volume},
|
| 338 |
timeout=60 * 3,
|
|
@@ -404,7 +495,7 @@ def change_image_objects_lab(
|
|
| 404 |
|
| 405 |
|
| 406 |
@app.function(
|
| 407 |
-
gpu="
|
| 408 |
image=image,
|
| 409 |
volumes={volume_path: volume},
|
| 410 |
timeout=60 * 3,
|
|
@@ -432,7 +523,7 @@ def apply_mosaic_with_bool_mask(
|
|
| 432 |
|
| 433 |
|
| 434 |
@app.function(
|
| 435 |
-
gpu="
|
| 436 |
image=image,
|
| 437 |
volumes={volume_path: volume},
|
| 438 |
timeout=60 * 3,
|
|
@@ -441,6 +532,7 @@ def preserve_privacy(
|
|
| 441 |
image_pil: Image.Image,
|
| 442 |
prompts: str,
|
| 443 |
privacy_strength: int = 15,
|
|
|
|
| 444 |
) -> Image.Image:
|
| 445 |
"""
|
| 446 |
Preserves privacy in an image by applying a mosaic effect to specified objects.
|
|
@@ -449,9 +541,10 @@ def preserve_privacy(
|
|
| 449 |
if isinstance(prompts, str):
|
| 450 |
prompts = [prompt.strip() for prompt in prompts.split(".")]
|
| 451 |
print(f"Parsed prompts: {prompts}")
|
| 452 |
-
prompt_segment_results =
|
| 453 |
image_pil=image_pil,
|
| 454 |
prompts=prompts,
|
|
|
|
| 455 |
)
|
| 456 |
if not prompt_segment_results:
|
| 457 |
return image_pil
|
|
|
|
| 39 |
"numpy",
|
| 40 |
"transformers",
|
| 41 |
"opencv-contrib-python-headless",
|
| 42 |
+
"scipy",
|
| 43 |
gpu="A10G",
|
| 44 |
)
|
| 45 |
.pip_install(
|
|
|
|
| 103 |
volumes={volume_path: volume},
|
| 104 |
timeout=60 * 3,
|
| 105 |
)
|
| 106 |
+
def privacy_prompt_segment(
|
| 107 |
+
image_pil: Image.Image,
|
| 108 |
+
prompts: list[str],
|
| 109 |
+
threshold: float,
|
| 110 |
+
) -> list[dict]:
|
| 111 |
+
owlv2_results = owlv2.remote(image_pil, prompts, threshold=threshold)
|
| 112 |
+
|
| 113 |
+
if not owlv2_results:
|
| 114 |
+
print("No boxes returned from OWLV2.")
|
| 115 |
+
return None
|
| 116 |
+
|
| 117 |
+
boxes = np.array(owlv2_results["boxes"])
|
| 118 |
+
|
| 119 |
+
sam_result_masks, sam_result_scores = sam2.remote(image_pil=image_pil, boxes=boxes)
|
| 120 |
+
|
| 121 |
+
print(f"sam_result_mask {sam_result_masks}")
|
| 122 |
+
|
| 123 |
+
if not sam_result_masks.any():
|
| 124 |
+
print("No masks or scores returned from SAM2.")
|
| 125 |
+
return None
|
| 126 |
+
|
| 127 |
+
if sam_result_masks.ndim == 3:
|
| 128 |
+
# If the masks are in 3D, we need to convert them to 4D
|
| 129 |
+
sam_result_masks = [sam_result_masks]
|
| 130 |
+
|
| 131 |
+
results = {
|
| 132 |
+
"labels": owlv2_results["labels"],
|
| 133 |
+
"boxes": boxes,
|
| 134 |
+
"owlv2_scores": owlv2_results["scores"],
|
| 135 |
+
"sam_masking_scores": sam_result_scores,
|
| 136 |
+
"masks": sam_result_masks,
|
| 137 |
+
}
|
| 138 |
+
return results
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
@app.function(
|
| 142 |
+
image=image,
|
| 143 |
+
gpu="A100",
|
| 144 |
+
volumes={volume_path: volume},
|
| 145 |
+
timeout=60 * 3,
|
| 146 |
+
)
|
| 147 |
def sam2(image_pil: Image.Image, boxes: list[np.ndarray]) -> list[dict]:
|
| 148 |
import torch
|
| 149 |
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
|
|
| 163 |
|
| 164 |
@app.function(
|
| 165 |
image=image,
|
| 166 |
+
gpu="A100",
|
| 167 |
+
volumes={volume_path: volume},
|
| 168 |
+
)
|
| 169 |
+
def owlv2(
|
| 170 |
+
image_pil: Image.Image,
|
| 171 |
+
labels: list[str],
|
| 172 |
+
threshold: float,
|
| 173 |
+
) -> list[dict]:
|
| 174 |
+
"""
|
| 175 |
+
Perform zero-shot segmentation on an image using specified labels.
|
| 176 |
+
Args:
|
| 177 |
+
image_pil (Image.Image): The input image as a PIL Image.
|
| 178 |
+
labels (list[str]): List of labels for zero-shot segmentation.
|
| 179 |
+
|
| 180 |
+
Returns:
|
| 181 |
+
list[dict]: List of dictionaries containing label and bounding box information.
|
| 182 |
+
"""
|
| 183 |
+
from transformers import pipeline
|
| 184 |
+
|
| 185 |
+
checkpoint = "google/owlv2-large-patch14-ensemble"
|
| 186 |
+
detector = pipeline(
|
| 187 |
+
model=checkpoint,
|
| 188 |
+
task="zero-shot-object-detection",
|
| 189 |
+
device="cuda",
|
| 190 |
+
use_fast=True,
|
| 191 |
+
)
|
| 192 |
+
# Load the image
|
| 193 |
+
predictions = detector(
|
| 194 |
+
image_pil,
|
| 195 |
+
candidate_labels=labels,
|
| 196 |
+
)
|
| 197 |
+
labels = []
|
| 198 |
+
scores = []
|
| 199 |
+
boxes = []
|
| 200 |
+
for prediction in predictions:
|
| 201 |
+
if prediction["score"] < threshold:
|
| 202 |
+
continue
|
| 203 |
+
labels.append(prediction["label"])
|
| 204 |
+
scores.append(prediction["score"])
|
| 205 |
+
boxes.append(np.array(list(prediction["box"].values())))
|
| 206 |
+
if labels == []:
|
| 207 |
+
print("No predictions found with score above threshold.")
|
| 208 |
+
return None
|
| 209 |
+
predictions = {"labels": labels, "scores": scores, "boxes": boxes}
|
| 210 |
+
return predictions
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
@app.function(
|
| 214 |
+
image=image,
|
| 215 |
+
gpu="A100",
|
| 216 |
volumes={volume_path: volume},
|
| 217 |
timeout=60 * 3,
|
| 218 |
)
|
|
|
|
| 325 |
|
| 326 |
|
| 327 |
@app.function(
|
| 328 |
+
gpu="A10G",
|
| 329 |
image=image,
|
| 330 |
volumes={volume_path: volume},
|
| 331 |
timeout=60 * 3,
|
|
|
|
| 423 |
|
| 424 |
|
| 425 |
@app.function(
|
| 426 |
+
gpu="A10G",
|
| 427 |
image=image,
|
| 428 |
volumes={volume_path: volume},
|
| 429 |
timeout=60 * 3,
|
|
|
|
| 495 |
|
| 496 |
|
| 497 |
@app.function(
|
| 498 |
+
gpu="A10G",
|
| 499 |
image=image,
|
| 500 |
volumes={volume_path: volume},
|
| 501 |
timeout=60 * 3,
|
|
|
|
| 523 |
|
| 524 |
|
| 525 |
@app.function(
|
| 526 |
+
gpu="A10G",
|
| 527 |
image=image,
|
| 528 |
volumes={volume_path: volume},
|
| 529 |
timeout=60 * 3,
|
|
|
|
| 532 |
image_pil: Image.Image,
|
| 533 |
prompts: str,
|
| 534 |
privacy_strength: int = 15,
|
| 535 |
+
threshold: float = 0.2,
|
| 536 |
) -> Image.Image:
|
| 537 |
"""
|
| 538 |
Preserves privacy in an image by applying a mosaic effect to specified objects.
|
|
|
|
| 541 |
if isinstance(prompts, str):
|
| 542 |
prompts = [prompt.strip() for prompt in prompts.split(".")]
|
| 543 |
print(f"Parsed prompts: {prompts}")
|
| 544 |
+
prompt_segment_results = privacy_prompt_segment.remote(
|
| 545 |
image_pil=image_pil,
|
| 546 |
prompts=prompts,
|
| 547 |
+
threshold=threshold,
|
| 548 |
)
|
| 549 |
if not prompt_segment_results:
|
| 550 |
return image_pil
|
src/tools.py
CHANGED
|
@@ -42,6 +42,7 @@ def privacy_preserve_image(
|
|
| 42 |
input_img,
|
| 43 |
input_prompt,
|
| 44 |
privacy_strength: int = 15,
|
|
|
|
| 45 |
) -> np.ndarray | Image.Image | str | Path | None:
|
| 46 |
"""
|
| 47 |
Obscures specified objects in the input image based on a natural language prompt, using a privacy-preserving blur or distortion effect.
|
|
@@ -53,6 +54,7 @@ def privacy_preserve_image(
|
|
| 53 |
input_img: Input image or can be URL string of the image or base64 string. Cannot be None.
|
| 54 |
input_prompt (str): Object to obscure in the image. It can be a single word or multiple words, e.g., "left person face", "license plate".
|
| 55 |
privacy_strength (int): Strength of the privacy preservation effect. Higher values result in stronger blurring. Default is 15.
|
|
|
|
| 56 |
Returns:
|
| 57 |
bytes: Binary image data of the modified image.
|
| 58 |
|
|
@@ -63,12 +65,15 @@ def privacy_preserve_image(
|
|
| 63 |
raise gr.Error("Input image cannot be None or empty.")
|
| 64 |
if not input_prompt or input_prompt.strip() == "":
|
| 65 |
raise gr.Error("Input prompt cannot be None or empty.")
|
|
|
|
|
|
|
| 66 |
|
| 67 |
func = modal.Function.from_name(modal_app_name, "preserve_privacy")
|
| 68 |
output_pil = func.remote(
|
| 69 |
image_pil=input_img,
|
| 70 |
prompts=input_prompt,
|
| 71 |
privacy_strength=privacy_strength,
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
if output_pil is None:
|
|
|
|
| 42 |
input_img,
|
| 43 |
input_prompt,
|
| 44 |
privacy_strength: int = 15,
|
| 45 |
+
threshold: float = 0.2,
|
| 46 |
) -> np.ndarray | Image.Image | str | Path | None:
|
| 47 |
"""
|
| 48 |
Obscures specified objects in the input image based on a natural language prompt, using a privacy-preserving blur or distortion effect.
|
|
|
|
| 54 |
input_img: Input image or can be URL string of the image or base64 string. Cannot be None.
|
| 55 |
input_prompt (str): Object to obscure in the image. It can be a single word or multiple words, e.g., "left person face", "license plate".
|
| 56 |
privacy_strength (int): Strength of the privacy preservation effect. Higher values result in stronger blurring. Default is 15.
|
| 57 |
+
threshold (float): Model threshold for detecting objects. It should be between 0.01 and 0.99. Default is 0.2. for detecting smaller objects, small regions or faces a lower threshold is recommended.
|
| 58 |
Returns:
|
| 59 |
bytes: Binary image data of the modified image.
|
| 60 |
|
|
|
|
| 65 |
raise gr.Error("Input image cannot be None or empty.")
|
| 66 |
if not input_prompt or input_prompt.strip() == "":
|
| 67 |
raise gr.Error("Input prompt cannot be None or empty.")
|
| 68 |
+
if threshold < 0.01 or threshold > 0.99:
|
| 69 |
+
raise gr.Error("Threshold must be between 0.01 and 0.99.")
|
| 70 |
|
| 71 |
func = modal.Function.from_name(modal_app_name, "preserve_privacy")
|
| 72 |
output_pil = func.remote(
|
| 73 |
image_pil=input_img,
|
| 74 |
prompts=input_prompt,
|
| 75 |
privacy_strength=privacy_strength,
|
| 76 |
+
threshold=threshold,
|
| 77 |
)
|
| 78 |
|
| 79 |
if output_pil is None:
|