### Initialize OCRProcessor with Different Model (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Imports the `OCRProcessor` class and creates a new instance, specifying a different Ollama model (`llama3.2-vision:11b`) to be used for subsequent OCR operations. Requires the `ollama_ocr` library and the specified model installed in Ollama. ```python from ollama_ocr import OCRProcessor # Create an instance ocr = OCRProcessor(model_name='llama3.2-vision:11b') ``` -------------------------------- ### Initialize OCRProcessor and Process PDF for Text (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Initializes the `OCRProcessor` with a specified model (`granite3.2-vision`), processes a single PDF file to extract text, and prints the result. Requires the `ollama_ocr` library and the specified model installed in Ollama. ```python from ollama_ocr import OCRProcessor # Create an instance ocr = OCRProcessor(model_name='granite3.2-vision') # Test with an image (replace with your image path) result = ocr.process_image( image_path="input\pdf.pdf", format_type="text", language="eng", ) print(result) ``` -------------------------------- ### Install Ollama-OCR Dependencies Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md Installs the required Python packages listed in the requirements.txt file using pip. ```bash pip install -r requirements.txt ``` -------------------------------- ### Installing Ollama OCR Package Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md This command installs the Ollama OCR Python package using pip, making it available for use in your Python projects. ```bash pip install ollama-ocr ``` -------------------------------- ### Initialize OCRProcessor with Different Model Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Imports the `OCRProcessor` class and creates a new instance, specifying a different Ollama model name ('llama3.2-vision:11b') for subsequent processing tasks. ```python from ollama_ocr import OCRProcessor # Create an instance ocr = OCRProcessor(model_name='llama3.2-vision:11b') ``` -------------------------------- ### Process Single Image for Key-Value Output Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Uses the initialized `OCRProcessor` to process a single image file. It specifically requests the output to be formatted as key-value pairs and prints the resulting structure. ```python # Test with key-value format result = ocr.process_image( image_path="input\img.png", format_type="key_value" ) print(result) ``` -------------------------------- ### Perform Batch Processing and Print Statistics Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Initiates batch processing on a specified input directory using the initialized `OCRProcessor`. It then prints the summary statistics obtained from the batch processing results. ```python # Test batch processing batch_results = ocr.process_batch('input') print(batch_results['statistics']) ``` -------------------------------- ### Process Single Image for Structured Output Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Uses the initialized `OCRProcessor` to process a single image file. It requests the output in a structured format and prints the resulting structured data. ```python # structured format result = ocr.process_image( image_path="input\img.png", format_type="structured" ) print(result) ``` -------------------------------- ### Process Single Image for Key-Value (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Uses the initialized `OCRProcessor` to process a single image file (`img.png`), requesting the output in "key_value" format, and prints the resulting structured data. Assumes `OCRProcessor` has been initialized. ```python # Test with key-value format result = ocr.process_image( image_path="input\img.png", format_type="key_value" ) print(result) ``` -------------------------------- ### Print Detailed Batch Processing Statistics (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Prints a formatted summary of the batch processing statistics obtained from the `batch_results` dictionary, including total, successful, and failed counts. Assumes `process_batch` has already been called and `batch_results` is available. ```python # Print statistics print("\nProcessing Statistics:") print(f"Total images: {batch_results['statistics']['total']}") print(f"Successfully processed: {batch_results['statistics']['successful']}") print(f"Failed: {batch_results['statistics']['failed']}") ``` -------------------------------- ### Initialize OCRProcessor and Process Single Image for Text Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Initializes the `OCRProcessor` with a specified Ollama model and processes a single image file (e.g., PDF) to extract text. It prints the resulting text output. ```python from ollama_ocr import OCRProcessor # Create an instance ocr = OCRProcessor(model_name='granite3.2-vision') # Test with an image (replace with your image path) result = ocr.process_image( image_path="input\pdf.pdf", format_type="text", language="eng" ) print(result) ``` -------------------------------- ### Process Single Image for Text Output Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Uses the initialized `OCRProcessor` to process a single image file. It requests the output in a plain text format and prints the extracted text. ```python # Text result = ocr.process_image( image_path="input\img.png", format_type="text" ) print(result) ``` -------------------------------- ### Process Single Image for JSON Output Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Uses the initialized `OCRProcessor` to process a single image file. It requests the output to be formatted as a JSON object and prints the resulting JSON string. ```python # json format result = ocr.process_image( image_path="input\img.png", format_type="json" ) print(result) ``` -------------------------------- ### Iterate and Print Text from Successful Batch Results Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Loops through the successfully processed files contained within the batch results. For each file, it prints the file path followed by the extracted text. ```python # Get text from all successfully processed images for file_path, text in batch_results['results'].items(): print(f"\nFile: {file_path}") print("-" * 50) print(f"Extracted Text: {text}") ``` -------------------------------- ### Perform Batch Processing and Print Statistics (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Uses the initialized `OCRProcessor` to process all images within a specified directory ('input') in batch mode and prints the statistics summary from the results. Requires a directory containing images and an initialized `OCRProcessor` instance. ```python # Test batch processing batch_results = ocr.process_batch('input') print(batch_results['statistics']) ``` -------------------------------- ### Process Single Image for Structured Output (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Uses the initialized `OCRProcessor` to process a single image file (`img.png`), requesting the output in "structured" format, and prints the resulting structured data. Assumes `OCRProcessor` has been initialized. ```python # structured format result = ocr.process_image( image_path="input\img.png", format_type="structured" ) print(result) ``` -------------------------------- ### Process Single Image for Text (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Uses the initialized `OCRProcessor` to process a single image file (`img.png`), requesting the output in "text" format, and prints the extracted plain text. Assumes `OCRProcessor` has been initialized. ```python # Text result = ocr.process_image( image_path="input\img.png", format_type="text" ) print(result) ``` -------------------------------- ### Process Single Image for JSON (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Uses the initialized `OCRProcessor` to process a single image file (`img.png`), requesting the output in "json" format, and prints the resulting JSON structure. Assumes `OCRProcessor` has been initialized. ```python # json format result = ocr.process_image( image_path="input\img.png", format_type="json" ) print(result) ``` -------------------------------- ### Print Detailed Batch Processing Statistics Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Accesses and prints detailed statistics from the results of a batch processing operation, including the total number of images processed, successfully processed images, and failed images. ```python # Print statistics print("\nProcessing Statistics:") print(f"Total images: {batch_results['statistics']['total']}") print(f"Successfully processed: {batch_results['statistics']['successful']}") print(f"Failed: {batch_results['statistics']['failed']}") ``` -------------------------------- ### Check and Print Batch Processing Errors Source: https://github.com/imanoop7/ollama-ocr/blob/main/example_notebooks/example.ipynb Checks if the batch processing results contain any errors. If errors are present, it iterates through them and prints the file path and the specific error message for each file that failed processing. ```python # Check if there were any errors if batch_results['errors']: print("\nErrors:") for file_path, error in batch_results['errors'].items(): print(f"Error in {file_path}: {error}") ``` -------------------------------- ### Iterate and Print Extracted Text from Batch Results (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Loops through the `results` dictionary within the `batch_results` object, which contains the extracted text for each successfully processed file, and prints the file path and its corresponding text. Assumes `process_batch` has been called and `batch_results` is available. ```python # Get text from all successfully processed images for file_path, text in batch_results['results'].items(): print(f"\nFile: {file_path}") print("-" * 50) print(f"Extracted Text: {text}") ``` -------------------------------- ### Check and Print Batch Processing Errors (Python) Source: https://github.com/imanoop7/ollama-ocr/blob/main/example.ipynb Checks if the `errors` dictionary within `batch_results` is not empty. If errors exist, it iterates through them and prints the file path and the associated error message for each failed file. Assumes `process_batch` has been called and `batch_results` is available. ```python # Check if there were any errors if batch_results['errors']: print("\nErrors:") for file_path, error in batch_results['errors'].items(): print(f"Error in {file_path}: {error}") ``` -------------------------------- ### Run Streamlit Application Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md Launches the Streamlit web application by executing the app.py file. ```bash streamlit run app.py ``` -------------------------------- ### Clone Ollama-OCR Repository Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md Clones the Ollama-OCR repository from GitHub and navigates into the newly created directory. ```bash git clone https://github.com/imanoop7/Ollama-OCR.git cd Ollama-OCR ``` -------------------------------- ### Navigate to Application Directory Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md Changes the current directory to the source directory where the main application file (app.py) is located. ```bash cd src/ollama_ocr ``` -------------------------------- ### Processing Single File with Ollama OCR Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md This Python code snippet demonstrates how to initialize the OCRProcessor and process a single image or PDF file using a specified Ollama model, output format, and optional custom prompt and language. ```python from ollama_ocr import OCRProcessor # Initialize OCR processor ocr = OCRProcessor(model_name='llama3.2-vision:11b', base_url="http://host.docker.internal:11434/api/generate") # You can use any vision model available on Ollama # you can pass your custom ollama api # Process an image result = ocr.process_image( image_path="path/to/your/image.png", # path to your pdf files "path/to/your/file.pdf" format_type="markdown", # Options: markdown, text, json, structured, key_value custom_prompt="Extract all text, focusing on dates and names.", # Optional custom prompt language="English" # Specify the language of the text (New! 🆕) ) print(result) ``` -------------------------------- ### Processing Batch Files with Ollama OCR Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md This Python code snippet shows how to use the OCRProcessor for batch processing of multiple image or PDF files within a directory, including options for parallel processing, recursive search, preprocessing, custom prompts, and language specification. ```python from ollama_ocr import OCRProcessor # Initialize OCR processor ocr = OCRProcessor(model_name='llama3.2-vision:11b', max_workers=4) # max workers for parallel processing # Process multiple images # Process multiple images with progress tracking batch_results = ocr.process_batch( input_path="path/to/images/folder", # Directory or list of image paths format_type="markdown", recursive=True, # Search subdirectories preprocess=True, # Enable image preprocessing custom_prompt="Extract all text, focusing on dates and names.", # Optional custom prompt language="English" # Specify the language of the text (New! 🆕) ) # Access results for file_path, text in batch_results['results'].items(): print(f"\nFile: {file_path}") print(f"Extracted Text: {text}") # View statistics print("\nProcessing Statistics:") print(f"Total images: {batch_results['statistics']['total']}") print(f"Successfully processed: {batch_results['statistics']['successful']}") print(f"Failed: {batch_results['statistics']['failed']}") ``` -------------------------------- ### Pulling Required Ollama Models Source: https://github.com/imanoop7/ollama-ocr/blob/main/README.md These commands pull specific vision language models from the Ollama library, which are required by the Ollama OCR package to perform text extraction from images and PDFs. ```bash ollama pull llama3.2-vision:11b ollama pull granite3.2-vision ollama pull moondream ollama pull minicpm-v ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.