-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_concealment_attack.py
59 lines (47 loc) · 2.05 KB
/
object_concealment_attack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import cv2
from ultralytics import YOLO
import supervision as sv
import numpy as np
# --- Model Setup (Download only once) ---
def setup_models():
"""Downloads the YOLO model if not already present."""
if not os.path.exists("yolov8s-world.pt"):
os.system("wget https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s-world.pt")
# Call the setup function to ensure the model is ready
setup_models()
# --- Object Concealment Attack ---
def conceal_object(image_path, target_object, noise_density):
"""
Conceals a target object in an image by adding noise to its bounding box.
Args:
image_path (str): Path to the input image.
target_object (str): Class name of the object to be concealed.
noise_density (float): Percentage of noise to be added (0.0 to 1.0).
Returns:
None: Saves the modified image as "modified_image.png".
"""
# 1. Object Detection (YOLO)
model = YOLO("yolov8s-world.pt")
image = cv2.imread(image_path)
model.set_classes([target_object])
results = model(image)[0]
detections = sv.Detections.from_ultralytics(results)
# 2. Noise Generation and Application
for box in detections.xyxy:
x_min, y_min, x_max, y_max = map(int, box)
bbox_width = x_max - x_min
bbox_height = y_max - y_min
# Generate noise
noise = np.random.randn(bbox_height, bbox_width, 3) * 255 * noise_density
# Add noise to the bounding box region
image[y_min:y_max, x_min:x_max] = image[y_min:y_max, x_min:x_max] + noise.astype(np.uint8)
# Clip pixel values to valid range (0-255)
image = np.clip(image, 0, 255).astype(np.uint8)
cv2.imwrite("modified_image.png", image)
# --- User Input ---
image_path = input("Enter the path to the image: ")
target_object = input("Enter the target object class name (e.g., 'person', 'dog'): ")
noise_density = float(input("Enter the noise density (0.0 to 1.0): "))
conceal_object(image_path, target_object, noise_density)
print("Modified image saved as 'modified_image.png'")