zliang commited on
Commit
0940e5a
·
verified ·
1 Parent(s): 649e38b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -4,6 +4,7 @@ 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
@@ -33,7 +34,8 @@ def crop_images_from_boxes(image, boxes, scale_factor):
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
@@ -42,7 +44,8 @@ def process_low_res_page(page_num, low_res_pix, scale_factor, doc):
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
 
@@ -55,6 +58,7 @@ def process_high_res_page(page_num, boxes, scale_factor, doc):
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
@@ -68,16 +72,16 @@ def process_pdf(pdf_file):
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
 
4
  import fitz # PyMuPDF
5
  import spaces
6
  from concurrent.futures import ThreadPoolExecutor
7
+ from multiprocessing import cpu_count
8
  import cv2
9
 
10
  # Load the trained model
 
34
  return cropped_images
35
 
36
  # Function to process a single page's low-resolution image and perform inference
37
+ def process_low_res_page(page_num, low_res_pix, scale_factor, doc_path):
38
+ doc = fitz.open(doc_path)
39
  low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
40
 
41
  # Get bounding boxes from low DPI image
 
44
  return page_num, boxes
45
 
46
  # Function to process a single page's high-resolution image for cropping
47
+ def process_high_res_page(page_num, boxes, scale_factor, doc_path):
48
+ doc = fitz.open(doc_path)
49
  high_res_pix = doc[page_num].get_pixmap(dpi=high_dpi)
50
  high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
51
 
 
58
  def process_pdf(pdf_file):
59
  # Open the PDF file
60
  doc = fitz.open(pdf_file)
61
+ doc_path = pdf_file.name
62
  all_cropped_images = []
63
 
64
  # Set the DPI for inference and high resolution for cropping
 
72
  low_res_pixmaps = [page.get_pixmap(dpi=low_dpi) for page in doc]
73
 
74
  # Prepare arguments for threading
75
+ args_low_res = [(page_num, low_res_pix, scale_factor, doc_path) for page_num, low_res_pix in enumerate(low_res_pixmaps)]
76
 
77
  # Process low-res pages concurrently using threading to get bounding boxes
78
  with ThreadPoolExecutor(max_workers=cpu_count()) as executor:
79
+ low_res_results = list(executor.map(lambda args: process_low_res_page(*args), args_low_res))
80
 
81
  # Sequentially process high-res pages to crop images
82
  for page_num, boxes in low_res_results:
83
  if boxes:
84
+ cropped_imgs = process_high_res_page(page_num, boxes, scale_factor, doc_path)
85
  all_cropped_images.extend(cropped_imgs)
86
 
87
  return all_cropped_images