iammraat commited on
Commit
583f78a
·
verified ·
1 Parent(s): 0cf77d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -3,9 +3,6 @@ import cv2
3
  import numpy as np
4
  import os
5
  from huggingface_hub import snapshot_download
6
-
7
- # --- STRICT UPDATE: Use PPStructureV3 directly ---
8
- # Your logs confirmed this class exists in your installed version.
9
  from paddleocr import PPStructureV3
10
 
11
  # --- STEP 1: Download the Model ---
@@ -14,14 +11,16 @@ model_path = snapshot_download(repo_id="PaddlePaddle/PP-DocLayoutV3", allow_patt
14
  print(f"Model downloaded to: {model_path}")
15
 
16
  # --- STEP 2: Initialize V3 Engine ---
17
- # We instantiate PPStructureV3 directly.
18
  layout_engine = PPStructureV3(
19
  layout_model_dir=model_path,
20
  table=False,
21
  ocr=False,
22
- show_log=True,
23
- use_angle_cls=True,
24
- enable_mkldnn=False # Keeps the crash fix while using the new model
 
 
 
25
  )
26
 
27
  def analyze_layout(input_image):
@@ -45,8 +44,9 @@ def analyze_layout(input_image):
45
 
46
  # Iterate through results
47
  for region in result:
48
- # V3 Output format usually includes 'layout_bbox'
49
  if isinstance(region, dict):
 
50
  box = region.get('layout_bbox') or region.get('bbox')
51
  label = region.get('label', 'unknown')
52
  else:
@@ -55,17 +55,20 @@ def analyze_layout(input_image):
55
  if box is None: continue
56
 
57
  # Draw the box
58
- x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
59
-
60
- # Color coding
61
- color = (0, 255, 0)
62
- if label == 'title': color = (0, 0, 255)
63
- elif label == 'figure': color = (255, 0, 0)
64
- elif label == 'table': color = (255, 255, 0)
 
65
 
66
- cv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)
67
- cv2.putText(viz_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
68
- detections_text.append(f"Found {label} at {box}")
 
 
69
 
70
  return viz_image, "\n".join(detections_text)
71
 
 
3
  import numpy as np
4
  import os
5
  from huggingface_hub import snapshot_download
 
 
 
6
  from paddleocr import PPStructureV3
7
 
8
  # --- STEP 1: Download the Model ---
 
11
  print(f"Model downloaded to: {model_path}")
12
 
13
  # --- STEP 2: Initialize V3 Engine ---
 
14
  layout_engine = PPStructureV3(
15
  layout_model_dir=model_path,
16
  table=False,
17
  ocr=False,
18
+ # show_log=True, <-- REMOVED (Caused the crash)
19
+
20
+ # In V3, 'use_angle_cls' is often renamed for documents:
21
+ use_doc_orientation_classify=True,
22
+
23
+ enable_mkldnn=False # Keeps the crash fix
24
  )
25
 
26
  def analyze_layout(input_image):
 
44
 
45
  # Iterate through results
46
  for region in result:
47
+ # V3 Output format usually includes 'layout_bbox' or 'bbox'
48
  if isinstance(region, dict):
49
+ # Try specific v3 keys first, fallback to generic
50
  box = region.get('layout_bbox') or region.get('bbox')
51
  label = region.get('label', 'unknown')
52
  else:
 
55
  if box is None: continue
56
 
57
  # Draw the box
58
+ try:
59
+ x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
60
+
61
+ # Color coding
62
+ color = (0, 255, 0) # Default Green
63
+ if label == 'title': color = (0, 0, 255) # Red
64
+ elif label == 'figure': color = (255, 0, 0) # Blue
65
+ elif label == 'table': color = (255, 255, 0) # Cyan
66
 
67
+ cv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)
68
+ cv2.putText(viz_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
69
+ detections_text.append(f"Found {label} at {box}")
70
+ except Exception:
71
+ pass
72
 
73
  return viz_image, "\n".join(detections_text)
74