Working state.
This commit is contained in:
+16
-10
@@ -3,7 +3,7 @@ import os
|
||||
import json
|
||||
import numpy as np
|
||||
import shutil
|
||||
from PIL import Image, ImageChops
|
||||
from PIL import Image, ImageChops, ImageFilter
|
||||
Image.MAX_IMAGE_PIXELS = None
|
||||
from pdf2image import convert_from_path
|
||||
import annotating # Reuse rendering logic
|
||||
@@ -36,7 +36,10 @@ def detect_checks_and_notes(output_dir):
|
||||
# Warning: If the PDF is huge, pdf2image might split pages or OOM.
|
||||
# Assuming user didn't change page dimensions/order.
|
||||
try:
|
||||
user_pages = convert_from_path(pdf_path, dpi=DPI)
|
||||
# user_pages = convert_from_path(pdf_path, dpi=DPI)
|
||||
# La version suivante évite les size mismatch
|
||||
# Mais donne plus de bruit
|
||||
user_pages = convert_from_path(pdf_path, dpi=72)
|
||||
except Exception as e:
|
||||
print(f"Error reading PDF: {e}")
|
||||
return [], None
|
||||
@@ -97,19 +100,21 @@ def detect_checks_and_notes(output_dir):
|
||||
# Expand mask slightly to catch sloppy ticks
|
||||
mask_draw.rectangle([x1-5, y1-5, x2+5, y2+5], fill=0)
|
||||
else:
|
||||
# print("A box, not checked !", density)
|
||||
# Even if not "checked", mask the box area slightly to avoid
|
||||
# artifacts if user hovered over it, though arguably we keep it.
|
||||
# Let's strictly mask only if checked to verify detection?
|
||||
# No, prompt says "not extract the part that are just checking".
|
||||
# If user checked it, we mask it.
|
||||
pass
|
||||
mask_draw.rectangle([x1-2, y1-2, x2+2, y2+2], fill=0)
|
||||
|
||||
if box["type"] == "score" and box["value"] == 0.0:
|
||||
# Mask the whole line
|
||||
mask_draw.rectangle([0, y1-5, ref_img.width, y2+5], fill=0)
|
||||
|
||||
# --- Extraction Phase ---
|
||||
# Create the "Manual Notes" layer
|
||||
# Logic: User - Ref. If Diff is dark -> Note.
|
||||
# We want a transparent image with just the pen strokes.
|
||||
|
||||
# Try Gaussian Blur, peut-être inutile.
|
||||
ref_blur = ref_img.filter(ImageFilter.GaussianBlur(5))
|
||||
user_blur = user_img.filter(ImageFilter.GaussianBlur(5))
|
||||
|
||||
# 1. Get difference image
|
||||
diff_img = ImageChops.difference(ref_img, user_img).convert("L")
|
||||
|
||||
@@ -117,7 +122,8 @@ def detect_checks_and_notes(output_dir):
|
||||
# Pixels that are different enough:
|
||||
diff_data = np.array(diff_img)
|
||||
# Create alpha channel: 0 where no diff, 255 where diff
|
||||
alpha = np.where(diff_data > 20, 255, 0).astype(np.uint8)
|
||||
# Higher treshold is better
|
||||
alpha = np.where(diff_data > 100, 255, 0).astype(np.uint8)
|
||||
|
||||
# 3. Create output image (Black strokes, variable alpha)
|
||||
# Or Copy user colors? Better to copy user pixels.
|
||||
|
||||
Reference in New Issue
Block a user