deb113 commited on
Commit
f86853e
·
verified ·
1 Parent(s): 31608e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -6
app.py CHANGED
@@ -10,7 +10,7 @@ import io
10
  import os
11
  import matplotlib.pyplot as plt
12
  import pandas as pd
13
- from pathlib import Path # Fixed from "pathly" to "pathlib"
14
  import json
15
 
16
  # Create directories if they don't exist
@@ -37,8 +37,19 @@ CLASSES = ["Caption", "Footnote", "Formula", "List-item", "Page-footer", "Page-h
37
  # Define visual elements we want to extract
38
  VISUAL_ELEMENTS = ["Picture", "Caption", "Table", "Formula"]
39
 
40
- # Define colors for visualization
41
- COLORS = sv.ColorPalette.default()
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Set up the annotator
44
  box_annotator = sv.BoxAnnotator(color=COLORS)
@@ -60,7 +71,21 @@ def predict_layout(image):
60
  results = model(img)[0]
61
 
62
  # Format detections
63
- detections = sv.Detections.from_ultralytics(results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  # Get class names
66
  class_ids = detections.class_id
@@ -80,7 +105,6 @@ def predict_layout(image):
80
  class_name = CLASSES[class_id]
81
 
82
  # Include all visual elements (Pictures, Captions, Tables, Formulas)
83
- # You can add or remove classes based on what you consider "visual elements"
84
  if class_name in VISUAL_ELEMENTS:
85
  x1, y1, x2, y2 = map(int, xyxy)
86
  width = x2 - x1
@@ -178,4 +202,5 @@ with gr.Blocks() as demo:
178
  inputs=input_image
179
  )
180
 
181
- demo.launch()
 
 
10
  import os
11
  import matplotlib.pyplot as plt
12
  import pandas as pd
13
+ from pathlib import Path
14
  import json
15
 
16
  # Create directories if they don't exist
 
37
  # Define visual elements we want to extract
38
  VISUAL_ELEMENTS = ["Picture", "Caption", "Table", "Formula"]
39
 
40
+ # Define colors for visualization - Fix for ColorPalette issue
41
+ # Use the sv.ColorPalette directly or create a custom color palette based on supervision version
42
+ try:
43
+ # Try newer versions approach
44
+ COLORS = sv.ColorPalette.default()
45
+ except (AttributeError, TypeError):
46
+ try:
47
+ # Try alternate approach for some versions
48
+ COLORS = sv.ColorPalette.from_hex(["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF",
49
+ "#FFA500", "#800080", "#008000", "#000080", "#808080"])
50
+ except (AttributeError, TypeError):
51
+ # Fallback for older versions or different API
52
+ COLORS = sv.ColorPalette(11) # Create a color palette with 11 colors (one for each class)
53
 
54
  # Set up the annotator
55
  box_annotator = sv.BoxAnnotator(color=COLORS)
 
71
  results = model(img)[0]
72
 
73
  # Format detections
74
+ try:
75
+ # Try with newer supervision versions
76
+ detections = sv.Detections.from_ultralytics(results)
77
+ except (TypeError, AttributeError):
78
+ # Fallback for older versions
79
+ boxes = results.boxes.xyxy.cpu().numpy()
80
+ confidence = results.boxes.conf.cpu().numpy()
81
+ class_ids = results.boxes.cls.cpu().numpy().astype(int)
82
+
83
+ # Create Detections object manually
84
+ detections = sv.Detections(
85
+ xyxy=boxes,
86
+ confidence=confidence,
87
+ class_id=class_ids
88
+ )
89
 
90
  # Get class names
91
  class_ids = detections.class_id
 
105
  class_name = CLASSES[class_id]
106
 
107
  # Include all visual elements (Pictures, Captions, Tables, Formulas)
 
108
  if class_name in VISUAL_ELEMENTS:
109
  x1, y1, x2, y2 = map(int, xyxy)
110
  width = x2 - x1
 
202
  inputs=input_image
203
  )
204
 
205
+ if __name__ == "__main__":
206
+ demo.launch()