Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,6 @@ from ultralytics import YOLO
|
|
| 3 |
import numpy as np
|
| 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
|
| 11 |
model_path = 'best.pt' # Replace with the path to your trained .pt file
|
|
@@ -33,32 +30,10 @@ def crop_images_from_boxes(image, boxes, scale_factor):
|
|
| 33 |
]
|
| 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
|
| 42 |
-
boxes = infer_image_and_get_boxes(low_res_img)
|
| 43 |
-
|
| 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 |
-
|
| 52 |
-
# Crop images at high DPI
|
| 53 |
-
cropped_imgs = crop_images_from_boxes(high_res_img, boxes, scale_factor)
|
| 54 |
-
|
| 55 |
-
return cropped_imgs
|
| 56 |
-
|
| 57 |
@spaces.GPU
|
| 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
|
|
@@ -71,17 +46,20 @@ def process_pdf(pdf_file):
|
|
| 71 |
# Pre-cache all page pixmaps at low DPI
|
| 72 |
low_res_pixmaps = [page.get_pixmap(dpi=low_dpi) for page in doc]
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# Sequentially process high-res pages to crop images
|
| 82 |
-
for page_num, boxes in low_res_results:
|
| 83 |
if boxes:
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
all_cropped_images.extend(cropped_imgs)
|
| 86 |
|
| 87 |
return all_cropped_images
|
|
|
|
| 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 |
]
|
| 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 |
# 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
|