zliang commited on
Commit
fedf52b
1 Parent(s): e1af4a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -3,6 +3,7 @@ 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 = 'best.pt' # Replace with the path to your trained .pt file
@@ -30,10 +31,31 @@ def crop_images_from_boxes(image, boxes, scale_factor):
30
  ]
31
  return cropped_images
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  @spaces.GPU
34
  def process_pdf(pdf_file):
35
  # Open the PDF file
36
  doc = fitz.open(pdf_file)
 
37
  all_cropped_images = []
38
 
39
  # Set the DPI for inference and high resolution for cropping
@@ -46,21 +68,14 @@ def process_pdf(pdf_file):
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
54
- boxes = infer_image_and_get_boxes(low_res_img)
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
62
- cropped_imgs = crop_images_from_boxes(high_res_img, boxes, scale_factor)
63
- all_cropped_images.extend(cropped_imgs)
64
 
65
  return all_cropped_images
66
 
 
3
  import numpy as np
4
  import fitz # PyMuPDF
5
  import spaces
6
+ from multiprocessing import Pool, cpu_count
7
 
8
  # Load the trained model
9
  model_path = 'best.pt' # Replace with the path to your trained .pt file
 
31
  ]
32
  return cropped_images
33
 
34
+ # Function to process a single page
35
+ def process_page(args):
36
+ page_num, low_res_pix, scale_factor, doc_path = args
37
+ doc = fitz.open(doc_path)
38
+ low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
39
+
40
+ # Get bounding boxes from low DPI image
41
+ boxes = infer_image_and_get_boxes(low_res_img)
42
+
43
+ cropped_imgs = []
44
+ if boxes:
45
+ # Load high DPI image for cropping only if boxes are found
46
+ high_res_pix = doc[page_num].get_pixmap(dpi=high_dpi)
47
+ high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
48
+
49
+ # Crop images at high DPI
50
+ cropped_imgs = crop_images_from_boxes(high_res_img, boxes, scale_factor)
51
+
52
+ return cropped_imgs
53
+
54
  @spaces.GPU
55
  def process_pdf(pdf_file):
56
  # Open the PDF file
57
  doc = fitz.open(pdf_file)
58
+ doc_path = pdf_file.name
59
  all_cropped_images = []
60
 
61
  # Set the DPI for inference and high resolution for cropping
 
68
  # Pre-cache all page pixmaps at low DPI
69
  low_res_pixmaps = [page.get_pixmap(dpi=low_dpi) for page in doc]
70
 
71
+ # Prepare arguments for multiprocessing
72
+ args = [(page_num, low_res_pix, scale_factor, doc_path) for page_num, low_res_pix in enumerate(low_res_pixmaps)]
73
+
74
+ # Process pages concurrently using multiprocessing
75
+ with Pool(cpu_count()) as pool:
76
+ results = pool.map(process_page, args)
77
+ for result in results:
78
+ all_cropped_images.extend(result)
 
 
 
 
 
 
 
79
 
80
  return all_cropped_images
81