Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
from huggingface_hub import snapshot_download
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# --- STEP 1: Download the V3 Model ---
|
| 14 |
-
print("Downloading PP-DocLayoutV3 from Hugging Face...")
|
| 15 |
-
model_path = snapshot_download(repo_id="PaddlePaddle/PP-DocLayoutV3", allow_patterns=["*.pdiparams", "*.pdmodel", "*.yml", "*.json"])
|
| 16 |
-
print(f"Model downloaded to: {model_path}")
|
| 17 |
-
|
| 18 |
-
# --- STEP 2: Initialize ---
|
| 19 |
-
# We use the stable class but point 'layout_model_dir' to your V3 download.
|
| 20 |
-
layout_engine = PPStructure(
|
| 21 |
-
layout_model_dir=model_path, # This argument is valid in the standard class
|
| 22 |
-
use_angle_cls=True,
|
| 23 |
-
enable_mkldnn=False, # Keeps your CPU from crashing
|
| 24 |
-
show_log=False, # Explicitly False to avoid "Unknown Argument" error
|
| 25 |
-
# We disable these extra modules to focus strictly on layout analysis speed
|
| 26 |
-
table=False,
|
| 27 |
-
ocr=False
|
| 28 |
)
|
| 29 |
|
| 30 |
def analyze_layout(input_image):
|
|
@@ -35,36 +19,35 @@ def analyze_layout(input_image):
|
|
| 35 |
|
| 36 |
# Run Inference
|
| 37 |
try:
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
except Exception as e:
|
| 41 |
return image_np, f"Error running layout analysis: {e}"
|
| 42 |
|
| 43 |
viz_image = image_np.copy()
|
| 44 |
detections_text = []
|
| 45 |
|
| 46 |
-
if not
|
| 47 |
return viz_image, "No layout detected."
|
| 48 |
|
| 49 |
-
# ---
|
| 50 |
-
for region in
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
#
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
box = region.get('bbox')
|
| 56 |
-
label = region.get('type') or region.get('label')
|
| 57 |
-
|
| 58 |
if box is None: continue
|
| 59 |
|
| 60 |
try:
|
| 61 |
x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
|
| 62 |
|
| 63 |
# Color coding
|
| 64 |
-
color = (0, 255, 0) #
|
| 65 |
-
if label == 'title': color = (0, 0, 255)
|
| 66 |
-
elif label == 'figure': color = (255, 0, 0) #
|
| 67 |
-
elif label == 'table': color = (255, 255, 0)#
|
| 68 |
|
| 69 |
cv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)
|
| 70 |
cv2.putText(viz_image, str(label), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
|
|
@@ -76,7 +59,7 @@ def analyze_layout(input_image):
|
|
| 76 |
|
| 77 |
with gr.Blocks(title="PP-DocLayoutV3 Explorer") as demo:
|
| 78 |
gr.Markdown("## 📄 PP-DocLayoutV3 Explorer")
|
| 79 |
-
gr.Markdown("
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from paddleocr import PPStructureV3 # Explicitly import the class that exists
|
|
|
|
| 5 |
|
| 6 |
+
# --- INITIALIZATION ---
|
| 7 |
+
# We do NOT pass a custom model path. We let PPStructureV3 download its own default model.
|
| 8 |
+
# This avoids the "ValueError: Unknown argument" crashes.
|
| 9 |
+
layout_engine = PPStructureV3(
|
| 10 |
+
use_doc_orientation_classify=True, # Standard V3 argument for orientation
|
| 11 |
+
enable_mkldnn=False # CRITICAL: Keeps CPU from crashing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
def analyze_layout(input_image):
|
|
|
|
| 19 |
|
| 20 |
# Run Inference
|
| 21 |
try:
|
| 22 |
+
# V3 returns a generator, so we convert to list immediately
|
| 23 |
+
results = list(layout_engine(image_np))
|
| 24 |
except Exception as e:
|
| 25 |
return image_np, f"Error running layout analysis: {e}"
|
| 26 |
|
| 27 |
viz_image = image_np.copy()
|
| 28 |
detections_text = []
|
| 29 |
|
| 30 |
+
if not results:
|
| 31 |
return viz_image, "No layout detected."
|
| 32 |
|
| 33 |
+
# --- VISUALIZATION ---
|
| 34 |
+
for region in results:
|
| 35 |
+
if not isinstance(region, dict): continue
|
| 36 |
+
|
| 37 |
+
# V3 usually puts the box in 'layout_bbox' or 'bbox'
|
| 38 |
+
box = region.get('layout_bbox') or region.get('bbox')
|
| 39 |
+
label = region.get('label', 'unknown')
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
if box is None: continue
|
| 42 |
|
| 43 |
try:
|
| 44 |
x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
|
| 45 |
|
| 46 |
# Color coding
|
| 47 |
+
color = (0, 255, 0) # Green (Default)
|
| 48 |
+
if label == 'title': color = (0, 0, 255) # Red
|
| 49 |
+
elif label == 'figure': color = (255, 0, 0) # Blue
|
| 50 |
+
elif label == 'table': color = (255, 255, 0)# Cyan
|
| 51 |
|
| 52 |
cv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)
|
| 53 |
cv2.putText(viz_image, str(label), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
|
|
|
|
| 59 |
|
| 60 |
with gr.Blocks(title="PP-DocLayoutV3 Explorer") as demo:
|
| 61 |
gr.Markdown("## 📄 PP-DocLayoutV3 Explorer")
|
| 62 |
+
gr.Markdown("Auto-downloading the latest V3 weights for structure analysis.")
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
with gr.Column():
|