paddleocr-skills 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +220 -0
- package/bin/paddleocr-skills.js +20 -0
- package/lib/copy.js +39 -0
- package/lib/installer.js +70 -0
- package/lib/prompts.js +67 -0
- package/lib/python.js +75 -0
- package/lib/verify.js +121 -0
- package/package.json +42 -0
- package/templates/.env.example +12 -0
- package/templates/paddleocr-vl/references/paddleocr-vl/layout_schema.md +64 -0
- package/templates/paddleocr-vl/references/paddleocr-vl/output_format.md +154 -0
- package/templates/paddleocr-vl/references/paddleocr-vl/vl_model_spec.md +157 -0
- package/templates/paddleocr-vl/scripts/paddleocr-vl/_lib.py +780 -0
- package/templates/paddleocr-vl/scripts/paddleocr-vl/configure.py +270 -0
- package/templates/paddleocr-vl/scripts/paddleocr-vl/optimize_file.py +226 -0
- package/templates/paddleocr-vl/scripts/paddleocr-vl/requirements-optimize.txt +8 -0
- package/templates/paddleocr-vl/scripts/paddleocr-vl/requirements.txt +7 -0
- package/templates/paddleocr-vl/scripts/paddleocr-vl/smoke_test.py +199 -0
- package/templates/paddleocr-vl/scripts/paddleocr-vl/vl_caller.py +232 -0
- package/templates/paddleocr-vl/skills/paddleocr-vl/SKILL.md +481 -0
- package/templates/ppocrv5/references/ppocrv5/agent_policy.md +258 -0
- package/templates/ppocrv5/references/ppocrv5/normalized_schema.md +257 -0
- package/templates/ppocrv5/references/ppocrv5/provider_api.md +140 -0
- package/templates/ppocrv5/scripts/ppocrv5/_lib.py +635 -0
- package/templates/ppocrv5/scripts/ppocrv5/configure.py +346 -0
- package/templates/ppocrv5/scripts/ppocrv5/ocr_caller.py +684 -0
- package/templates/ppocrv5/scripts/ppocrv5/requirements.txt +4 -0
- package/templates/ppocrv5/scripts/ppocrv5/smoke_test.py +139 -0
- package/templates/ppocrv5/skills/ppocrv5/SKILL.md +272 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
PaddleOCR-VL Configuration Wizard
|
|
4
|
+
|
|
5
|
+
Supports two modes:
|
|
6
|
+
1. Interactive mode (default): python configure.py
|
|
7
|
+
2. CLI mode: python configure.py --api-url URL --token TOKEN
|
|
8
|
+
|
|
9
|
+
Interactive configuration for PaddleOCR-VL API credentials.
|
|
10
|
+
Saves configuration to .env file in project root.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
import os
|
|
15
|
+
import sys
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def save_config(api_url: str, token: str, project_root: Path, quiet: bool = False) -> bool:
|
|
20
|
+
"""
|
|
21
|
+
Save configuration to .env file
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
api_url: VL API URL
|
|
25
|
+
token: VL access token
|
|
26
|
+
project_root: Project root directory
|
|
27
|
+
quiet: If True, suppress output messages
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
True if successful, False otherwise
|
|
31
|
+
"""
|
|
32
|
+
env_file = project_root / ".env"
|
|
33
|
+
|
|
34
|
+
# Read existing .env if it exists
|
|
35
|
+
existing_config = {}
|
|
36
|
+
if env_file.exists():
|
|
37
|
+
if not quiet:
|
|
38
|
+
print(f"Found existing .env file: {env_file}")
|
|
39
|
+
overwrite = input("Overwrite? [Y/n]: ").strip().lower()
|
|
40
|
+
if overwrite == 'n':
|
|
41
|
+
print("Configuration cancelled")
|
|
42
|
+
return False
|
|
43
|
+
|
|
44
|
+
with open(env_file, 'r', encoding='utf-8') as f:
|
|
45
|
+
for line in f:
|
|
46
|
+
line = line.strip()
|
|
47
|
+
if line and not line.startswith('#') and '=' in line:
|
|
48
|
+
key, value = line.split('=', 1)
|
|
49
|
+
existing_config[key.strip()] = value.strip()
|
|
50
|
+
|
|
51
|
+
# Update configuration
|
|
52
|
+
existing_config['VL_API_URL'] = api_url
|
|
53
|
+
existing_config['VL_TOKEN'] = token
|
|
54
|
+
|
|
55
|
+
# Write to .env file
|
|
56
|
+
try:
|
|
57
|
+
with open(env_file, 'w', encoding='utf-8') as f:
|
|
58
|
+
# Write header
|
|
59
|
+
f.write("# PaddleOCR Skills Configuration\n")
|
|
60
|
+
f.write("# Generated by configuration wizard\n")
|
|
61
|
+
f.write("\n")
|
|
62
|
+
|
|
63
|
+
# Group PP-OCRv5 configs
|
|
64
|
+
if any(k.startswith('API_URL') or k.startswith('PADDLE_OCR_TOKEN') or k.startswith('PPOCRV5') for k in existing_config.keys()):
|
|
65
|
+
f.write("# ========================================\n")
|
|
66
|
+
f.write("# PP-OCRv5 Configuration\n")
|
|
67
|
+
f.write("# ========================================\n")
|
|
68
|
+
for key in ['API_URL', 'PADDLE_OCR_TOKEN', 'PPOCRV5_API_URL', 'PPOCRV5_TOKEN']:
|
|
69
|
+
if key in existing_config:
|
|
70
|
+
f.write(f"{key}={existing_config[key]}\n")
|
|
71
|
+
f.write("\n")
|
|
72
|
+
|
|
73
|
+
# Group PaddleOCR-VL configs
|
|
74
|
+
f.write("# ========================================\n")
|
|
75
|
+
f.write("# PaddleOCR-VL Configuration\n")
|
|
76
|
+
f.write("# ========================================\n")
|
|
77
|
+
f.write(f"VL_API_URL={existing_config['VL_API_URL']}\n")
|
|
78
|
+
f.write(f"VL_TOKEN={existing_config['VL_TOKEN']}\n")
|
|
79
|
+
f.write("\n")
|
|
80
|
+
|
|
81
|
+
# Write other configs
|
|
82
|
+
other_keys = [k for k in existing_config.keys()
|
|
83
|
+
if k not in ['API_URL', 'PADDLE_OCR_TOKEN', 'PPOCRV5_API_URL',
|
|
84
|
+
'PPOCRV5_TOKEN', 'VL_API_URL', 'VL_TOKEN']]
|
|
85
|
+
if other_keys:
|
|
86
|
+
f.write("# ========================================\n")
|
|
87
|
+
f.write("# Other Configuration\n")
|
|
88
|
+
f.write("# ========================================\n")
|
|
89
|
+
for key in other_keys:
|
|
90
|
+
f.write(f"{key}={existing_config[key]}\n")
|
|
91
|
+
|
|
92
|
+
if not quiet:
|
|
93
|
+
print(f"✓ Configuration saved to {env_file}")
|
|
94
|
+
return True
|
|
95
|
+
|
|
96
|
+
except Exception as e:
|
|
97
|
+
print(f"✗ Failed to save configuration: {e}")
|
|
98
|
+
return False
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def main():
|
|
102
|
+
# Parse command-line arguments
|
|
103
|
+
parser = argparse.ArgumentParser(
|
|
104
|
+
description='PaddleOCR-VL API Configuration Tool',
|
|
105
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
106
|
+
epilog="""
|
|
107
|
+
Examples:
|
|
108
|
+
# Interactive mode
|
|
109
|
+
python configure.py
|
|
110
|
+
|
|
111
|
+
# CLI mode (non-interactive)
|
|
112
|
+
python configure.py --api-url "https://your-service.com/v1" --token "your_token"
|
|
113
|
+
"""
|
|
114
|
+
)
|
|
115
|
+
parser.add_argument('--api-url', help='VL API URL (non-interactive mode)')
|
|
116
|
+
parser.add_argument('--token', help='VL access token (non-interactive mode)')
|
|
117
|
+
parser.add_argument('--quiet', action='store_true', help='Suppress output messages')
|
|
118
|
+
|
|
119
|
+
args = parser.parse_args()
|
|
120
|
+
|
|
121
|
+
# Find .env file location (project root, 2 levels up from script)
|
|
122
|
+
project_root = Path(__file__).parent.parent.parent
|
|
123
|
+
|
|
124
|
+
# ========================================
|
|
125
|
+
# CLI Mode (non-interactive)
|
|
126
|
+
# ========================================
|
|
127
|
+
if args.api_url and args.token:
|
|
128
|
+
try:
|
|
129
|
+
api_url = args.api_url.strip()
|
|
130
|
+
token = args.token.strip()
|
|
131
|
+
|
|
132
|
+
# Validate URL format
|
|
133
|
+
if not api_url.startswith(("http://", "https://")):
|
|
134
|
+
api_url = f"https://{api_url}"
|
|
135
|
+
|
|
136
|
+
# Validate token
|
|
137
|
+
if len(token) < 16:
|
|
138
|
+
print("Error: Token seems too short. Please check and try again.")
|
|
139
|
+
sys.exit(1)
|
|
140
|
+
|
|
141
|
+
# Save configuration
|
|
142
|
+
if save_config(api_url, token, project_root, quiet=args.quiet):
|
|
143
|
+
if not args.quiet:
|
|
144
|
+
masked_token = token[:8] + "..." + token[-4:] if len(token) > 12 else "***"
|
|
145
|
+
print("\n✓ Configuration complete!")
|
|
146
|
+
print(f" VL_API_URL: {api_url}")
|
|
147
|
+
print(f" VL_TOKEN: {masked_token}")
|
|
148
|
+
sys.exit(0)
|
|
149
|
+
else:
|
|
150
|
+
sys.exit(1)
|
|
151
|
+
|
|
152
|
+
except Exception as e:
|
|
153
|
+
print(f"Error: {e}")
|
|
154
|
+
sys.exit(1)
|
|
155
|
+
|
|
156
|
+
elif args.api_url or args.token:
|
|
157
|
+
print("Error: Both --api-url and --token are required for CLI mode")
|
|
158
|
+
print("Run without arguments for interactive mode")
|
|
159
|
+
sys.exit(1)
|
|
160
|
+
|
|
161
|
+
# ========================================
|
|
162
|
+
# Interactive Mode
|
|
163
|
+
# ========================================
|
|
164
|
+
print("=" * 60)
|
|
165
|
+
print("PaddleOCR-VL Configuration Wizard")
|
|
166
|
+
print("=" * 60)
|
|
167
|
+
print()
|
|
168
|
+
|
|
169
|
+
env_file = project_root / ".env"
|
|
170
|
+
print(f"Configuration will be saved to: {env_file}")
|
|
171
|
+
print()
|
|
172
|
+
|
|
173
|
+
# Read existing .env if it exists
|
|
174
|
+
existing_config = {}
|
|
175
|
+
if env_file.exists():
|
|
176
|
+
print("Found existing .env file, loading current values...")
|
|
177
|
+
with open(env_file, 'r', encoding='utf-8') as f:
|
|
178
|
+
for line in f:
|
|
179
|
+
line = line.strip()
|
|
180
|
+
if line and not line.startswith('#') and '=' in line:
|
|
181
|
+
key, value = line.split('=', 1)
|
|
182
|
+
existing_config[key.strip()] = value.strip()
|
|
183
|
+
print()
|
|
184
|
+
|
|
185
|
+
# Get current values
|
|
186
|
+
current_api_url = existing_config.get('VL_API_URL', '')
|
|
187
|
+
current_token = existing_config.get('VL_TOKEN', '')
|
|
188
|
+
|
|
189
|
+
print("Please provide your PaddleOCR-VL API credentials:")
|
|
190
|
+
print("(Press Enter to keep current value)")
|
|
191
|
+
print()
|
|
192
|
+
|
|
193
|
+
# Prompt for API URL
|
|
194
|
+
print("1. VL_API_URL - PaddleOCR-VL API endpoint")
|
|
195
|
+
print(" Example: https://your-service.com/v1")
|
|
196
|
+
if current_api_url:
|
|
197
|
+
print(f" Current: {current_api_url}")
|
|
198
|
+
|
|
199
|
+
api_url_input = input(" Enter VL_API_URL: ").strip()
|
|
200
|
+
new_api_url = api_url_input if api_url_input else current_api_url
|
|
201
|
+
|
|
202
|
+
if not new_api_url:
|
|
203
|
+
print()
|
|
204
|
+
print("ERROR: VL_API_URL is required.")
|
|
205
|
+
print("Please run this wizard again and provide a valid API URL.")
|
|
206
|
+
sys.exit(1)
|
|
207
|
+
|
|
208
|
+
print()
|
|
209
|
+
|
|
210
|
+
# Prompt for Token
|
|
211
|
+
print("2. VL_TOKEN - Your access token")
|
|
212
|
+
if current_token:
|
|
213
|
+
masked_token = current_token[:8] + "..." + current_token[-4:] if len(current_token) > 12 else "***"
|
|
214
|
+
print(f" Current: {masked_token}")
|
|
215
|
+
|
|
216
|
+
token_input = input(" Enter VL_TOKEN: ").strip()
|
|
217
|
+
new_token = token_input if token_input else current_token
|
|
218
|
+
|
|
219
|
+
if not new_token:
|
|
220
|
+
print()
|
|
221
|
+
print("ERROR: VL_TOKEN is required.")
|
|
222
|
+
print("Please run this wizard again and provide a valid token.")
|
|
223
|
+
sys.exit(1)
|
|
224
|
+
|
|
225
|
+
print()
|
|
226
|
+
|
|
227
|
+
# Save configuration
|
|
228
|
+
print("Saving configuration...")
|
|
229
|
+
|
|
230
|
+
if not save_config(new_api_url, new_token, project_root):
|
|
231
|
+
sys.exit(1)
|
|
232
|
+
|
|
233
|
+
print()
|
|
234
|
+
|
|
235
|
+
# Verify configuration
|
|
236
|
+
print("Verifying configuration...")
|
|
237
|
+
try:
|
|
238
|
+
sys.path.insert(0, str(Path(__file__).parent))
|
|
239
|
+
from _lib import Config
|
|
240
|
+
|
|
241
|
+
Config.load_env()
|
|
242
|
+
|
|
243
|
+
test_url = Config.get_vl_api_url()
|
|
244
|
+
test_token = Config.get_vl_token()
|
|
245
|
+
|
|
246
|
+
print("✓ VL_API_URL loaded successfully")
|
|
247
|
+
print("✓ VL_TOKEN loaded successfully")
|
|
248
|
+
print()
|
|
249
|
+
|
|
250
|
+
except Exception as e:
|
|
251
|
+
print(f"✗ Configuration verification failed: {e}")
|
|
252
|
+
print()
|
|
253
|
+
sys.exit(1)
|
|
254
|
+
|
|
255
|
+
# Next steps
|
|
256
|
+
print("=" * 60)
|
|
257
|
+
print("Configuration Complete!")
|
|
258
|
+
print("=" * 60)
|
|
259
|
+
print()
|
|
260
|
+
print("Next steps:")
|
|
261
|
+
print(" 1. Test the configuration:")
|
|
262
|
+
print(" python scripts/paddleocr-vl/smoke_test.py")
|
|
263
|
+
print()
|
|
264
|
+
print(" 2. Try parsing a document:")
|
|
265
|
+
print(" python scripts/paddleocr-vl/vl_caller.py --file-url \"URL\"")
|
|
266
|
+
print()
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
if __name__ == '__main__':
|
|
270
|
+
main()
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
File Optimizer for PaddleOCR-VL
|
|
5
|
+
|
|
6
|
+
Compresses and optimizes large files to meet size requirements.
|
|
7
|
+
Supports images (PNG, JPG) and PDFs.
|
|
8
|
+
|
|
9
|
+
Usage:
|
|
10
|
+
python scripts/paddleocr-vl/optimize_file.py input.pdf output.pdf --target-size 15
|
|
11
|
+
python scripts/paddleocr-vl/optimize_file.py input.png output.png --quality 85
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import argparse
|
|
15
|
+
import sys
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def optimize_image(input_path: Path, output_path: Path, quality: int = 85, max_size_mb: float = 20):
|
|
20
|
+
"""
|
|
21
|
+
Optimize image file by reducing quality and/or resolution
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
input_path: Input image path
|
|
25
|
+
output_path: Output image path
|
|
26
|
+
quality: JPEG quality (1-100, lower = smaller file)
|
|
27
|
+
max_size_mb: Target max size in MB
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
from PIL import Image
|
|
31
|
+
except ImportError:
|
|
32
|
+
print("ERROR: Pillow not installed")
|
|
33
|
+
print("Install with: pip install Pillow")
|
|
34
|
+
sys.exit(1)
|
|
35
|
+
|
|
36
|
+
print(f"Optimizing image: {input_path}")
|
|
37
|
+
|
|
38
|
+
# Open image
|
|
39
|
+
img = Image.open(input_path)
|
|
40
|
+
original_size = input_path.stat().st_size / 1024 / 1024
|
|
41
|
+
|
|
42
|
+
print(f"Original size: {original_size:.2f}MB")
|
|
43
|
+
print(f"Original dimensions: {img.size[0]}x{img.size[1]}")
|
|
44
|
+
|
|
45
|
+
# Convert RGBA to RGB if needed (for JPEG)
|
|
46
|
+
if img.mode in ('RGBA', 'LA', 'P'):
|
|
47
|
+
# Create white background
|
|
48
|
+
background = Image.new('RGB', img.size, (255, 255, 255))
|
|
49
|
+
if img.mode == 'P':
|
|
50
|
+
img = img.convert('RGBA')
|
|
51
|
+
background.paste(img, mask=img.split()[-1] if img.mode in ('RGBA', 'LA') else None)
|
|
52
|
+
img = background
|
|
53
|
+
|
|
54
|
+
# Determine output format
|
|
55
|
+
output_format = output_path.suffix.lower()
|
|
56
|
+
if output_format in ['.jpg', '.jpeg']:
|
|
57
|
+
save_format = 'JPEG'
|
|
58
|
+
elif output_format == '.png':
|
|
59
|
+
save_format = 'PNG'
|
|
60
|
+
else:
|
|
61
|
+
save_format = 'JPEG'
|
|
62
|
+
output_path = output_path.with_suffix('.jpg')
|
|
63
|
+
|
|
64
|
+
# Try saving with specified quality
|
|
65
|
+
img.save(output_path, format=save_format, quality=quality, optimize=True)
|
|
66
|
+
new_size = output_path.stat().st_size / 1024 / 1024
|
|
67
|
+
|
|
68
|
+
# If still too large, reduce resolution
|
|
69
|
+
scale_factor = 0.9
|
|
70
|
+
while new_size > max_size_mb and scale_factor > 0.3:
|
|
71
|
+
new_width = int(img.size[0] * scale_factor)
|
|
72
|
+
new_height = int(img.size[1] * scale_factor)
|
|
73
|
+
|
|
74
|
+
print(f"Resizing to {new_width}x{new_height} (scale: {scale_factor:.2f})")
|
|
75
|
+
|
|
76
|
+
resized = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
|
77
|
+
resized.save(output_path, format=save_format, quality=quality, optimize=True)
|
|
78
|
+
new_size = output_path.stat().st_size / 1024 / 1024
|
|
79
|
+
|
|
80
|
+
scale_factor -= 0.1
|
|
81
|
+
|
|
82
|
+
print(f"Optimized size: {new_size:.2f}MB")
|
|
83
|
+
print(f"Reduction: {((original_size - new_size) / original_size * 100):.1f}%")
|
|
84
|
+
|
|
85
|
+
if new_size > max_size_mb:
|
|
86
|
+
print(f"\nWARNING: File still larger than {max_size_mb}MB")
|
|
87
|
+
print("Consider:")
|
|
88
|
+
print(" - Lower quality (--quality 70)")
|
|
89
|
+
print(" - Use --file-url instead of local file")
|
|
90
|
+
print(" - Split multi-page documents")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def optimize_pdf(input_path: Path, output_path: Path, max_size_mb: float = 20):
|
|
94
|
+
"""
|
|
95
|
+
Optimize PDF by compressing images within it
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
input_path: Input PDF path
|
|
99
|
+
output_path: Output PDF path
|
|
100
|
+
max_size_mb: Target max size in MB
|
|
101
|
+
"""
|
|
102
|
+
try:
|
|
103
|
+
import fitz # PyMuPDF
|
|
104
|
+
except ImportError:
|
|
105
|
+
print("ERROR: PyMuPDF not installed")
|
|
106
|
+
print("Install with: pip install PyMuPDF")
|
|
107
|
+
sys.exit(1)
|
|
108
|
+
|
|
109
|
+
print(f"Optimizing PDF: {input_path}")
|
|
110
|
+
|
|
111
|
+
original_size = input_path.stat().st_size / 1024 / 1024
|
|
112
|
+
print(f"Original size: {original_size:.2f}MB")
|
|
113
|
+
|
|
114
|
+
# Open PDF
|
|
115
|
+
doc = fitz.open(input_path)
|
|
116
|
+
print(f"Pages: {len(doc)}")
|
|
117
|
+
|
|
118
|
+
# Create output PDF with compression
|
|
119
|
+
writer = fitz.open()
|
|
120
|
+
|
|
121
|
+
for page_num in range(len(doc)):
|
|
122
|
+
page = doc[page_num]
|
|
123
|
+
|
|
124
|
+
# Get page as pixmap with lower DPI if needed
|
|
125
|
+
dpi = 150 # Lower DPI for smaller file
|
|
126
|
+
mat = fitz.Matrix(dpi / 72, dpi / 72)
|
|
127
|
+
pix = page.get_pixmap(matrix=mat)
|
|
128
|
+
|
|
129
|
+
# Create new page
|
|
130
|
+
new_page = writer.new_page(width=page.rect.width, height=page.rect.height)
|
|
131
|
+
|
|
132
|
+
# Insert image with compression
|
|
133
|
+
new_page.insert_image(new_page.rect, pixmap=pix)
|
|
134
|
+
|
|
135
|
+
print(f"Processed page {page_num + 1}/{len(doc)}")
|
|
136
|
+
|
|
137
|
+
# Save with compression
|
|
138
|
+
writer.save(
|
|
139
|
+
output_path,
|
|
140
|
+
garbage=4, # Maximum compression
|
|
141
|
+
deflate=True,
|
|
142
|
+
clean=True
|
|
143
|
+
)
|
|
144
|
+
writer.close()
|
|
145
|
+
doc.close()
|
|
146
|
+
|
|
147
|
+
new_size = output_path.stat().st_size / 1024 / 1024
|
|
148
|
+
print(f"Optimized size: {new_size:.2f}MB")
|
|
149
|
+
print(f"Reduction: {((original_size - new_size) / original_size * 100):.1f}%")
|
|
150
|
+
|
|
151
|
+
if new_size > max_size_mb:
|
|
152
|
+
print(f"\nWARNING: PDF still larger than {max_size_mb}MB")
|
|
153
|
+
print("Consider:")
|
|
154
|
+
print(" - Split into multiple files")
|
|
155
|
+
print(" - Process specific pages only")
|
|
156
|
+
print(" - Use --file-url instead")
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def main():
|
|
160
|
+
parser = argparse.ArgumentParser(
|
|
161
|
+
description='Optimize files for PaddleOCR-VL processing',
|
|
162
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
163
|
+
epilog="""
|
|
164
|
+
Examples:
|
|
165
|
+
# Optimize image with default quality (85)
|
|
166
|
+
python scripts/paddleocr-vl/optimize_file.py input.png output.png
|
|
167
|
+
|
|
168
|
+
# Optimize with specific quality
|
|
169
|
+
python scripts/paddleocr-vl/optimize_file.py input.jpg output.jpg --quality 70
|
|
170
|
+
|
|
171
|
+
# Optimize PDF
|
|
172
|
+
python scripts/paddleocr-vl/optimize_file.py input.pdf output.pdf
|
|
173
|
+
|
|
174
|
+
# Target specific size
|
|
175
|
+
python scripts/paddleocr-vl/optimize_file.py input.pdf output.pdf --target-size 15
|
|
176
|
+
|
|
177
|
+
Supported formats:
|
|
178
|
+
- Images: PNG, JPG, JPEG, BMP, TIFF
|
|
179
|
+
- Documents: PDF
|
|
180
|
+
"""
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
parser.add_argument('input', help='Input file path')
|
|
184
|
+
parser.add_argument('output', help='Output file path')
|
|
185
|
+
parser.add_argument(
|
|
186
|
+
'--quality',
|
|
187
|
+
type=int,
|
|
188
|
+
default=85,
|
|
189
|
+
help='JPEG quality (1-100, default: 85)'
|
|
190
|
+
)
|
|
191
|
+
parser.add_argument(
|
|
192
|
+
'--target-size',
|
|
193
|
+
type=float,
|
|
194
|
+
default=20,
|
|
195
|
+
help='Target maximum size in MB (default: 20)'
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
args = parser.parse_args()
|
|
199
|
+
|
|
200
|
+
input_path = Path(args.input)
|
|
201
|
+
output_path = Path(args.output)
|
|
202
|
+
|
|
203
|
+
# Validate input
|
|
204
|
+
if not input_path.exists():
|
|
205
|
+
print(f"ERROR: Input file not found: {input_path}")
|
|
206
|
+
sys.exit(1)
|
|
207
|
+
|
|
208
|
+
# Determine file type
|
|
209
|
+
ext = input_path.suffix.lower()
|
|
210
|
+
|
|
211
|
+
if ext == '.pdf':
|
|
212
|
+
optimize_pdf(input_path, output_path, args.target_size)
|
|
213
|
+
elif ext in ['.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.tif']:
|
|
214
|
+
optimize_image(input_path, output_path, args.quality, args.target_size)
|
|
215
|
+
else:
|
|
216
|
+
print(f"ERROR: Unsupported file format: {ext}")
|
|
217
|
+
print("Supported: PDF, PNG, JPG, JPEG, BMP, TIFF")
|
|
218
|
+
sys.exit(1)
|
|
219
|
+
|
|
220
|
+
print(f"\nOptimized file saved to: {output_path}")
|
|
221
|
+
print("\nYou can now process with:")
|
|
222
|
+
print(f' python scripts/paddleocr-vl/vl_caller.py --file-path "{output_path}" --pretty')
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
if __name__ == '__main__':
|
|
226
|
+
main()
|