zliang commited on
Commit
b296597
1 Parent(s): 0380162

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import gradio as gr
2
  from ultralytics import YOLO
3
- import cv2
4
  import numpy as np
5
  import fitz # PyMuPDF
6
- from PIL import Image
7
  import spaces
 
8
  # Load the trained model
9
- model_path = 'best.pt' # Replace with the path to your trained .pt file
10
  model = YOLO(model_path)
11
 
12
  # Define the class indices for figures and tables
@@ -30,6 +29,7 @@ def crop_images_from_boxes(image, boxes, scale_factor):
30
  for (x1, y1, x2, y2) in boxes
31
  ]
32
  return cropped_images
 
33
  @spaces.GPU
34
  def process_pdf(pdf_file):
35
  # Open the PDF file
@@ -43,11 +43,12 @@ def process_pdf(pdf_file):
43
  # Calculate the scaling factor
44
  scale_factor = high_dpi / low_dpi
45
 
46
- # Pre-cache all page pixmaps at low DPI
47
- low_res_pixmaps = [page.get_pixmap(dpi=low_dpi) for page in doc]
48
-
49
  # Loop through each page
50
- for page_num, low_res_pix in enumerate(low_res_pixmaps):
 
 
 
 
51
  low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
52
 
53
  # Get bounding boxes from low DPI image
@@ -55,7 +56,7 @@ def process_pdf(pdf_file):
55
 
56
  if boxes:
57
  # Load high DPI image for cropping only if boxes are found
58
- high_res_pix = doc[page_num].get_pixmap(dpi=high_dpi)
59
  high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
60
 
61
  # Crop images at high DPI
 
1
  import gradio as gr
2
  from ultralytics import YOLO
 
3
  import numpy as np
4
  import fitz # PyMuPDF
 
5
  import spaces
6
+
7
  # Load the trained model
8
+ model_path = 'runs/detect/train7/weights/best.pt' # Replace with the path to your trained .pt file
9
  model = YOLO(model_path)
10
 
11
  # Define the class indices for figures and tables
 
29
  for (x1, y1, x2, y2) in boxes
30
  ]
31
  return cropped_images
32
+
33
  @spaces.GPU
34
  def process_pdf(pdf_file):
35
  # Open the PDF file
 
43
  # Calculate the scaling factor
44
  scale_factor = high_dpi / low_dpi
45
 
 
 
 
46
  # Loop through each page
47
+ for page_num in range(len(doc)):
48
+ page = doc.load_page(page_num)
49
+
50
+ # Perform inference at low DPI
51
+ low_res_pix = page.get_pixmap(dpi=low_dpi)
52
  low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
53
 
54
  # Get bounding boxes from low DPI image
 
56
 
57
  if boxes:
58
  # Load high DPI image for cropping only if boxes are found
59
+ high_res_pix = page.get_pixmap(dpi=high_dpi)
60
  high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
61
 
62
  # Crop images at high DPI