Update app.py
Browse files
app.py
CHANGED
|
@@ -61,34 +61,51 @@ def compare_images(img1, img2):
|
|
| 61 |
def highlight_changes(img, mask):
|
| 62 |
overlay = img.copy()
|
| 63 |
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 64 |
-
|
|
|
|
| 65 |
if cv2.contourArea(cnt) > 100: # Filter based on area to reduce false positives
|
| 66 |
x, y, w, h = cv2.boundingRect(cnt)
|
| 67 |
cv2.rectangle(overlay, (x, y), (x + w, y + h), (0, 0, 255), 2) # Red for changes
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# Generate comparison PDF
|
| 71 |
def generate_comparison_pdf(original_pdf, edited_pdf):
|
| 72 |
original_images = convert_pdf_to_images(original_pdf)
|
| 73 |
edited_images = convert_pdf_to_images(edited_pdf)
|
| 74 |
combined_images = []
|
|
|
|
| 75 |
for orig_img, edit_img in zip(original_images, edited_images):
|
| 76 |
aligned_img = align_images(orig_img, edit_img)
|
| 77 |
diff_mask = compare_images(orig_img, aligned_img)
|
| 78 |
-
highlighted_img = highlight_changes(edit_img, diff_mask)
|
|
|
|
| 79 |
# Ensure dimensions match
|
| 80 |
height = min(orig_img.shape[0], highlighted_img.shape[0])
|
| 81 |
orig_img_resized = orig_img[:height]
|
| 82 |
highlighted_img_resized = highlighted_img[:height]
|
| 83 |
combined_images.append(np.hstack((orig_img_resized, highlighted_img_resized)))
|
|
|
|
| 84 |
output_path = "outputs/comparison_result.pdf"
|
| 85 |
pdf = FPDF()
|
|
|
|
|
|
|
| 86 |
for img in combined_images:
|
| 87 |
temp_path = "temp_image.png"
|
| 88 |
cv2.imwrite(temp_path, img)
|
| 89 |
pdf.add_page()
|
| 90 |
pdf.image(temp_path, x=10, y=10, w=190)
|
| 91 |
os.remove(temp_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
pdf.output(output_path)
|
| 93 |
return output_path
|
| 94 |
|
|
@@ -114,6 +131,8 @@ interface = gr.Interface(
|
|
| 114 |
gr.File(label="Upload Edited PDF", file_types=[".pdf"])
|
| 115 |
],
|
| 116 |
outputs=gr.File(label="Download Comparison Report"),
|
|
|
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
|
|
|
| 61 |
def highlight_changes(img, mask):
|
| 62 |
overlay = img.copy()
|
| 63 |
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 64 |
+
summary = [] # To store details of changes
|
| 65 |
+
for idx, cnt in enumerate(contours, start=1):
|
| 66 |
if cv2.contourArea(cnt) > 100: # Filter based on area to reduce false positives
|
| 67 |
x, y, w, h = cv2.boundingRect(cnt)
|
| 68 |
cv2.rectangle(overlay, (x, y), (x + w, y + h), (0, 0, 255), 2) # Red for changes
|
| 69 |
+
# Add change details to the summary
|
| 70 |
+
summary.append(f"Change {idx}: Area at (x={x}, y={y}, width={w}, height={h})")
|
| 71 |
+
return overlay, summary
|
| 72 |
|
| 73 |
+
# Generate comparison PDF with summary
|
| 74 |
def generate_comparison_pdf(original_pdf, edited_pdf):
|
| 75 |
original_images = convert_pdf_to_images(original_pdf)
|
| 76 |
edited_images = convert_pdf_to_images(edited_pdf)
|
| 77 |
combined_images = []
|
| 78 |
+
summary = [] # Summary of all changes
|
| 79 |
for orig_img, edit_img in zip(original_images, edited_images):
|
| 80 |
aligned_img = align_images(orig_img, edit_img)
|
| 81 |
diff_mask = compare_images(orig_img, aligned_img)
|
| 82 |
+
highlighted_img, page_summary = highlight_changes(edit_img, diff_mask)
|
| 83 |
+
summary.extend(page_summary) # Collect all changes
|
| 84 |
# Ensure dimensions match
|
| 85 |
height = min(orig_img.shape[0], highlighted_img.shape[0])
|
| 86 |
orig_img_resized = orig_img[:height]
|
| 87 |
highlighted_img_resized = highlighted_img[:height]
|
| 88 |
combined_images.append(np.hstack((orig_img_resized, highlighted_img_resized)))
|
| 89 |
+
|
| 90 |
output_path = "outputs/comparison_result.pdf"
|
| 91 |
pdf = FPDF()
|
| 92 |
+
|
| 93 |
+
# Add each comparison image to the PDF
|
| 94 |
for img in combined_images:
|
| 95 |
temp_path = "temp_image.png"
|
| 96 |
cv2.imwrite(temp_path, img)
|
| 97 |
pdf.add_page()
|
| 98 |
pdf.image(temp_path, x=10, y=10, w=190)
|
| 99 |
os.remove(temp_path)
|
| 100 |
+
|
| 101 |
+
# Add summary page to the PDF
|
| 102 |
+
pdf.add_page()
|
| 103 |
+
pdf.set_font("Arial", size=12)
|
| 104 |
+
pdf.cell(0, 10, "Summary of Changes", ln=True, align="C")
|
| 105 |
+
pdf.ln(10) # Add a line break
|
| 106 |
+
for idx, change in enumerate(summary, start=1):
|
| 107 |
+
pdf.cell(0, 10, f"{idx}. {change}", ln=True)
|
| 108 |
+
|
| 109 |
pdf.output(output_path)
|
| 110 |
return output_path
|
| 111 |
|
|
|
|
| 131 |
gr.File(label="Upload Edited PDF", file_types=[".pdf"])
|
| 132 |
],
|
| 133 |
outputs=gr.File(label="Download Comparison Report"),
|
| 134 |
+
title="PDF Comparison Tool with Summary",
|
| 135 |
+
description="Upload two PDFs: the original and the edited version. The tool will highlight changes and provide a summary in the output PDF."
|
| 136 |
)
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|