mail@pastecode.io avatar
6 months ago
1.7 kB
import cv2
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input, decode_predictions

# Load the pre-trained InceptionV3 model
model = InceptionV3(weights='imagenet')

def preprocess_image(img_path):
    # Load and preprocess the image
    img = image.load_img(img_path, target_size=(299, 299))
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = preprocess_input(img_array)
    return img_array

def predict_invoice(img_path):
    # Preprocess the image
    img_array = preprocess_image(img_path)
    #print("Image Array : ",img_array)

    # Make predictions
    predictions = model.predict(img_array)
    #print("Predictions : ",predictions)

    # Decode and print the top-3 predicted classes
    decoded_predictions = decode_predictions(predictions, top=3)[0]
    print("Decoded Predictions:")
    for i, (imagenet_id, label, score) in enumerate(decoded_predictions):
        print(f"{i + 1}: {label} ({score:.2f})")

    # Check if the top prediction is related to an invoice
    invoice_keywords = ["invoice", "receipt", "bill"]
    for (_, label, _) in decoded_predictions:
        if any(keyword in label.lower() for keyword in invoice_keywords):
            return True
    return False

# Test the function with an image
img_path = "/Analytics/venv/Jup/CAPE_Case_Management_PDF_Invoicing/Data/images/Amazon_Web_Services_1001319invoice1411607217.pdf_page_1.png"
is_invoice = predict_invoice(img_path)

if is_invoice:
    print("The image is likely an invoice.")
    print("The image is not recognized as an invoice.")
Leave a Comment