zliang commited on
Commit
649e38b
1 Parent(s): fedf52b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -3,7 +3,8 @@ from ultralytics import YOLO
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,31 +32,29 @@ def crop_images_from_boxes(image, boxes, scale_factor):
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,14 +67,18 @@ def process_pdf(pdf_file):
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
 
 
3
  import numpy as np
4
  import fitz # PyMuPDF
5
  import spaces
6
+ from concurrent.futures import ThreadPoolExecutor
7
+ import cv2
8
 
9
  # Load the trained model
10
  model_path = 'best.pt' # Replace with the path to your trained .pt file
 
32
  ]
33
  return cropped_images
34
 
35
+ # Function to process a single page's low-resolution image and perform inference
36
+ def process_low_res_page(page_num, low_res_pix, scale_factor, doc):
 
 
37
  low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
38
 
39
  # Get bounding boxes from low DPI image
40
  boxes = infer_image_and_get_boxes(low_res_img)
41
 
42
+ return page_num, boxes
43
+
44
+ # Function to process a single page's high-resolution image for cropping
45
+ def process_high_res_page(page_num, boxes, scale_factor, doc):
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
  all_cropped_images = []
59
 
60
  # Set the DPI for inference and high resolution for cropping
 
67
  # Pre-cache all page pixmaps at low DPI
68
  low_res_pixmaps = [page.get_pixmap(dpi=low_dpi) for page in doc]
69
 
70
+ # Prepare arguments for threading
71
+ args_low_res = [(page_num, low_res_pix, scale_factor, doc) for page_num, low_res_pix in enumerate(low_res_pixmaps)]
72
 
73
+ # Process low-res pages concurrently using threading to get bounding boxes
74
+ with ThreadPoolExecutor(max_workers=cpu_count()) as executor:
75
+ low_res_results = list(executor.map(lambda p: process_low_res_page(*p), args_low_res))
76
+
77
+ # Sequentially process high-res pages to crop images
78
+ for page_num, boxes in low_res_results:
79
+ if boxes:
80
+ cropped_imgs = process_high_res_page(page_num, boxes, scale_factor, doc)
81
+ all_cropped_images.extend(cropped_imgs)
82
 
83
  return all_cropped_images
84