How to Convert Receipt Images to JSON Using OCR with ChatML

Aug 5, 2024 | Educational

In the ever-evolving world of data interaction, converting receipts from images to structured data allows businesses to streamline operations and enhance accuracy. Today, we’ll explore how to use OCR (Optical Character Recognition) capabilities to achieve this magical transformation seamlessly using ChatML and a model named mychen76mistral_ocr2json_v3_chatml.

What You Need

  • Paddle-OCR: A powerful tool for processing images for text recognition.
  • Python and Jupyter Notebooks: For providing a flexible environment to run your code.
  • Image of a Receipt: Make sure it’s clear to facilitate accurate OCR.
  • Model ID: The model you’re going to utilize: mychen76mistral_ocr2json_v3_chatml.

Step-by-Step Guide

Follow these steps to convert your receipt image to JSON format with ease:

1. Set Up Your Environment

Make sure you have the necessary libraries installed:

pip install Paddle-OCR

Set up your Jupyter Notebook to work with the libraries and the model efficiently. Ensure your model path is set correctly to point to mychen76mistral_ocr2json_v3_chatml.

2. Use Paddle-OCR to Detect Text

Load the image of the receipt and apply OCR to extract the text. Here’s a sample code that demonstrates this:


import paddleocr

ocr = paddleocr.OCR()
receipt_image = 'path_to_your_receipt_image.jpg'
receipt_boxes = ocr.ocr(receipt_image)

3. Structure the Data

Once you have extracted the text, the next task is to structure it into a JSON format. You’ll need to parse the OCR result and construct the JSON object accordingly. Think of this as organizing a chaotic room—placing every item in its designated spot!


receipt_data = {
    "store_name": "The Lone Pine",
    "store_address": "43 Manchester Road",
    "city": "Brisbane",
    "country": "Australia",
    "phone": "617-3236-6207",
    "invoice_number": "08000008",
    "invoice_date": "09/04/08",
    "table_number": 25,
    "time": "12:45",
    "items": [
        {"item_name": "Carlsberg Bottle", "quantity": 2, "price": 16.00},
        {"item_name": "Heineken Draft Half Liter", "quantity": 1, "price": 15.20},
        {"item_name": "Heineken Draft Standard", "quantity": 3, "price": 12.00},
        {"item_name": "Sirloin Steak", "quantity": 3, "price": 96.00},
        {"item_name": "Grilled Chicken Breast", "quantity": 4, "price": 74.00},
        {"item_name": "Coke", "quantity": 1, "price": 3.50},
        {"item_name": "Ice Cream", "quantity": 5, "price": 18.00}
    ],
    "subtotal": 327.30,
    "tax": 16.36,
    "service_charge": 32.73,
    "total": 376.40,
    "payment": {
        "cash": 400.00,
        "change": 23.60
    },
    "customer": {
        "name": "John",
        "discount": "15%"
    }
}

Troubleshooting Tips

As with any project, you may run into issues. Here are some common problems and solutions:

  • OCR not recognizing text: Ensure your receipt images are of good quality and have clear, legible text.
  • Incorrect JSON structure: Double-check your parsing logic to ensure all data points are captured correctly.
  • Runtime errors: Verify the versions of your installed libraries and ensure compatibility.
  • Performance problems: Optimize your OCR settings, or try using a different image-enhancing tool prior to OCR processing.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With just a few steps, you can easily convert receipt images into structured JSON data using Paddle-OCR and ChatML. This not only saves time but also enhances the accuracy of your data handling processes. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox