Upload paddleocr-vl-1.5.py with huggingface_hub
Browse files- paddleocr-vl-1.5.py +15 -25
paddleocr-vl-1.5.py
CHANGED
|
@@ -158,19 +158,17 @@ def smart_resize(
|
|
| 158 |
|
| 159 |
def prepare_image(
|
| 160 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 161 |
-
task_mode: str = "ocr",
|
| 162 |
-
apply_smart_resize: bool = True,
|
| 163 |
) -> Image.Image:
|
| 164 |
"""
|
| 165 |
Prepare image for PaddleOCR-VL-1.5 processing.
|
| 166 |
|
|
|
|
|
|
|
| 167 |
Args:
|
| 168 |
image: PIL Image, dict with bytes, or file path
|
| 169 |
-
task_mode: Task mode (affects max_pixels for spotting)
|
| 170 |
-
apply_smart_resize: Whether to apply smart resize
|
| 171 |
|
| 172 |
Returns:
|
| 173 |
-
Processed PIL Image
|
| 174 |
"""
|
| 175 |
# Convert to PIL Image if needed
|
| 176 |
if isinstance(image, Image.Image):
|
|
@@ -185,17 +183,6 @@ def prepare_image(
|
|
| 185 |
# Convert to RGB
|
| 186 |
pil_img = pil_img.convert("RGB")
|
| 187 |
|
| 188 |
-
# Apply smart resize if requested
|
| 189 |
-
if apply_smart_resize:
|
| 190 |
-
max_pixels = MAX_PIXELS.get(task_mode, MAX_PIXELS["default"])
|
| 191 |
-
original_size = pil_img.size
|
| 192 |
-
new_height, new_width = smart_resize(
|
| 193 |
-
pil_img.height, pil_img.width, max_pixels=max_pixels
|
| 194 |
-
)
|
| 195 |
-
if (new_width, new_height) != (pil_img.width, pil_img.height):
|
| 196 |
-
pil_img = pil_img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 197 |
-
logger.debug(f"Resized image from {original_size} to {pil_img.size}")
|
| 198 |
-
|
| 199 |
return pil_img
|
| 200 |
|
| 201 |
|
|
@@ -425,10 +412,9 @@ def main(
|
|
| 425 |
)
|
| 426 |
|
| 427 |
logger.info(f"Model loaded on {next(model.parameters()).device}")
|
|
|
|
| 428 |
logger.info(f"Processing {len(dataset)} images in batches of {batch_size}")
|
| 429 |
-
|
| 430 |
-
max_pixels = MAX_PIXELS.get(task_mode, MAX_PIXELS["default"])
|
| 431 |
-
logger.info(f"Smart resize enabled (max_pixels={max_pixels:,})")
|
| 432 |
|
| 433 |
# Process images in batches
|
| 434 |
all_outputs = []
|
|
@@ -443,18 +429,16 @@ def main(
|
|
| 443 |
|
| 444 |
try:
|
| 445 |
# Prepare images and create messages
|
| 446 |
-
processed_images = [
|
| 447 |
-
prepare_image(
|
| 448 |
-
img, task_mode=task_mode, apply_smart_resize=apply_smart_resize
|
| 449 |
-
)
|
| 450 |
-
for img in batch_images
|
| 451 |
-
]
|
| 452 |
|
| 453 |
# Create messages for batch
|
| 454 |
batch_messages = [
|
| 455 |
create_message(img, task_mode) for img in processed_images
|
| 456 |
]
|
| 457 |
|
|
|
|
|
|
|
|
|
|
| 458 |
# Process with transformers batch inference
|
| 459 |
inputs = processor.apply_chat_template(
|
| 460 |
batch_messages,
|
|
@@ -463,6 +447,12 @@ def main(
|
|
| 463 |
tokenize=True,
|
| 464 |
return_dict=True,
|
| 465 |
return_tensors="pt",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
).to(model.device)
|
| 467 |
|
| 468 |
# Generate outputs
|
|
|
|
| 158 |
|
| 159 |
def prepare_image(
|
| 160 |
image: Union[Image.Image, Dict[str, Any], str],
|
|
|
|
|
|
|
| 161 |
) -> Image.Image:
|
| 162 |
"""
|
| 163 |
Prepare image for PaddleOCR-VL-1.5 processing.
|
| 164 |
|
| 165 |
+
Note: Image resizing is handled by the processor via images_kwargs.
|
| 166 |
+
|
| 167 |
Args:
|
| 168 |
image: PIL Image, dict with bytes, or file path
|
|
|
|
|
|
|
| 169 |
|
| 170 |
Returns:
|
| 171 |
+
Processed PIL Image in RGB format
|
| 172 |
"""
|
| 173 |
# Convert to PIL Image if needed
|
| 174 |
if isinstance(image, Image.Image):
|
|
|
|
| 183 |
# Convert to RGB
|
| 184 |
pil_img = pil_img.convert("RGB")
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
return pil_img
|
| 187 |
|
| 188 |
|
|
|
|
| 412 |
)
|
| 413 |
|
| 414 |
logger.info(f"Model loaded on {next(model.parameters()).device}")
|
| 415 |
+
max_pixels = MAX_PIXELS.get(task_mode, MAX_PIXELS["default"])
|
| 416 |
logger.info(f"Processing {len(dataset)} images in batches of {batch_size}")
|
| 417 |
+
logger.info(f"Image resizing: max_pixels={max_pixels:,} (handled by processor)")
|
|
|
|
|
|
|
| 418 |
|
| 419 |
# Process images in batches
|
| 420 |
all_outputs = []
|
|
|
|
| 429 |
|
| 430 |
try:
|
| 431 |
# Prepare images and create messages
|
| 432 |
+
processed_images = [prepare_image(img) for img in batch_images]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
# Create messages for batch
|
| 435 |
batch_messages = [
|
| 436 |
create_message(img, task_mode) for img in processed_images
|
| 437 |
]
|
| 438 |
|
| 439 |
+
# Get max_pixels for image processing
|
| 440 |
+
max_pixels = MAX_PIXELS.get(task_mode, MAX_PIXELS["default"])
|
| 441 |
+
|
| 442 |
# Process with transformers batch inference
|
| 443 |
inputs = processor.apply_chat_template(
|
| 444 |
batch_messages,
|
|
|
|
| 447 |
tokenize=True,
|
| 448 |
return_dict=True,
|
| 449 |
return_tensors="pt",
|
| 450 |
+
images_kwargs={
|
| 451 |
+
"size": {
|
| 452 |
+
"shortest_edge": processor.image_processor.min_pixels,
|
| 453 |
+
"longest_edge": max_pixels,
|
| 454 |
+
}
|
| 455 |
+
},
|
| 456 |
).to(model.device)
|
| 457 |
|
| 458 |
# Generate outputs
|