Faster annotating : 2 threads

master
Sébastien Miquel 2026-02-25 21:16:12 +01:00
parent f3dc5c452a
commit 375305439a
2 changed files with 70 additions and 56 deletions

View File

@ -385,9 +385,10 @@ def compose_label_image(base_img, label, result, hmin,
def natural_key(text): def natural_key(text):
return [int(c) if c.isdigit() else c.lower() for c in re.split(r'(\d+)', str(text))] return [int(c) if c.isdigit() else c.lower() for c in re.split(r'(\d+)', str(text))]
import concurrent.futures
def process_correction(root_dir, data, all_labels, overwrite=False): def process_student(student_id, labels_data, root_dir, all_labels, overwrite):
for student_id, labels in data.items(): """Helper function to process a single student."""
# Prepare output directory: Dir/Anot_CopieID # Prepare output directory: Dir/Anot_CopieID
output_dir = os.path.join(root_dir, "Anot", f"Copie{student_id}") output_dir = os.path.join(root_dir, "Anot", f"Copie{student_id}")
@ -396,7 +397,7 @@ def process_correction(root_dir, data, all_labels, overwrite=False):
concat_path = os.path.join(output_dir, "Concat.jpg") concat_path = os.path.join(output_dir, "Concat.jpg")
if os.path.exists(concat_path) and not overwrite: if os.path.exists(concat_path) and not overwrite:
print(f"Skipping Copie {student_id} (Concat.jpg exists)") print(f"Skipping Copie {student_id} (Concat.jpg exists)")
continue return
print("Processing :", student_id) print("Processing :", student_id)
@ -408,9 +409,9 @@ def process_correction(root_dir, data, all_labels, overwrite=False):
d_notes = dict.fromkeys(all_labels, "") d_notes = dict.fromkeys(all_labels, "")
label_images = [] label_images = []
labels = sorted(list(labels.items()), key=natural_key) sorted_labels = sorted(list(labels_data.items()), key=natural_key)
for label, content in labels: for label, content in sorted_labels:
# 1. Find PDF path # 1. Find PDF path
copie_folder = f"Copie{student_id}" copie_folder = f"Copie{student_id}"
pdf_rel_path = os.path.join(copie_folder, f"{label}.pdf") pdf_rel_path = os.path.join(copie_folder, f"{label}.pdf")
@ -454,6 +455,19 @@ def process_correction(root_dir, data, all_labels, overwrite=False):
cy += img.height cy += img.height
canvas.save(concat_path) canvas.save(concat_path)
def process_correction(root_dir, data, all_labels, overwrite=False):
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
# Create a list of futures
futures = []
for student_id, labels in sorted(data.items()):
futures.append(
executor.submit(process_student, student_id, labels, root_dir, all_labels, overwrite)
)
# Wait for all threads to complete
concurrent.futures.wait(futures)
import argparse import argparse
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Annotate copies") parser = argparse.ArgumentParser(description="Annotate copies")