Read this script carefully. I want to modify something. # Install necessary libraries…
- User
Read this script carefully. I want to modify something. # Install necessary libraries !pip install ultralytics opencv-python
Import packages
from ultralytics import YOLO import cv2
def yolo_add_watermark(img_path): # Load YOLOv8 model model = YOLO('yolov8n.pt') # Make sure this is the correct path to your weights
# Detect faces in the image results = model(img_path) img = cv2.imread(img_path) # Get image dimensions img_height, img_width = img.shape[:2] # Define watermark text and its position watermark_text = "Sample Watermark" font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 1 font_thickness = 2 text_size = cv2.getTextSize(watermark_text, font, font_scale, font_thickness)[0] # Default position (bottom-right) watermark_x = img_width - text_size[0] - 10 watermark_y = img_height - 10 # Iterate over detected faces to adjust watermark position if needed for result in results: boxes = result.boxes for box in boxes: bounding_coordinates = box.xyxy x1, y1, x2, y2 = (int(bounding_coordinates[0, 0].item()), int(bounding_coordinates[0, 1].item()), int(bounding_coordinates[0, 2].item()), int(bounding_coordinates[0, 3].item())) # If the watermark overlaps with any face, move it to top-left if (watermark_x < x2 and watermark_x + text_size[0] > x1 and watermark_y - text_size[1] < y2 and watermark_y > y1): watermark_x = 10 watermark_y = 30 break # Adjust once and break out of loop # Add watermark to the image cv2.putText(img, watermark_text, (watermark_x, watermark_y), font, font_scale, (255, 255, 255), font_thickness) # Save the watermarked image output_path = "watermarked_image.jpg" cv2.imwrite(output_path, img) return output_pathRun the function
output_image = yolo_add_watermark('/content/R9nMWkrc.jpeg') # Update with the correct path to your image
- Assistant
To modify the script, let's first understand its purpose: It uses a YOLOv8 model to detect faces in an image and then adds a watermark to the image. If the default watermark location overlaps with a detected face, the watermark is repositioned.
Here are some potential modifications you might consider:
-
Change Watermark Text or Appearance:
- Update the
watermark_textvariable or modifyfont,font_scale,font_thickness, or the color incv2.putText.
- Update the
-
Modify Watermark Positioning Logic:
- Adjust how the position is calculated or where it's repositioned if overlapping with a face.
-
Add Functionality for Dynamic Watermark Position:
- Instead of defaulting to top-left on overlap, make the position configurable.
-
Enable Saving in Different Formats:
- Modify the
output_pathor enhance functionality to save in different formats like PNG.
- Modify the
-
Batch Process Multiple Images:
- Create a loop to process multiple images if desired.
Below is an example of modifying the watermark text and allowing the user to supply an image list for processing:
# Install necessary libraries !pip install ultralytics opencv-python # Import packages from ultralytics import YOLO import cv2 import os def yolo_add_watermark(img_paths, watermark_text="Sample Watermark"): # Load YOLOv8 model model = YOLO('yolov8n.pt') # Make sure this is the correct path to your weights for img_path in img_paths: # Detect faces in the image results = model(img_path) img = cv2.imread(img_path) # Get image dimensions img_height, img_width = img.shape[:2] # Define watermark text and its position font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 1 font_thickness = 2 text_size = cv2.getTextSize(watermark_text, font, font_scale, font_thickness)[0] # Default position (bottom-right) watermark_x = img_width - text_size[0] - 10 watermark_y = img_height - 10 # Iterate over detected faces to adjust watermark position if needed for result in results: boxes = result.boxes for box in boxes: bounding_coordinates = box.xyxy x1, y1, x2, y2 = (int(bounding_coordinates[0, 0].item()), int(bounding_coordinates[0, 1].item()), int(bounding_coordinates[0, 2].item()), int(bounding_coordinates[0, 3].item())) # If the watermark overlaps with any face, move it to top-left if (watermark_x < x2 and watermark_x + text_size[0] > x1 and watermark_y - text_size[1] < y2 and watermark_y > y1): watermark_x = 10 watermark_y = 30 break # Adjust once and break out of loop # Add watermark to the image cv2.putText(img, watermark_text, (watermark_x, watermark_y), font, font_scale, (255, 255, 255), font_thickness) # Save the watermarked image with a new name to avoid overwriting output_path = f"watermarked_{os.path.basename(img_path)}" cv2.imwrite(output_path, img) print(f"Watermarked image saved as {output_path}") # Run the modified function image_paths = ['/content/R9nMWkrc.jpeg'] # Update with the correct path(s) to your images output_images = yolo_add_watermark(image_paths, "Custom Watermark")This example modifies the watermark text and iterates over multiple images provided in
image_paths. You can expand on this by incorporating user input or additional logic as necessary. -
- User
I want to put watermark multiple times in the image but not in face. Opacity should 50% and text size should bigger
- Assistant
To apply the watermark multiple times across the image while avoiding faces, you can modify the script as follows. We also will adjust the opacity of the watermark text by blending it with the original image. Additionally, we'll increase the size of the text as requested.
Here's how you can do it:
# Install necessary libraries !pip install ultralytics opencv-python numpy # Import packages from ultralytics import YOLO import cv2 import numpy as np import os def yolo_add_watermark(img_paths, watermark_text="Sample Watermark"): # Load YOLOv8 model model = YOLO('yolov8n.pt') # Make sure this is the correct path to your weights for img_path in img_paths: # Detect faces in the image results = model(img_path) img = cv2.imread(img_path) # Get image dimensions img_height, img_width = img.shape[:2] # Define watermark text and its appearance font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 2 # Larger size font_thickness = 3 opacity = 0.5 # 50% opacity text_size = cv2.getTextSize(watermark_text, font, font_scale, font_thickness)[0] # Collect face bounding boxes face_boxes = [] for result in results: boxes = result.boxes for box in boxes: bounding_coordinates = box.xyxy x1, y1, x2, y2 = (int(bounding_coordinates[0, 0].item()), int(bounding_coordinates[0, 1].item()), int(bounding_coordinates[0, 2].item()), int(bounding_coordinates[0, 3].item())) face_boxes.append((x1, y1, x2, y2)) # Create a copy of the image for blending overlay = img.copy() # Draw the watermark text at multiple positions for y in range(0, img_height, text_size[1] + 50): for x in range(0, img_width, text_size[0] + 50): # Check if the watermark overlaps with any face overlap = False for (x1, y1, x2, y2) in face_boxes: if (x < x2 and x + text_size[0] > x1 and y < y2 and y + text_size[1] > y1): overlap = True break # If not overlapping, draw the watermark if not overlap: cv2.putText(overlay, watermark_text, (x, y), font, font_scale, (255, 255, 255), font_thickness) # Blend the overlay with the original image cv2.addWeighted(overlay, opacity, img, 1 - opacity, 0, img) # Save the watermarked image with a new name to avoid overwriting output_path = f"watermarked_{os.path.basename(img_path)}" cv2.imwrite(output_path, img) print(f"Watermarked image saved as {output_path}") # Run the modified function image_paths = ['/content/R9nMWkrc.jpeg'] # Update with the correct path(s) to your images output_images = yolo_add_watermark(image_paths, "Custom Watermark")Key Changes:
- Font Size: Increased to make the text larger.
- Opacity: Added an overlay image and used
cv2.addWeightedto blend the watermark with 50% opacity. - Multiple Positions: Iterated over the image in a grid fashion to place watermarks, ensuring they do not overlap with detected faces.
- Face Detection: Stored face bounding boxes to avoid overlapping watermarks with faces.