Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,9 @@ import cv2
|
|
5 |
import numpy as np
|
6 |
import fitz # PyMuPDF
|
7 |
from PIL import Image
|
8 |
-
import
|
9 |
-
|
|
|
10 |
model_path = 'best.pt' # Replace with the path to your trained .pt file
|
11 |
model = YOLO(model_path)
|
12 |
|
@@ -43,23 +44,9 @@ def crop_images_from_boxes(image, boxes, scale_factor):
|
|
43 |
cropped_images.append(cropped_image)
|
44 |
return cropped_images
|
45 |
|
46 |
-
|
47 |
-
def
|
48 |
-
|
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,8 +62,31 @@ def process_pdf(pdf_file):
|
|
75 |
|
76 |
# Crop images at high DPI
|
77 |
cropped_imgs = crop_images_from_boxes(high_res_img, boxes, scale_factor)
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
return all_cropped_images
|
81 |
|
82 |
# Create Gradio interface
|
|
|
5 |
import numpy as np
|
6 |
import fitz # PyMuPDF
|
7 |
from PIL import Image
|
8 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
9 |
+
|
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 |
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 |
|
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 |
+
def process_pdf(pdf_file):
|
72 |
+
# Open the PDF file
|
73 |
+
doc = fitz.open(pdf_file)
|
74 |
+
all_cropped_images = []
|
75 |
+
|
76 |
+
# Set the DPI for inference and high resolution for cropping
|
77 |
+
low_dpi = 50
|
78 |
+
high_dpi = 300
|
79 |
|
80 |
+
# Calculate the scaling factor
|
81 |
+
scale_factor = high_dpi / low_dpi
|
82 |
+
|
83 |
+
# Use ThreadPoolExecutor for batch processing
|
84 |
+
with ThreadPoolExecutor() as executor:
|
85 |
+
futures = [executor.submit(process_single_page, doc.load_page(page_num), low_dpi, high_dpi, scale_factor) for page_num in range(len(doc))]
|
86 |
+
|
87 |
+
for future in as_completed(futures):
|
88 |
+
all_cropped_images.extend(future.result())
|
89 |
+
|
90 |
return all_cropped_images
|
91 |
|
92 |
# Create Gradio interface
|