Monday, June 9, 2025

 The following serves as an illustration to remove duplicates from a continuous stream of aerial images:

import cv2

import imagehash

import numpy as np

from PIL import Image

from collections import deque

from PIL import Image

import imagehash

def perceptual_hash(image_path):

    img = Image.open(image_path)

    return imagehash.phash(img)

class ImageDeduplicator:

    def __init__(self, buffer_size=100):

        """Initialize a ring buffer for tracking image hashes."""

        self.buffer_size = buffer_size

        self.hash_buffer = deque(maxlen=buffer_size)

    def compute_hash(self, image):

        """Compute perceptual hash of an image."""

        return imagehash.phash(Image.fromarray(image))

    def is_duplicate(self, image):

        """Check if the image is a duplicate."""

        img_hash = self.compute_hash(image)

        if img_hash in self.hash_buffer:

            return True

        self.hash_buffer.append(img_hash)

        return False

def process_image_stream(image_stream):

    """Process a stream of images and eliminate duplicates."""

    deduplicator = ImageDeduplicator()

    unique_images = []

    for image in image_stream:

        if not deduplicator.is_duplicate(image):

            unique_images.append(image)

    return unique_images

# Example usage

image_paths = ["image1.jpg", "image2.jpg", "image3.jpg"] # Replace with actual image paths

image_stream = [cv2.imread(img) for img in image_paths]

unique_images = process_image_stream(image_stream)

print(f"Unique images count: {len(unique_images)}")

Reference: previous article for context: 

Schema:


No comments:

Post a Comment