zliang commited on
Commit
3cadd69
·
verified ·
1 Parent(s): ac2d267

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -30
app.py CHANGED
@@ -5,9 +5,8 @@ import cv2
5
  import numpy as np
6
  import fitz # PyMuPDF
7
  from PIL import Image
8
- from concurrent.futures import ThreadPoolExecutor, as_completed
9
  import spaces
10
- # Load the trained model globally
11
  model_path = 'best.pt' # Replace with the path to your trained .pt file
12
  model = YOLO(model_path)
13
 
@@ -44,9 +43,23 @@ def crop_images_from_boxes(image, boxes, scale_factor):
44
  cropped_images.append(cropped_image)
45
  return cropped_images
46
 
47
- # Function to process a single page
48
- def process_single_page(page, low_dpi, high_dpi, scale_factor):
49
- try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # Perform inference at low DPI
51
  low_res_pix = page.get_pixmap(dpi=low_dpi)
52
  low_res_img = Image.frombytes("RGB", [low_res_pix.width, low_res_pix.height], low_res_pix.samples)
@@ -62,32 +75,8 @@ def process_single_page(page, low_dpi, high_dpi, scale_factor):
62
 
63
  # Crop images at high DPI
64
  cropped_imgs = crop_images_from_boxes(high_res_img, boxes, scale_factor)
65
-
66
- return cropped_imgs
67
- except Exception as e:
68
- print(f"Error processing page: {e}")
69
- return []
70
-
71
- @spaces.GPU
72
- def process_pdf(pdf_file):
73
- # Open the PDF file
74
- doc = fitz.open(pdf_file)
75
- all_cropped_images = []
76
-
77
- # Set the DPI for inference and high resolution for cropping
78
- low_dpi = 50
79
- high_dpi = 300
80
-
81
- # Calculate the scaling factor
82
- scale_factor = high_dpi / low_dpi
83
 
84
- # Use ThreadPoolExecutor for batch processing
85
- with ThreadPoolExecutor() as executor:
86
- futures = [executor.submit(process_single_page, doc.load_page(page_num), low_dpi, high_dpi, scale_factor) for page_num in range(len(doc))]
87
-
88
- for future in as_completed(futures):
89
- all_cropped_images.extend(future.result())
90
-
91
  return all_cropped_images
92
 
93
  # Create Gradio interface
@@ -101,3 +90,4 @@ iface = gr.Interface(
101
 
102
  # Launch the app
103
  iface.launch()
 
 
5
  import numpy as np
6
  import fitz # PyMuPDF
7
  from PIL import Image
 
8
  import spaces
9
+ # Load the trained model
10
  model_path = 'best.pt' # Replace with the path to your trained .pt file
11
  model = YOLO(model_path)
12
 
 
43
  cropped_images.append(cropped_image)
44
  return cropped_images
45
 
46
+ @spaces.GPU
47
+ def process_pdf(pdf_file):
48
+ # Open the PDF file
49
+ doc = fitz.open(pdf_file)
50
+ all_cropped_images = []
51
+
52
+ # Set the DPI for inference and high resolution for cropping
53
+ low_dpi = 50
54
+ high_dpi = 300
55
+
56
+ # Calculate the scaling factor
57
+ scale_factor = high_dpi / low_dpi
58
+
59
+ # Loop through each page
60
+ for page_num in range(len(doc)):
61
+ page = doc.load_page(page_num)
62
+
63
  # Perform inference at low DPI
64
  low_res_pix = page.get_pixmap(dpi=low_dpi)
65
  low_res_img = Image.frombytes("RGB", [low_res_pix.width, low_res_pix.height], low_res_pix.samples)
 
75
 
76
  # Crop images at high DPI
77
  cropped_imgs = crop_images_from_boxes(high_res_img, boxes, scale_factor)
78
+ all_cropped_images.extend(cropped_imgs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
 
 
 
 
 
 
 
80
  return all_cropped_images
81
 
82
  # Create Gradio interface
 
90
 
91
  # Launch the app
92
  iface.launch()
93
+