Spaces:
Sleeping
Sleeping
Download src/data/pre_resize.py from amitaddi/btxrd-workstation: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/amitaddi/btxrd-workstation/resolve/main/src/data/pre_resize.py
- Command line
-
hf download hf://spaces/amitaddi/btxrd-workstation/src/data/pre_resize.py
-
curl -L -o pre_resize.py https://huggingface.co/spaces/amitaddi/btxrd-workstation/resolve/main/src/data/pre_resize.py
2.21 kB
| import os | |
| import glob | |
| import cv2 | |
| from pathlib import Path | |
| from concurrent.futures import ThreadPoolExecutor | |
| from tqdm import tqdm | |
| def resize_image(src_path, dst_path, size=(512, 512), is_mask=False): | |
| try: | |
| img = cv2.imread(src_path, cv2.IMREAD_GRAYSCALE if is_mask else cv2.IMREAD_COLOR) | |
| if img is None: | |
| print(f"Failed to read {src_path}") | |
| return | |
| # For masks, use nearest neighbor interpolation to preserve binary nature | |
| interp = cv2.INTER_NEAREST if is_mask else cv2.INTER_LINEAR | |
| img_resized = cv2.resize(img, size, interpolation=interp) | |
| os.makedirs(os.path.dirname(dst_path), exist_ok=True) | |
| cv2.imwrite(dst_path, img_resized) | |
| except Exception as e: | |
| print(f"Error resizing {src_path}: {e}") | |
| def main(): | |
| project_root = "." | |
| src_img_dir = os.path.join(project_root, 'BTXRD dataset', 'images') | |
| src_mask_dir = os.path.join(project_root, 'BTXRD dataset', 'masks') | |
| dst_img_dir = os.path.join(project_root, 'BTXRD dataset', 'images_512') | |
| dst_mask_dir = os.path.join(project_root, 'BTXRD dataset', 'masks_512') | |
| tasks = [] | |
| for split in ['train', 'val']: | |
| # Images | |
| img_pattern = os.path.join(src_img_dir, split, '*.*') | |
| for img_path in glob.glob(img_pattern): | |
| ext = Path(img_path).suffix.lower() | |
| if ext in ['.jpg', '.jpeg', '.png']: | |
| stem = Path(img_path).name | |
| dst_path = os.path.join(dst_img_dir, split, stem) | |
| tasks.append((img_path, dst_path, False)) | |
| # Masks | |
| mask_pattern = os.path.join(src_mask_dir, split, '*.png') | |
| for mask_path in glob.glob(mask_pattern): | |
| stem = Path(mask_path).name | |
| dst_path = os.path.join(dst_mask_dir, split, stem) | |
| tasks.append((mask_path, dst_path, True)) | |
| print(f"Starting resize of {len(tasks)} items to 512x512...") | |
| with ThreadPoolExecutor(max_workers=8) as executor: | |
| list(tqdm(executor.map(lambda t: resize_image(t[0], t[1], is_mask=t[2]), tasks), total=len(tasks))) | |
| print("Pre-resizing completed!") | |
| if __name__ == '__main__': | |
| main() | |