Files

123 lines
4.5 KiB
Python
Raw Permalink Normal View History

2026-10-04 18:24:18 -04:00
"""
Test module for the ColorFeaturesPyramid class.
This module contains unit tests to verify the functionality of the ColorFeaturesPyramid class,
including initialization, down-sampling, and tile analysis.
"""
import os
import sys
import numpy as np
from avdb.db import DBOps, DBConnection
from avdb.color_features_pyramid import ColorFeaturesPyramid
def demo():
"""
Main function to demonstrate the usage of the ColorFeaturesPyramid class.
This function shows how to use the `_down_sample_and_analyze` method to perform
down-sampling and tile analysis on an image.
"""
# Example image path and UUID
img_path = "img/TESTIMG.jpeg"
uuid = "TESTIMG"
# Initialize the ColorFeaturesPyramid with custom parameters
pyramid = ColorFeaturesPyramid(
uuid=uuid,
root_path=".",
down_sampling=9,
colorspace='lab'
)
print("Starting down-sampling and analysis...")
# Run the down-sampling and analysis process
results = list(pyramid._down_sample_and_analyze())
# Print the results for each level
for level, (histograms, metadata) in results:
imgw, imgh, tilesw, tilesh = metadata
print(f"Level {level}:")
print(f" Image dimensions: {imgw} x {imgh}")
print(f" Tiles: {tilesw} x {tilesh}")
print(f" Histograms shape: {histograms.shape}")
print()
print("Down-sampling and analysis completed successfully.")
def analyze_all_images(limit=10, dryrun=True):
"""
Iterate over all images in the 'img/' folder and analyze them using the ColorFeaturesPyramid class.
This function processes each image in the 'img/' folder, initializes the ColorFeaturesPyramid,
and performs down-sampling and tile analysis for each image.
"""
with DBConnection('testdb').get_connection() as db_conn:
db_ops = DBOps(db_conn)
img_folder = "img"
if not os.path.exists(img_folder):
print(f"Error: The folder '{img_folder}' does not exist.")
return
# Get all image files in the folder
image_extensions = ('.jpg', '.jpeg')
image_files = [
f"{f}/{f}.jpeg" for f in os.listdir(img_folder)
]
if not image_files:
print(f"No images found in the folder '{img_folder}'.")
return
print(f"Found {len(image_files)} images in the folder '{img_folder}'.")
for img_file in image_files[:limit]:
img_path = os.path.join(img_folder, img_file)
uuid = os.path.splitext(img_file.split('/')[1])[0] # Use the filename (without extension) as UUID
data_file_exists = os.path.exists(img_path.replace(".jpeg", ".lab.pyr_3.jpeg.tiles.mmap"))
print(img_path.replace(".jpeg", ".lab.pyr_3.jpeg.tiles.mmap"))
print(data_file_exists)
# Check if the analysis files exist using level 7 as proxy
if not data_file_exists:
# img = cv.imread(img_path)
# print(f"\nAnalyzing image: {img_path} - {os.path.basename(img_path)} - {len(image_files)} - {img.shape}")
# if not dryrun:
# img_name = os.path.basename(img_path)
# img_id_out = db_ops.insert_image(
# img_path,
# img_name,
# img.shape[0],
# img.shape[1]
# )
# print(f"Inserted image {i+1} {img_id_out}")
# Initialize the ColorFeaturesPyramid with custom parameters
pyramid = ColorFeaturesPyramid(
uuid=uuid,
root_path=".",
down_sampling=9,
colorspace='lab'
)
# Run the down-sampling and analysis process
results = list(pyramid._down_sample_and_analyze())
# Print the results for each level
for level, (histograms, metadata) in results:
imgw, imgh, tilesw, tilesh = metadata
print(f" Level {level}:")
print(f" Image dimensions: {imgw} x {imgh}")
print(f" Tiles: {tilesw} x {tilesh}")
print(f" Histograms shape: {histograms.shape}\n\n")
else:
print(f"\nSkipping analysis file already exists:\n\t{img_file}\n")
print("\nAnalysis of all images completed successfully.")
if __name__ == "__main__":
# demo()
analyze_all_images(limit=999, dryrun=False)