Parse all PDF and image files in a folder Args: folder_path: Input folder path output_dir: Output directory config_path: Configuration file path task: Optional task type for single task recognition group_size: Number of files to group together by
(folder_path, output_dir, config_path, task=None, split_pages=False, group_size=None, pred_abandon=False, skip_processed: bool = False, reverse_order: bool = False)
| 18 | } |
| 19 | |
| 20 | def parse_folder(folder_path, output_dir, config_path, task=None, split_pages=False, group_size=None, pred_abandon=False, skip_processed: bool = False, reverse_order: bool = False): |
| 21 | """ |
| 22 | Parse all PDF and image files in a folder |
| 23 | |
| 24 | Args: |
| 25 | folder_path: Input folder path |
| 26 | output_dir: Output directory |
| 27 | config_path: Configuration file path |
| 28 | task: Optional task type for single task recognition |
| 29 | group_size: Number of files to group together by total page count (None means process individually) |
| 30 | skip_processed: If True, skip files whose output folder already exists |
| 31 | """ |
| 32 | print(f"Starting to parse folder: {folder_path}") |
| 33 | total_start_time = time.time() |
| 34 | |
| 35 | # Check if folder exists |
| 36 | if not os.path.exists(folder_path): |
| 37 | raise FileNotFoundError(f"Folder does not exist: {folder_path}") |
| 38 | |
| 39 | if not os.path.isdir(folder_path): |
| 40 | raise ValueError(f"Path is not a directory: {folder_path}") |
| 41 | |
| 42 | # Find all supported files |
| 43 | supported_extensions = {'.pdf', '.jpg', '.jpeg', '.png'} |
| 44 | all_files = [] |
| 45 | |
| 46 | for root, dirs, files in os.walk(folder_path): |
| 47 | for file in files: |
| 48 | file_path = os.path.join(root, file) |
| 49 | file_ext = os.path.splitext(file)[1].lower() |
| 50 | if file_ext in supported_extensions: |
| 51 | all_files.append(file_path) |
| 52 | |
| 53 | all_files.sort(reverse=reverse_order) |
| 54 | |
| 55 | # NEW: skip already processed files by checking expected output folder |
| 56 | skipped_files = [] |
| 57 | if skip_processed: |
| 58 | def _expected_output_dir(file_path: str) -> str: |
| 59 | file_name = '.'.join(os.path.basename(file_path).split(".")[:-1]) |
| 60 | rel_path = os.path.relpath(os.path.dirname(file_path), folder_path) |
| 61 | if rel_path == '.': |
| 62 | return os.path.join(output_dir, file_name) |
| 63 | return os.path.join(output_dir, rel_path, file_name) |
| 64 | |
| 65 | pending_files = [] |
| 66 | for fp in all_files: |
| 67 | out_dir = _expected_output_dir(fp) |
| 68 | if os.path.exists(out_dir): |
| 69 | skipped_files.append(fp) |
| 70 | print(f"Skipping (already exists): {fp} -> {out_dir}") |
| 71 | else: |
| 72 | pending_files.append(fp) |
| 73 | all_files = pending_files |
| 74 | |
| 75 | if not all_files: |
| 76 | print("All files are already processed (or output folders exist). Nothing to do.") |
| 77 | return output_dir |
no test coverage detected