iammraat commited on
Commit
c3e3ab2
·
verified ·
1 Parent(s): 94c91d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -39
app.py CHANGED
@@ -1,30 +1,14 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- import os
5
- from huggingface_hub import snapshot_download
6
 
7
- # --- KEY FIX: Use the standard PPStructure class ---
8
- # The 'PPStructureV3' class is currently broken/strict in the PyPI release.
9
- # The standard 'PPStructure' class is stable and CAN load V3 weights
10
- # because it reads the architecture from the downloaded inference.yml file.
11
- from paddleocr import PPStructure
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
- # The standard class returns a list of results directly
39
- result = layout_engine(image_np)
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 result:
47
  return viz_image, "No layout detected."
48
 
49
- # --- STEP 3: Visualize ---
50
- for region in result:
51
- # PPStructure V2/Standard output format: dict with 'type', 'bbox', 'img'
52
- # Note: V3 model output via V2 class might label keys slightly differently,
53
- # so we check for both standard sets of keys.
 
 
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) # Text (Green)
65
- if label == 'title': color = (0, 0, 255) # Title (Red)
66
- elif label == 'figure': color = (255, 0, 0) # Figure (Blue)
67
- elif label == 'table': color = (255, 255, 0)# Table (Cyan)
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("Using **PP-DocLayoutV3** weights via the stable engine.")
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():