From b32458790bdca14dd885fbc48ef25737f81b3bcd Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 11 May 2026 16:04:35 +0000 Subject: [PATCH 1/3] feat: add AI-driven Hebrew OCR platform Created `ocr_platform.py`, a script that converts PDFs containing handwritten/printed Hebrew text into a Word Document with extracted text. It utilizes `pdf2image`, `pytesseract` (with `heb` model support), and `python-docx`. Also updated `.gitignore` to prevent test artifacts and cache files from being committed. Co-authored-by: veredsil <37815559+veredsil@users.noreply.github.com> --- .gitignore | 4 +++ generate_test_pdf.py | 26 +++++++++++++++++ ocr_platform.py | 69 ++++++++++++++++++++++++++++++++++++++++++++ test_script.py | 7 +++++ 4 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 generate_test_pdf.py create mode 100644 ocr_platform.py create mode 100644 test_script.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..330ead4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +*.pyc +test_hebrew.pdf +test_output.docx diff --git a/generate_test_pdf.py b/generate_test_pdf.py new file mode 100644 index 0000000..dbdb5aa --- /dev/null +++ b/generate_test_pdf.py @@ -0,0 +1,26 @@ +from PIL import Image, ImageDraw, ImageFont +import os + +def create_pdf(output_path): + # Create an image with white background + img = Image.new('RGB', (800, 400), color='white') + d = ImageDraw.Draw(img) + + # Try to load a Hebrew-supporting font if possible, else default + # Note: the default font won't really render Hebrew perfectly but + # it's just for testing the pipeline end-to-end + try: + # Many systems have this font + font = ImageFont.truetype("DejaVuSans.ttf", 40) + except: + font = ImageFont.load_default() + + text = "שלום עולם" # "Hello World" in Hebrew + d.text((50, 150), text, fill=(0, 0, 0), font=font) + + # Save as PDF + img.save(output_path, "PDF", resolution=100.0) + print(f"Test PDF generated at {output_path}") + +if __name__ == "__main__": + create_pdf("test_hebrew.pdf") diff --git a/ocr_platform.py b/ocr_platform.py new file mode 100644 index 0000000..4f5f21b --- /dev/null +++ b/ocr_platform.py @@ -0,0 +1,69 @@ +import argparse +import os +import sys +from pdf2image import convert_from_path +import pytesseract +from docx import Document +from PIL import Image + +def pdf_to_docx(pdf_path, docx_path, lang='heb'): + """ + Converts a PDF containing handwritten/printed text to a DOCX file using OCR. + """ + if not os.path.exists(pdf_path): + print(f"Error: The input file {pdf_path} does not exist.") + sys.exit(1) + + print(f"Loading PDF from {pdf_path}...") + try: + # Convert PDF pages to images + images = convert_from_path(pdf_path) + except Exception as e: + print(f"Error converting PDF to images: {e}") + sys.exit(1) + + print(f"Successfully converted {len(images)} pages to images.") + + # Initialize a new Word document + document = Document() + document.add_heading('OCR Output', 0) + + # Process each image page + for i, image in enumerate(images): + print(f"Processing page {i + 1}/{len(images)}...") + + # Read text from image using pytesseract + try: + # pytesseract takes PIL image directly + text = pytesseract.image_to_string(image, lang=lang) + + if text.strip(): + document.add_paragraph(f"--- Page {i + 1} ---") + document.add_paragraph(text) + else: + document.add_paragraph(f"--- Page {i + 1} ---") + document.add_paragraph("[No text detected]") + + except Exception as e: + print(f"Error processing OCR on page {i + 1}: {e}") + document.add_paragraph(f"--- Page {i + 1} ---") + document.add_paragraph(f"[Error processing page: {e}]") + + # Save the resulting DOCX file + print(f"Saving extracted text to {docx_path}...") + try: + document.save(docx_path) + print("Success!") + except Exception as e: + print(f"Error saving DOCX file: {e}") + sys.exit(1) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Convert PDF with handwritten text in Hebrew to a DOCX file with printed text.") + parser.add_argument("input_pdf", help="Path to the input PDF file.") + parser.add_argument("output_docx", help="Path to the output DOCX file.") + parser.add_argument("--lang", default="heb", help="Language code for OCR (default: 'heb').") + + args = parser.parse_args() + + pdf_to_docx(args.input_pdf, args.output_docx, args.lang) diff --git a/test_script.py b/test_script.py new file mode 100644 index 0000000..4184dd6 --- /dev/null +++ b/test_script.py @@ -0,0 +1,7 @@ +import sys + +def main(): + print("Dependencies installed successfully.") + +if __name__ == "__main__": + main() From 16a1fca4637b0f78f4cb534e14df35b38473ae22 Mon Sep 17 00:00:00 2001 From: veredsil <37815559+veredsil@users.noreply.github.com> Date: Wed, 13 May 2026 10:05:53 +0000 Subject: [PATCH 2/3] chore: clean up directory Remove OCR related scripts from TCGA codebase to migrate them to veredsil/ocr. --- .gitignore | 4 --- generate_test_pdf.py | 26 ----------------- ocr_platform.py | 69 -------------------------------------------- test_script.py | 7 ----- 4 files changed, 106 deletions(-) delete mode 100644 .gitignore delete mode 100644 generate_test_pdf.py delete mode 100644 ocr_platform.py delete mode 100644 test_script.py diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 330ead4..0000000 --- a/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -__pycache__/ -*.pyc -test_hebrew.pdf -test_output.docx diff --git a/generate_test_pdf.py b/generate_test_pdf.py deleted file mode 100644 index dbdb5aa..0000000 --- a/generate_test_pdf.py +++ /dev/null @@ -1,26 +0,0 @@ -from PIL import Image, ImageDraw, ImageFont -import os - -def create_pdf(output_path): - # Create an image with white background - img = Image.new('RGB', (800, 400), color='white') - d = ImageDraw.Draw(img) - - # Try to load a Hebrew-supporting font if possible, else default - # Note: the default font won't really render Hebrew perfectly but - # it's just for testing the pipeline end-to-end - try: - # Many systems have this font - font = ImageFont.truetype("DejaVuSans.ttf", 40) - except: - font = ImageFont.load_default() - - text = "שלום עולם" # "Hello World" in Hebrew - d.text((50, 150), text, fill=(0, 0, 0), font=font) - - # Save as PDF - img.save(output_path, "PDF", resolution=100.0) - print(f"Test PDF generated at {output_path}") - -if __name__ == "__main__": - create_pdf("test_hebrew.pdf") diff --git a/ocr_platform.py b/ocr_platform.py deleted file mode 100644 index 4f5f21b..0000000 --- a/ocr_platform.py +++ /dev/null @@ -1,69 +0,0 @@ -import argparse -import os -import sys -from pdf2image import convert_from_path -import pytesseract -from docx import Document -from PIL import Image - -def pdf_to_docx(pdf_path, docx_path, lang='heb'): - """ - Converts a PDF containing handwritten/printed text to a DOCX file using OCR. - """ - if not os.path.exists(pdf_path): - print(f"Error: The input file {pdf_path} does not exist.") - sys.exit(1) - - print(f"Loading PDF from {pdf_path}...") - try: - # Convert PDF pages to images - images = convert_from_path(pdf_path) - except Exception as e: - print(f"Error converting PDF to images: {e}") - sys.exit(1) - - print(f"Successfully converted {len(images)} pages to images.") - - # Initialize a new Word document - document = Document() - document.add_heading('OCR Output', 0) - - # Process each image page - for i, image in enumerate(images): - print(f"Processing page {i + 1}/{len(images)}...") - - # Read text from image using pytesseract - try: - # pytesseract takes PIL image directly - text = pytesseract.image_to_string(image, lang=lang) - - if text.strip(): - document.add_paragraph(f"--- Page {i + 1} ---") - document.add_paragraph(text) - else: - document.add_paragraph(f"--- Page {i + 1} ---") - document.add_paragraph("[No text detected]") - - except Exception as e: - print(f"Error processing OCR on page {i + 1}: {e}") - document.add_paragraph(f"--- Page {i + 1} ---") - document.add_paragraph(f"[Error processing page: {e}]") - - # Save the resulting DOCX file - print(f"Saving extracted text to {docx_path}...") - try: - document.save(docx_path) - print("Success!") - except Exception as e: - print(f"Error saving DOCX file: {e}") - sys.exit(1) - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Convert PDF with handwritten text in Hebrew to a DOCX file with printed text.") - parser.add_argument("input_pdf", help="Path to the input PDF file.") - parser.add_argument("output_docx", help="Path to the output DOCX file.") - parser.add_argument("--lang", default="heb", help="Language code for OCR (default: 'heb').") - - args = parser.parse_args() - - pdf_to_docx(args.input_pdf, args.output_docx, args.lang) diff --git a/test_script.py b/test_script.py deleted file mode 100644 index 4184dd6..0000000 --- a/test_script.py +++ /dev/null @@ -1,7 +0,0 @@ -import sys - -def main(): - print("Dependencies installed successfully.") - -if __name__ == "__main__": - main() From 84a3fdcd05913177795f26462f5be8d133588cf0 Mon Sep 17 00:00:00 2001 From: veredsil <37815559+veredsil@users.noreply.github.com> Date: Wed, 13 May 2026 10:51:29 +0000 Subject: [PATCH 3/3] feat: add hebrew OCR platform to ocr directory Moved the newly created OCR platform script and tests from `/home/jules/ocr` to `/app/ocr` so the user can access them from their workstation branch. --- ocr/.gitignore | 4 +++ ocr/generate_test_pdf.py | 26 +++++++++++++++ ocr/ocr_platform.py | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 ocr/.gitignore create mode 100644 ocr/generate_test_pdf.py create mode 100644 ocr/ocr_platform.py diff --git a/ocr/.gitignore b/ocr/.gitignore new file mode 100644 index 0000000..330ead4 --- /dev/null +++ b/ocr/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +*.pyc +test_hebrew.pdf +test_output.docx diff --git a/ocr/generate_test_pdf.py b/ocr/generate_test_pdf.py new file mode 100644 index 0000000..dbdb5aa --- /dev/null +++ b/ocr/generate_test_pdf.py @@ -0,0 +1,26 @@ +from PIL import Image, ImageDraw, ImageFont +import os + +def create_pdf(output_path): + # Create an image with white background + img = Image.new('RGB', (800, 400), color='white') + d = ImageDraw.Draw(img) + + # Try to load a Hebrew-supporting font if possible, else default + # Note: the default font won't really render Hebrew perfectly but + # it's just for testing the pipeline end-to-end + try: + # Many systems have this font + font = ImageFont.truetype("DejaVuSans.ttf", 40) + except: + font = ImageFont.load_default() + + text = "שלום עולם" # "Hello World" in Hebrew + d.text((50, 150), text, fill=(0, 0, 0), font=font) + + # Save as PDF + img.save(output_path, "PDF", resolution=100.0) + print(f"Test PDF generated at {output_path}") + +if __name__ == "__main__": + create_pdf("test_hebrew.pdf") diff --git a/ocr/ocr_platform.py b/ocr/ocr_platform.py new file mode 100644 index 0000000..4f5f21b --- /dev/null +++ b/ocr/ocr_platform.py @@ -0,0 +1,69 @@ +import argparse +import os +import sys +from pdf2image import convert_from_path +import pytesseract +from docx import Document +from PIL import Image + +def pdf_to_docx(pdf_path, docx_path, lang='heb'): + """ + Converts a PDF containing handwritten/printed text to a DOCX file using OCR. + """ + if not os.path.exists(pdf_path): + print(f"Error: The input file {pdf_path} does not exist.") + sys.exit(1) + + print(f"Loading PDF from {pdf_path}...") + try: + # Convert PDF pages to images + images = convert_from_path(pdf_path) + except Exception as e: + print(f"Error converting PDF to images: {e}") + sys.exit(1) + + print(f"Successfully converted {len(images)} pages to images.") + + # Initialize a new Word document + document = Document() + document.add_heading('OCR Output', 0) + + # Process each image page + for i, image in enumerate(images): + print(f"Processing page {i + 1}/{len(images)}...") + + # Read text from image using pytesseract + try: + # pytesseract takes PIL image directly + text = pytesseract.image_to_string(image, lang=lang) + + if text.strip(): + document.add_paragraph(f"--- Page {i + 1} ---") + document.add_paragraph(text) + else: + document.add_paragraph(f"--- Page {i + 1} ---") + document.add_paragraph("[No text detected]") + + except Exception as e: + print(f"Error processing OCR on page {i + 1}: {e}") + document.add_paragraph(f"--- Page {i + 1} ---") + document.add_paragraph(f"[Error processing page: {e}]") + + # Save the resulting DOCX file + print(f"Saving extracted text to {docx_path}...") + try: + document.save(docx_path) + print("Success!") + except Exception as e: + print(f"Error saving DOCX file: {e}") + sys.exit(1) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Convert PDF with handwritten text in Hebrew to a DOCX file with printed text.") + parser.add_argument("input_pdf", help="Path to the input PDF file.") + parser.add_argument("output_docx", help="Path to the output DOCX file.") + parser.add_argument("--lang", default="heb", help="Language code for OCR (default: 'heb').") + + args = parser.parse_args() + + pdf_to_docx(args.input_pdf, args.output_docx, args.lang)