Divers, en lien avec DMI04 (gemini_for_enonce.py, smaller images, link to enonce.pdf)

This commit is contained in:
2026-05-09 16:26:01 +02:00
parent 8d9165d0ac
commit 0836d5809d
9 changed files with 202 additions and 59 deletions
+24 -31
View File
@@ -26,7 +26,8 @@ if os.path.isfile(path_arg) and path_arg.lower().endswith('.pdf'):
files = [os.path.basename(path_arg)]
elif os.path.isdir(path_arg):
INPUT_DIR = path_arg
files = sorted([f for f in os.listdir(INPUT_DIR) if f.lower().endswith('.pdf')])
files = sorted([f for f in os.listdir(INPUT_DIR) if f.lower().endswith('.pdf') and
"nonc" not in f.lower()])
else:
sys.exit("Error: Input must be a directory or a PDF file.")
@@ -83,12 +84,20 @@ def stitch_images(image_list):
return combined
import threading
pdf_cache_lock = threading.Lock()
@lru_cache(maxsize=3)
def get_pdf_pages(filename):
"""Caches the heavy PDF rendering step for the current and next files."""
def _get_pdf_pages_cached(filename):
pdf_path = os.path.join(INPUT_DIR, filename)
return convert_from_path(pdf_path)
def get_pdf_pages(filename):
"""Thread-safe wrapper for the cached PDF conversion."""
with pdf_cache_lock:
return _get_pdf_pages_cached(filename)
def process_single_pdf(filename, shift_offset=0, max_per_file=5):
"""
Converts PDF to stitched images.
@@ -137,7 +146,8 @@ def process_single_pdf(filename, shift_offset=0, max_per_file=5):
# 3. Generate Preview (All stitched together, Resized)
full_stitch = stitch_images(cropped_images)
preview_resized = full_stitch.resize(OUTPUT_SIZE, Image.LANCZOS)
# preview_resized = full_stitch.resize(OUTPUT_SIZE, Image.LANCZOS)
preview_resized = full_stitch.resize(OUTPUT_SIZE, Image.BILINEAR)
schema = {
"original_filename": filename,
@@ -200,8 +210,6 @@ class ImageReviewer:
self.current_preview = None # Only stores the resized preview for GUI
self.is_processing = False
# Queue for pre-fetched results (index, (preview, splits, schema))
self.prefetch_queue = Queue(maxsize=1)
# Queue for manual re-processing results
self.manual_queue = Queue()
@@ -244,19 +252,15 @@ class ImageReviewer:
self.trigger_processing(self.files[self.index], self.current_shift)
def prefetch_worker(self):
"""Background thread to process the NEXT image constantly."""
idx_to_process = 0
"""Background thread to load the NEXT file's PDF pages into RAM."""
idx_to_process = -1
while True:
target = self.index + 1
if target < len(self.files):
if idx_to_process != target:
fname = self.files[target]
result = process_single_pdf(fname, shift_offset=0)
if result:
self.prefetch_queue.put((target, result)) # Blocks if full
idx_to_process = target
time.sleep(0.1)
if target < len(self.files) and target != idx_to_process:
fname = self.files[target]
get_pdf_pages(fname) # Just calling it warms the lru_cache
idx_to_process = target
time.sleep(0.05)
def load_current_image(self, use_prefetch=False):
if self.index >= len(self.files):
@@ -266,21 +270,10 @@ class ImageReviewer:
filename = self.files[self.index]
self.is_processing = False
self.current_shift = 0
result_found = None
if use_prefetch and not self.prefetch_queue.empty():
q_idx, q_result = self.prefetch_queue.queue[0]
if q_idx == self.index:
_, result_found = self.prefetch_queue.get()
self.current_shift = 0
print(f"Loaded {filename} from prefetch.")
if result_found:
self.handle_processing_result(result_found, filename)
else:
# Not in queue (first load or queue mismatch), process manually
self.trigger_processing(filename, self.current_shift)
# Always trigger processing. If prefetched, get_pdf_pages returns instantly.
self.trigger_processing(filename, self.current_shift)
def trigger_processing(self, filename, shift):
"""Starts a thread to process image so GUI doesn't freeze."""