zliang commited on
Commit
07f5bd9
·
verified ·
1 Parent(s): f97fcb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -4,9 +4,14 @@ import numpy as np
4
  import fitz # PyMuPDF
5
  import spaces
6
 
7
- # Load the trained model with FP16 precision
 
 
 
 
8
  model = YOLO("best.pt")
9
 
 
10
  # Define the class indices for figures and tables
11
  figure_class_index = 3 # class index for figures
12
  table_class_index = 4 # class index for tables
@@ -42,18 +47,19 @@ def process_pdf(pdf_file):
42
  # Calculate the scaling factor
43
  scale_factor = high_dpi / low_dpi
44
 
 
 
 
45
  # Loop through each page
46
- for page in doc:
47
- # Get low-resolution pixmap
48
- low_res_pix = page.get_pixmap(dpi=low_dpi)
49
  low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
50
-
51
  # Get bounding boxes from low DPI image
52
  boxes = infer_image_and_get_boxes(low_res_img)
53
 
54
  if boxes:
55
  # Load high DPI image for cropping only if boxes are found
56
- high_res_pix = page.get_pixmap(dpi=high_dpi)
57
  high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
58
 
59
  # Crop images at high DPI
 
4
  import fitz # PyMuPDF
5
  import spaces
6
 
7
+
8
+ # Load the trained model
9
+
10
+
11
+
12
  model = YOLO("best.pt")
13
 
14
+
15
  # Define the class indices for figures and tables
16
  figure_class_index = 3 # class index for figures
17
  table_class_index = 4 # class index for tables
 
47
  # Calculate the scaling factor
48
  scale_factor = high_dpi / low_dpi
49
 
50
+ # Pre-cache all page pixmaps at low DPI
51
+ low_res_pixmaps = [page.get_pixmap(dpi=low_dpi) for page in doc]
52
+
53
  # Loop through each page
54
+ for page_num, low_res_pix in enumerate(low_res_pixmaps):
 
 
55
  low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
56
+
57
  # Get bounding boxes from low DPI image
58
  boxes = infer_image_and_get_boxes(low_res_img)
59
 
60
  if boxes:
61
  # Load high DPI image for cropping only if boxes are found
62
+ high_res_pix = doc[page_num].get_pixmap(dpi=high_dpi)
63
  high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
64
 
65
  # Crop images at high DPI