Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ from ultralytics import YOLO
|
|
| 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
|
| 9 |
model = YOLO(model_path)
|
|
@@ -43,12 +42,11 @@ def process_pdf(pdf_file):
|
|
| 43 |
# Calculate the scaling factor
|
| 44 |
scale_factor = high_dpi / low_dpi
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
# Loop through each page
|
| 47 |
-
for page_num in
|
| 48 |
-
page = doc.load_page(page_num)
|
| 49 |
-
|
| 50 |
-
# Perform inference at low DPI
|
| 51 |
-
low_res_pix = page.get_pixmap(dpi=low_dpi)
|
| 52 |
low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
|
| 53 |
|
| 54 |
# Get bounding boxes from low DPI image
|
|
@@ -56,7 +54,7 @@ def process_pdf(pdf_file):
|
|
| 56 |
|
| 57 |
if boxes:
|
| 58 |
# Load high DPI image for cropping only if boxes are found
|
| 59 |
-
high_res_pix =
|
| 60 |
high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
|
| 61 |
|
| 62 |
# Crop images at high DPI
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
import spaces
|
|
|
|
| 6 |
# Load the trained model
|
| 7 |
model_path = 'best.pt' # Replace with the path to your trained .pt file
|
| 8 |
model = YOLO(model_path)
|
|
|
|
| 42 |
# Calculate the scaling factor
|
| 43 |
scale_factor = high_dpi / low_dpi
|
| 44 |
|
| 45 |
+
# Pre-cache all page pixmaps at low DPI
|
| 46 |
+
low_res_pixmaps = [page.get_pixmap(dpi=low_dpi) for page in doc]
|
| 47 |
+
|
| 48 |
# Loop through each page
|
| 49 |
+
for page_num, low_res_pix in enumerate(low_res_pixmaps):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
|
| 51 |
|
| 52 |
# Get bounding boxes from low DPI image
|
|
|
|
| 54 |
|
| 55 |
if boxes:
|
| 56 |
# Load high DPI image for cropping only if boxes are found
|
| 57 |
+
high_res_pix = doc[page_num].get_pixmap(dpi=high_dpi)
|
| 58 |
high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
|
| 59 |
|
| 60 |
# Crop images at high DPI
|