diff --git a/machine-learning-client/RestaurantReceipt2.jpeg b/machine-learning-client/RestaurantReceipt2.jpeg new file mode 100644 index 0000000..6adacd0 Binary files /dev/null and b/machine-learning-client/RestaurantReceipt2.jpeg differ diff --git a/machine-learning-client/main.py b/machine-learning-client/main.py index 0d47d16..d88147b 100644 --- a/machine-learning-client/main.py +++ b/machine-learning-client/main.py @@ -6,6 +6,7 @@ import json from dotenv import load_dotenv from bson import ObjectId +from mindee import Client, PredictResponse, product # To access mindee OCR API import logging @@ -58,7 +59,7 @@ def pretdict_endpoint(): return jsonify({'error': 'Object_ID not found in request data'}), 400 Object_ID = ObjectId(request_data['Object_ID']) - logger.debug('OBJECT_ID MESSAGE:', Object_ID) + logging.debug('OBJECT_ID MESSAGE:', Object_ID) #image = db.receipts.find_one({"_id": Object_ID})['image'] # Here, you would add the code to perform OCR on the image @@ -66,23 +67,29 @@ def pretdict_endpoint(): # Uncomment next line to perform OCR data = perform_ocr(Object_ID) + logging.debug("data after ocr: %s", data) # debug + print("data after ocr: %s", data) #data = json.load(open("response1.json", "r")) # Connect to your collection (replace 'mycollection' with your collection name) collection = db['receipts'] # Prepare the data to be inserted into the database - receipt = data['receipts'][0] + # line_items = data['document']['inference']['pages'][0]['prediction']['line_items'] + receipt = data['document']['inference']['pages'][0]['prediction'] + logging.debug('OCR Json Keys:', data['document']['inference']['pages'][0]['prediction'].keys()) # debug + print('OCR Json Keys:', data['document']['inference']['pages'][0]['prediction'].keys()) receipt_data = { - 'receipt_name': receipt['merchant_name'], - 'currency': receipt['currency'], - 'items': [{'description': item['description'], 'amount': item['amount']} for item in receipt['items']], - 'total': receipt['total'], - 'tax': receipt['tax'], - 'tip': receipt['tip'], - 'subtotal': receipt['subtotal'], + 'receipt_name': receipt['supplier_name']['raw_value'], + 'currency': receipt['locale']['currency'], + 'items': [{'description': item['description'], 'amount': item['total_amount'], 'quantity': item['quantity']} for item in receipt['line_items']], + 'total': receipt['total_amount']['value'], + 'tax': receipt['total_tax']['value'], + 'tip': receipt['tip']['value'], + 'subtotal': receipt['total_net']['value'], } - logger.debug(receipt_data) + logging.debug(receipt_data) + print("receipt_data: %s", receipt_data) # Update the document with given ObjectId collection.update_one({'_id': Object_ID}, {'$set': receipt_data}) @@ -92,32 +99,80 @@ def pretdict_endpoint(): return jsonify({'_id': str(inserted_id)}) def perform_ocr(Object_ID): - logger.debug("starting perform_ocr function...") # debug - url = "https://ocr.asprise.com/api/v1/receipt" + logging.debug("starting perform_ocr function with mindee api...") # debug + url = "https://api.mindee.net/v1/products/mindee/expense_receipts/v5/predict" + api_key = os.getenv("OCR_API_KEY") # Get the API key from environment variable + + # Fetch the image data from the database image_data = db.receipts.find_one({"_id": Object_ID})['image'] file_path = f"receipt_{Object_ID}.jpg" # Set the file path to save the image - logger.debug("file path: %s", file_path) # debug + logging.debug("file path: %s", file_path) # debug + + # Save the image data to a file with open(file_path, "wb") as f: f.write(image_data) - file_path = file_path.replace('\x00', '') # Remove any null bytes from the file path - - - # Get response (can only do this a couple times with the test API key) - res = requests.post(url, - data = { - 'api_key': 'TEST', - 'recognizer': 'auto', - 'ref_no': 'ocr_python_123' - }, - - files = { - 'file': open(file_path, "rb") - }) - - with open("response.json", "w") as f: - f.write(res.text) - return res.json() + # Parse the file with the Mindee API + with open(file_path, "rb") as myfile: + files = {"document": myfile} + headers = {"Authorization": "Token " + api_key} + response = requests.post(url, files=files, headers=headers) + print("Type of response: ", type(response)) + print("response.text: %s", response.text) + return response.json() + +# def perform_ocr(Object_ID): +# logging.debug("starting perform_ocr function with mindee api...") # debug +# # Init a new client +# api_key = os.getenv("OCR_API_KEY") # Get the API key from environment variable +# mindee_client = Client(api_key) + +# # Fetch the image data from the database +# image_data = db.receipts.find_one({"_id": Object_ID})['image'] +# file_path = f"receipt_{Object_ID}.jpg" # Set the file path to save the image +# logging.debug("file path: %s", file_path) # debug + +# # Save the image data to a file +# with open(file_path, "wb") as f: +# f.write(image_data) + +# # Load the file +# input_doc = mindee_client.source_from_path(file_path) + +# # Parse the file with the Mindee API +# result: PredictResponse = mindee_client.parse(product.ReceiptV5, input_doc) +# logging.debug("result: %s", result.document) # debug + +# # Return the result as a JSON response +# return jsonify(result.document) + +# def perform_ocr_old_version(Object_ID): +# logging.debug("starting perform_ocr function...") # debug +# url = "https://ocr.asprise.com/api/v1/receipt" +# image_data = db.receipts.find_one({"_id": Object_ID})['image'] +# file_path = f"receipt_{Object_ID}.jpg" # Set the file path to save the image +# logging.debug("file path: %s", file_path) # debug +# with open(file_path, "wb") as f: +# f.write(image_data) +# file_path = file_path.replace('\x00', '') # Remove any null bytes from the file path + + +# # Get response (can only do this a couple times with the test API key) +# res = requests.post(url, +# data = { +# 'api_key': 'TEST', +# 'recognizer': 'auto', +# 'ref_no': 'ocr_python_123' +# }, + +# files = { +# 'file': open(file_path, "rb") +# }) + +# with open("response.json", "w") as f: +# f.write(res.text) + +# return res.json() if __name__ == '__main__': app.run(host='0.0.0.0', port=5002) # Run the app \ No newline at end of file diff --git a/machine-learning-client/requirements.txt b/machine-learning-client/requirements.txt index bc7580b..c116578 100644 --- a/machine-learning-client/requirements.txt +++ b/machine-learning-client/requirements.txt @@ -3,4 +3,5 @@ pylint black requests Flask -python-dotenv \ No newline at end of file +python-dotenv +mindee \ No newline at end of file diff --git a/machine-learning-client/sample-response.json b/machine-learning-client/sample-response.json new file mode 100644 index 0000000..e69de29