support for `--update-score' after manual update of score.json

This commit is contained in:
2026-06-06 10:10:53 +02:00
parent 80d06e4693
commit a80187ba80
3 changed files with 58 additions and 13 deletions
+30 -8
View File
@@ -161,7 +161,8 @@ def has_significant_notes(note_img, threshold=20):
# print(f"Debug : visible pixels is {visible_pixels}")
return visible_pixels > threshold
def apply_actions_and_regenerate(root_dir, data, student_id, actions, notes_layer, all_labels):
def apply_actions_and_regenerate(root_dir, data, student_id, actions, notes_layer,
all_labels, update_score=False):
"""
Modifies data based on actions, reads bnote.json, cuts notes,
regenerates all label images for consistency, saves dirty ones,
@@ -230,6 +231,23 @@ def apply_actions_and_regenerate(root_dir, data, student_id, actions, notes_laye
print(f" > Deleted rect in {label}")
dirty_labels.add(label)
# --- 1.5 Override with existing score.json if requested ---
if update_score and os.path.exists(score_path):
try:
with open(score_path, "r") as f:
existing_scores = json.load(f)
for label, existing_score in existing_scores.items():
if label in labels_data:
current_score = str(labels_data[label]['result'].get('score', 0))
# If manually modified, override the result and mark dirty
if current_score != str(existing_score):
labels_data[label]['result']['score'] = existing_score
dirty_labels.add(label)
print(f" > Overrode score for {label} to {existing_score} from existing score.json")
except json.JSONDecodeError:
print(f" > Warning: Could not read existing {score_path}")
# --- 2. Process Images (Cut notes, Regenerate, Concatenate) ---
concat_list = []
concat_list_F = []
@@ -256,7 +274,7 @@ def apply_actions_and_regenerate(root_dir, data, student_id, actions, notes_laye
# B. Regenerate Label Image
# We always regenerate to ensure Concat.jpg is consistent with any modifications
# pdf_path = Path(root_dir) / "Copies" / f"Copie{student_id}" / f"{label}.pdf"
pdf_path = content.get('pdf_path') # Contient le suffixe _new si nécessaire
pdf_path = content.get('pdf_path') # Contient le suffixe _new si nécessaire
if not os.path.exists(pdf_path): continue
(base_img, _, _) = annotating.make_base_image(pdf_path)
@@ -332,11 +350,13 @@ def apply_actions_and_regenerate(root_dir, data, student_id, actions, notes_laye
from utils import read_all_labels
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python reading_annotations.py <Dir>")
sys.exit(1)
import argparse
parser = argparse.ArgumentParser(description="Read annotations and compile PDFs")
parser.add_argument("input_path", help="Directory path")
parser.add_argument("--update-score", action="store_true", help="Override scores with values from existing score.json")
args = parser.parse_args()
root_dir = sys.argv[1]
root_dir = args.input_path
try:
all_labels = read_all_labels(Path(root_dir))
@@ -352,7 +372,9 @@ if __name__ == "__main__":
if os.path.exists(bnot_dir):
print(f"Processing annotations for: {student_id}")
actions, notes = detect_checks_and_notes(bnot_dir)
if actions or notes:
apply_actions_and_regenerate(root_dir, original_data, student_id, actions, notes, all_labels)
if actions or notes or args.update_score:
apply_actions_and_regenerate(root_dir, original_data, student_id,
actions, notes, all_labels,
update_score=args.update_score)
else:
print(" No changes detected or missing files.")