Repository navigation
Usage example.
The BestTile module is designed to automatically extract the most informative and detailed tiles from input images. It can be used to prepare high-quality samples for training or analysis by selecting regions with the highest structural complexity.
from pepedp.enum import ProcessType
from pepedp.scripts.utils.best_tile import BestTile
from pepedp.scripts.utils.complexity.laplacian import LaplacianComplexity
# from pepedp.scripts.utils.complexity.ic9600 import IC9600Complexity
bt = BestTile(
in_folder="input_folder", # Path to the folder containing source images
out_folder="output_folder", # Path where selected tiles will be saved
tile_size=512, # Tile size in pixels (square: 512x512)
process_type=ProcessType.THREAD, # Processing type (e.g., multithreading)
scale=1, # Downscale factor used during tile search (1 = no scaling)
dynamic_n_tiles=True, # If True, multiple tiles can be extracted from a single image if it's large enough
threshold=0.0, # Minimum quality score (e.g., Laplacian) to keep the tile
image_gray=True, # Whether to convert input images to grayscale
func=LaplacianComplexity(median_blur=5), # Complexity estimation method (here: Laplacian with median blur)
)
bt.run()The func argument defines the complexity evaluation strategy used to rank and select tiles. Available options include:
-
LaplacianComplexity: evaluates detail richness using a Laplacian filter with optional blurring; -
IC9600Complexity: utilizes a neural network model to detect areas rich in fine-grained details.
These methods help ensure that only the most structurally informative tiles are selected for downstream tasks.
The VideoToFrame utility allows you to extract key frames from a video by measuring visual differences between frames using image embeddings. This method ensures that only significantly different frames are retained, making it useful for dataset curation and redundancy reduction.
from pepedp.embedding.embedding_class import ImgToEmbedding
from pepedp.embedding.enum import EmbeddedModel
from pepedp.scripts.utils.distance import euclid_dist
from pepedp.scripts.utils.video_to_frames import VideoToFrame
vtf = VideoToFrame(
embedder=ImgToEmbedding(
model=EmbeddedModel.ConvNextS, # Embedding model (e.g., ConvNeXt Small)
amp=True, # Enables automatic mixed precision (fp16) if supported
scale=4 # Downscales input image to speed up embedding
),
threshold=0.4, # Distance threshold: only sufficiently different frames are saved
distance_fn=euclid_dist # Distance function between embeddings (Euclidean)
)
vtf("video.mp4", "extracted_video") # Extracts and saves diverse frames from the input videoThe VideoToFrame class converts a video into a sequence of distinct key frames based on feature vector (embedding) differences. The algorithm proceeds as follows:
- The video is read frame by frame.
- Each frame is passed through an image embedder (e.g.,
ConvNeXt) to obtain a feature vector. - The embedding of the current frame is compared to that of the last saved frame using the specified distance function.
- If the distance exceeds the
threshold, the frame is saved.
Defines the image embedding model and pre-processing behavior. Supported models include:
-
ConvNextS,ConvNextL: lightweight convolutional models pretrained on ImageNet; -
DINOv2(VITS,VITB,VITL,VITG): self-supervised transformer models from Facebook Research, suitable for semantic similarity.
Parameters:
-
scale=4: input images are downscaled by a factor of 4 before embedding, reducing memory and compute costs. -
amp=True: activates automatic mixed precision (float16) for faster inference on compatible hardware.
Defines the minimum distance between the embeddings of two frames required to consider them different. A value in the range of 0.3–0.5 works well for models like ConvNext.
Function to compute the distance between two embeddings:
-
euclid_dist(...): standard L2 (Euclidean) distance; -
cosine_dist(...): cosine distance, commonly used with transformer-based models such as DINOv2.
The following example demonstrates how to detect and move duplicate images within a folder based on embedding similarity.
from pepedp.embedding.embedding_class import ImgToEmbedding
from pepedp.embedding.enum import EmbeddedModel
from pepedp.scripts.utils.deduplicate import (
create_embedd,
filtered_pairs,
move_duplicate_files,
)
from pepedp.scripts.utils.distance import euclid_dist
# Generate embeddings for all images in the "input" folder
embedded = create_embedd(
img_folder="input",
embedder=ImgToEmbedding(
model=EmbeddedModel.ConvNextS, # Using ConvNext Small model
amp=True, # Enable mixed precision (float16) for faster processing
scale=4 # Downscale images by a factor of 4 before embedding
),
)
# Find pairs of similar images based on Euclidean distance
paired = filtered_pairs(
embeddings=embedded, # Embeddings dictionary: {filename: embedding}
dist_func=euclid_dist, # Distance metric: Euclidean distance
threshold=1.5 # Distance threshold below which images are considered duplicates
)
# Move identified duplicate files to the "duplicate" folder
move_duplicate_files(
duplicates_dict=paired, # Dictionary with detected duplicate pairs
src_dir="input", # Source directory of images
dst_dir="duplicate", # Destination directory for duplicates
)def create_embedd(img_folder: str, embedder: ImgToEmbedding = ImgToEmbedding()):Generates embeddings for all images in the specified folder.
-
img_folder— path to the folder containing images. -
embedder— image embedding model instance (default isConvNextS). - Returns: a dictionary where keys are filenames and values are embeddings.
def filtered_pairs(embeddings, dist_func=euclid_dist, threshold=1.5, device_str=None):Finds pairs of images whose embeddings are closer than a specified threshold.
-
embeddings— dictionary of embeddings{filename: embedding}. -
dist_func— function to calculate distance between two embeddings (e.g., Euclidean or cosine). -
threshold— numeric threshold; pairs with distance below this are considered duplicates. -
Operation: embeddings are batched and compared pairwise asymmetrically (
ivs. allj > i), excluding duplicates. -
Output: a list of pairs
(i, j, distance)that satisfy the threshold condition.
def move_duplicate_files(duplicates_dict, src_dir="", dst_dir=""):Moves files marked as duplicates from the source to the destination directory.
-
duplicates_dict— dictionary returned byfiltered_pairscontaining:-
names: list of all file names. -
filtered_pairs: list of tuples(i, j, distance)representing duplicate pairs.
-
-
src_dir— source directory containing original images. -
dst_dir— destination directory where duplicates will be moved. -
Uses
shutil.move(...)to perform file operations.
Additional details:
- Files not found in the source directory are logged.
- Files that already exist in the destination folder are skipped.
- The total number of successfully moved files is reported.
This module allows you to automatically filter out low-quality images from a directory using various Image Quality Assessment (IQA) algorithms.
from pepedp.torch_enum import ThresholdAlg
thread_type = ThresholdAlg.HIPERIQA
alg = thread_type.value(
img_dir="input", # Directory containing images to process
batch_size=8, # Number of images processed in a batch
threshold=0.5, # Threshold for quality score (see notes below)
median_threshold=0.5, # Percentage of top-quality images to keep or move
move_folder=None # Destination directory; if None, low-quality images are deleted
)
alg()ThresholdAlg is an enumeration that currently supports the following five algorithms:
- HIPERIQA, ANIIQA, TOPIQ — General-purpose IQA models that return scores in the range 0 (poor) to 1 (excellent).
- BLOCKINESS — Detects JPEG compression artifacts. A higher score indicates worse quality. Thresholding is inverted: images above the threshold are discarded.
- IC9600 — Evaluates the amount of structural detail in an image. Useful for keeping only rich-content samples 0 (poor) to 1 (excellent).
-
img_dir(str) — Path to the directory containing images to be evaluated. -
batch_size=8(int) — Number of images processed simultaneously (batches). -
threshold=0.5(float) — The cutoff value for IQA:- For most models (HIPERIQA, ANIIQA, etc.): images below the threshold are considered low quality.
- For
BLOCKINESS: images above the threshold are discarded due to higher compression artifacts.
-
median_threshold=0.5(float, optional) — If set to a non-zero value, the algorithm:- Sorts images that passed the
thresholdby quality score. - Keeps (or moves) only the top
median_thresholdfraction (e.g.,0.8= top 80%).
- Sorts images that passed the
-
move_folder=None(str or None) — If specified, high-quality images are moved to this directory instead of deleting the low-quality ones. IfNone, images failing the threshold are deleted.