mahan_ym
commited on
Commit
·
9c43fab
1
Parent(s):
ac24bf9
fixed up privacy preserve
Browse files- src/modal_app.py +8 -21
- src/tools.py +10 -1
src/modal_app.py
CHANGED
@@ -227,7 +227,7 @@ def change_image_objects_lab(
|
|
227 |
image=image,
|
228 |
volumes={volume_path: volume},
|
229 |
)
|
230 |
-
def apply_mosaic_with_bool_mask(image, mask, intensity: int =
|
231 |
h, w = image.shape[:2]
|
232 |
block_size = max(1, min(intensity, min(h, w)))
|
233 |
|
@@ -246,16 +246,20 @@ def apply_mosaic_with_bool_mask(image, mask, intensity: int = 20):
|
|
246 |
image=image,
|
247 |
volumes={volume_path: volume},
|
248 |
)
|
249 |
-
def
|
250 |
image_pil: Image.Image,
|
251 |
prompt: str,
|
252 |
) -> Image.Image:
|
|
|
|
|
|
|
|
|
253 |
|
254 |
langsam_results = lang_sam_segment.remote(
|
255 |
image_pil=image_pil,
|
256 |
prompt=prompt,
|
257 |
box_threshold=0.35,
|
258 |
-
text_threshold=0.
|
259 |
)
|
260 |
|
261 |
img_array = np.array(image_pil)
|
@@ -280,24 +284,7 @@ def preserve_privacy_test(
|
|
280 |
continue
|
281 |
print(f"Processing mask {i + 1}/{len(result['masks'])}")
|
282 |
print(f"Mask score: {mask_score}")
|
283 |
-
|
284 |
-
cv2.rectangle(
|
285 |
-
img_array,
|
286 |
-
(int(box[0]), int(box[1])),
|
287 |
-
(int(box[2]), int(box[3])),
|
288 |
-
(255, 0, 0), # Blue color in BGR
|
289 |
-
2, # Thickness of the rectangle
|
290 |
-
)
|
291 |
-
label = result["labels"][i]
|
292 |
-
cv2.putText(
|
293 |
-
img_array,
|
294 |
-
label,
|
295 |
-
(int(box[0]), int(box[1] - 10)),
|
296 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
297 |
-
0.5, # Font size
|
298 |
-
(255, 0, 0), # Blue color in BGR
|
299 |
-
2, # Thickness of the text
|
300 |
-
)
|
301 |
mask_bool = mask.astype(bool)
|
302 |
|
303 |
img_array = apply_mosaic_with_bool_mask.remote(img_array, mask_bool)
|
|
|
227 |
image=image,
|
228 |
volumes={volume_path: volume},
|
229 |
)
|
230 |
+
def apply_mosaic_with_bool_mask(image, mask, intensity: int = 50):
|
231 |
h, w = image.shape[:2]
|
232 |
block_size = max(1, min(intensity, min(h, w)))
|
233 |
|
|
|
246 |
image=image,
|
247 |
volumes={volume_path: volume},
|
248 |
)
|
249 |
+
def preserve_privacy(
|
250 |
image_pil: Image.Image,
|
251 |
prompt: str,
|
252 |
) -> Image.Image:
|
253 |
+
os.environ["TORCH_HOME"] = TORCH_HOME
|
254 |
+
os.environ["HF_HOME"] = HF_HOME
|
255 |
+
os.makedirs(HF_HOME, exist_ok=True)
|
256 |
+
os.makedirs(TORCH_HOME, exist_ok=True)
|
257 |
|
258 |
langsam_results = lang_sam_segment.remote(
|
259 |
image_pil=image_pil,
|
260 |
prompt=prompt,
|
261 |
box_threshold=0.35,
|
262 |
+
text_threshold=0.40,
|
263 |
)
|
264 |
|
265 |
img_array = np.array(image_pil)
|
|
|
284 |
continue
|
285 |
print(f"Processing mask {i + 1}/{len(result['masks'])}")
|
286 |
print(f"Mask score: {mask_score}")
|
287 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
mask_bool = mask.astype(bool)
|
289 |
|
290 |
img_array = apply_mosaic_with_bool_mask.remote(img_array, mask_bool)
|
src/tools.py
CHANGED
@@ -26,8 +26,17 @@ def privacy_preserve_image(
|
|
26 |
example:
|
27 |
input_prompt = ["face", "license plate"]
|
28 |
"""
|
|
|
|
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
|
33 |
def change_color_objects_hsv(
|
|
|
26 |
example:
|
27 |
input_prompt = ["face", "license plate"]
|
28 |
"""
|
29 |
+
func = modal.Function.from_name("ImageAlfred", "preserve_privacy")
|
30 |
+
output_pil = func.remote(image_pil=input_img, prompt=input_prompt)
|
31 |
|
32 |
+
if output_pil is None:
|
33 |
+
raise ValueError("Received None from modal remote function.")
|
34 |
+
if not isinstance(output_pil, Image.Image):
|
35 |
+
raise TypeError(
|
36 |
+
f"Expected Image.Image from modal remote function, got {type(output_pil)}"
|
37 |
+
)
|
38 |
+
|
39 |
+
return output_pil
|
40 |
|
41 |
|
42 |
def change_color_objects_hsv(
|