Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
import cv2
|
4 |
-
import spaces
|
5 |
import numpy as np
|
6 |
import fitz # PyMuPDF
|
7 |
from PIL import Image
|
8 |
-
|
9 |
# Load the trained model
|
10 |
model_path = 'best.pt' # Replace with the path to your trained .pt file
|
11 |
model = YOLO(model_path)
|
@@ -31,7 +30,6 @@ def crop_images_from_boxes(image, boxes, scale_factor):
|
|
31 |
for (x1, y1, x2, y2) in boxes
|
32 |
]
|
33 |
return cropped_images
|
34 |
-
|
35 |
@spaces.GPU
|
36 |
def process_pdf(pdf_file):
|
37 |
# Open the PDF file
|
@@ -45,12 +43,11 @@ def process_pdf(pdf_file):
|
|
45 |
# Calculate the scaling factor
|
46 |
scale_factor = high_dpi / low_dpi
|
47 |
|
|
|
|
|
|
|
48 |
# Loop through each page
|
49 |
-
for page_num in
|
50 |
-
page = doc.load_page(page_num)
|
51 |
-
|
52 |
-
# Perform inference at low DPI
|
53 |
-
low_res_pix = page.get_pixmap(dpi=low_dpi)
|
54 |
low_res_img = np.frombuffer(low_res_pix.samples, dtype=np.uint8).reshape(low_res_pix.height, low_res_pix.width, 3)
|
55 |
|
56 |
# Get bounding boxes from low DPI image
|
@@ -58,7 +55,7 @@ def process_pdf(pdf_file):
|
|
58 |
|
59 |
if boxes:
|
60 |
# Load high DPI image for cropping only if boxes are found
|
61 |
-
high_res_pix =
|
62 |
high_res_img = np.frombuffer(high_res_pix.samples, dtype=np.uint8).reshape(high_res_pix.height, high_res_pix.width, 3)
|
63 |
|
64 |
# Crop images at high DPI
|
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
import cv2
|
|
|
4 |
import numpy as np
|
5 |
import fitz # PyMuPDF
|
6 |
from PIL import Image
|
7 |
+
import spaces
|
8 |
# Load the trained model
|
9 |
model_path = 'best.pt' # Replace with the path to your trained .pt file
|
10 |
model = YOLO(model_path)
|
|
|
30 |
for (x1, y1, x2, y2) in boxes
|
31 |
]
|
32 |
return cropped_images
|
|
|
33 |
@spaces.GPU
|
34 |
def process_pdf(pdf_file):
|
35 |
# Open the 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
|
|
|
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
|