sign_language_comparison_table_modified / copy_min_max_segments.py
HowardZhangdqs's picture
Add files using upload-large-folder tool
a98fc5a verified
import os
import shutil
import re
from collections import defaultdict
# Source and destination directories
source_dir = "edited_segmented_images"
dest_dir = "min_max_segments"
# Create destination directory if it doesn't exist
os.makedirs(dest_dir, exist_ok=True)
print(f"Created destination directory: {dest_dir}")
# Dictionary to store page -> list of (segment_number, filename) tuples
pages = defaultdict(list)
# Pattern to match the filename format
pattern = re.compile(r"page_(\d+)_segment_(\d+)\.png")
# Scan the source directory
print("Scanning source directory...")
for filename in os.listdir(source_dir):
match = pattern.match(filename)
if match:
page_id = int(match.group(1))
segment_num = int(match.group(2))
pages[page_id].append((segment_num, filename))
# Copy min and max segment files for each page
copy_count = 0
print("\nCopying files...")
for page_id in sorted(pages.keys()):
segments = pages[page_id]
if segments:
# Sort by segment number
segments.sort(key=lambda x: x[0])
# Get min and max segment files
min_segment = segments[0]
max_segment = segments[-1]
# Copy min segment file
src_file = os.path.join(source_dir, min_segment[1])
dst_file = os.path.join(dest_dir, min_segment[1])
shutil.copy2(src_file, dst_file)
print(f"Copied: {min_segment[1]} (page {page_id:04d}, min segment {min_segment[0]})")
copy_count += 1
# Copy max segment file (only if different from min)
if min_segment[0] != max_segment[0]:
src_file = os.path.join(source_dir, max_segment[1])
dst_file = os.path.join(dest_dir, max_segment[1])
shutil.copy2(src_file, dst_file)
print(f"Copied: {max_segment[1]} (page {page_id:04d}, max segment {max_segment[0]})")
copy_count += 1
print(f"\n✅ Completed! Copied {copy_count} files to '{dest_dir}' directory.")
print(f"Total pages processed: {len(pages)}")