Divers : Interro 27
This commit is contained in:
+41
-6
@@ -9,7 +9,7 @@ from tkinter import messagebox
|
||||
from pathlib import Path
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageTk
|
||||
|
||||
print("o to open pdf, O original pdf, e to emacs part, i to interro, click for coordinates")
|
||||
print("o to open pdf, O original pdf, e to emacs part, p to go back, i to interro, click for coordinates")
|
||||
|
||||
# --- Configuration & Globals ---
|
||||
padding = 60
|
||||
@@ -80,7 +80,7 @@ def prepare_image(image_path: str, bounding_boxes, all_labels, nb_pages, last_la
|
||||
if current_index < last_label_index or (last_label_index == -1 and current_index != 0):
|
||||
color = "red"
|
||||
elif current_index > last_label_index + 1:
|
||||
color = "yellow"
|
||||
color = "orange"
|
||||
last_label_index = current_index
|
||||
|
||||
draw.rectangle(((abs_x_min, abs_y_min), (abs_x_max, abs_y_max)), outline=color, width=4)
|
||||
@@ -172,8 +172,15 @@ class ImageViewer:
|
||||
self.active_copie_name = None
|
||||
self.accumulated_results = None # Dict with "name" and "list"
|
||||
|
||||
# To go back
|
||||
self.history = []
|
||||
self.forward_stack = []
|
||||
self.current_pil_image = None
|
||||
|
||||
|
||||
# Bindings
|
||||
self.root.bind('<Return>', self.on_enter)
|
||||
self.root.bind('p', self.on_previous)
|
||||
self.root.bind('e', self.on_edit)
|
||||
self.root.bind('o', self.on_open_pdf)
|
||||
self.root.bind('i', self.on_open_interro)
|
||||
@@ -186,7 +193,11 @@ class ImageViewer:
|
||||
def poll_queue(self):
|
||||
if not self.is_viewing:
|
||||
try:
|
||||
pil_image, json_path, metadata = image_queue.get_nowait()
|
||||
# pil_image, json_path, metadata = image_queue.get_nowait()
|
||||
if self.forward_stack:
|
||||
pil_image, json_path, metadata = self.forward_stack.pop()
|
||||
else:
|
||||
pil_image, json_path, metadata = image_queue.get_nowait()
|
||||
|
||||
# Handle End of Stream
|
||||
if pil_image is None:
|
||||
@@ -201,6 +212,7 @@ class ImageViewer:
|
||||
# Start new batch
|
||||
self.active_copie_name = metadata["copie"]
|
||||
self.accumulated_results = {"name": metadata["name"], "list": []}
|
||||
self.history.clear()
|
||||
|
||||
self.display_image(pil_image, json_path, metadata)
|
||||
except queue.Empty:
|
||||
@@ -216,7 +228,24 @@ class ImageViewer:
|
||||
json.dump(self.accumulated_results, f)
|
||||
self.accumulated_results = None
|
||||
|
||||
|
||||
def on_previous(self, event):
|
||||
if self.is_viewing and self.history:
|
||||
print("Going back to previous image...")
|
||||
prev_pil, prev_json, prev_meta, num_added = self.history.pop()
|
||||
|
||||
# Undo the accumulation to prevent duplicates when we hit Enter again
|
||||
if self.accumulated_results and num_added > 0:
|
||||
self.accumulated_results["list"] = self.accumulated_results["list"][:-num_added]
|
||||
|
||||
# Push current image to the forward stack so we don't lose it
|
||||
self.forward_stack.append((self.current_pil_image,
|
||||
self.current_json_path, self.current_meta))
|
||||
|
||||
# Display the previous image immediately
|
||||
self.display_image(prev_pil, prev_json, prev_meta)
|
||||
def display_image(self, pil_image, json_path, metadata):
|
||||
self.current_pil_image = pil_image # ADD THIS LINE
|
||||
self.orig_size = pil_image.size
|
||||
self.scale_factor = 1.0
|
||||
screen_h = self.root.winfo_screenheight() - 100
|
||||
@@ -235,6 +264,7 @@ class ImageViewer:
|
||||
def on_enter(self, event):
|
||||
if self.is_viewing:
|
||||
print(f"Committing data for {self.current_json_path.name}...")
|
||||
num_added = 0 # ADD THIS LINE
|
||||
|
||||
try:
|
||||
with open(self.current_json_path, 'r') as f:
|
||||
@@ -247,6 +277,8 @@ class ImageViewer:
|
||||
self.current_meta["schema"]
|
||||
)
|
||||
|
||||
num_added = len(converted_items)
|
||||
|
||||
# Add to accumulator
|
||||
if self.accumulated_results:
|
||||
self.accumulated_results["list"].extend(converted_items)
|
||||
@@ -261,6 +293,9 @@ class ImageViewer:
|
||||
messagebox.showerror("JSON Error", msg)
|
||||
return # Abort advancement
|
||||
|
||||
self.history.append((self.current_pil_image, self.current_json_path,
|
||||
self.current_meta, num_added))
|
||||
|
||||
# Advance UI
|
||||
self.is_viewing = False
|
||||
self.label.config(image="", text="Loading next...")
|
||||
@@ -272,13 +307,13 @@ class ImageViewer:
|
||||
print(f"Opening {pdf_path}")
|
||||
subprocess.Popen(['xdg-open', str(pdf_path.absolute())])
|
||||
|
||||
def on_open_ori_pdf(self, event):
|
||||
def on_open_interro(self, event):
|
||||
if self.is_viewing and self.current_json_path:
|
||||
pdf_path = "/home/sebastien/Staging/Interro/" + str(base_dir) + "pdf"
|
||||
pdf_path = "/home/sebastien/Prépa/Staging/Interro/" + str(base_dir) + ".pdf"
|
||||
print(f"Opening {pdf_path}")
|
||||
subprocess.Popen(['xdg-open', pdf_path])
|
||||
|
||||
def on_open_interro(self, event):
|
||||
def on_open_ori_pdf(self, event):
|
||||
if self.is_viewing and self.current_json_path:
|
||||
new_filename = self.current_json_path.stem.split('_')[0] + ".pdf"
|
||||
pdf_path = self.current_json_path.parent / "Copies Originales" / new_filename
|
||||
|
||||
Reference in New Issue
Block a user