zliang commited on
Commit
03c8fc6
1 Parent(s): b0e8452

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -31,7 +31,7 @@ def crop_images_from_boxes(image, boxes, scale_factor):
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 = []
@@ -43,24 +43,29 @@ 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
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
 
@@ -75,3 +80,4 @@ iface = gr.Interface(
75
 
76
  # Launch the app
77
  iface.launch()
 
 
31
  return cropped_images
32
 
33
  @spaces.GPU
34
+ def process_pdf(pdf_file, chunk_size=10):
35
  # Open the PDF file
36
  doc = fitz.open(pdf_file)
37
  all_cropped_images = []
 
43
  # Calculate the scaling factor
44
  scale_factor = high_dpi / low_dpi
45
 
46
+ # Process the PDF in chunks
47
+ num_pages = len(doc)
48
+ for chunk_start in range(0, num_pages, chunk_size):
49
+ chunk_end = min(chunk_start + chunk_size, num_pages)
 
 
50
 
51
+ # Pre-cache page pixmaps at low DPI for the current chunk
52
+ low_res_pixmaps = [doc.load_page(page_num).get_pixmap(dpi=low_dpi) for page_num in range(chunk_start, chunk_end)]
53
 
54
+ # Process each page in the current chunk
55
+ for page_num, low_res_pix in enumerate(low_res_pixmaps, start=chunk_start):
56
+ low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
57
+
58
+ # Get bounding boxes from low DPI image
59
+ boxes = infer_image_and_get_boxes(low_res_img)
60
 
61
+ if boxes:
62
+ # Load high DPI image for cropping only if boxes are found
63
+ high_res_pix = doc.load_page(page_num).get_pixmap(dpi=high_dpi)
64
+ high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
65
+
66
+ # Crop images at high DPI
67
+ cropped_imgs = crop_images_from_boxes(high_res_img, boxes, scale_factor)
68
+ all_cropped_images.extend(cropped_imgs)
69
 
70
  return all_cropped_images
71
 
 
80
 
81
  # Launch the app
82
  iface.launch()
83
+