Spaces:
Runtime error
Runtime error
Commit ·
4c24cb0
1
Parent(s): 59202bb
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytesseract
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import re
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def ocr_df_using_pytesseract(image):
|
| 13 |
+
#pytesseract.pytesseract.tesseract_cmd =r"C:\Users\amold\Desktop\Upwork\pdf to image and pytesseract\tesseact_exe\Tesseract-OCR\tesseract.exe"
|
| 14 |
+
|
| 15 |
+
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
|
| 16 |
+
#image = Image.open(example['image_path'])
|
| 17 |
+
|
| 18 |
+
width, height = image.size
|
| 19 |
+
|
| 20 |
+
# apply ocr to the image
|
| 21 |
+
ocr_df = pytesseract.image_to_data(image, output_type='data.frame')
|
| 22 |
+
float_cols = ocr_df.select_dtypes('float').columns
|
| 23 |
+
ocr_df = ocr_df.dropna().reset_index(drop=True)
|
| 24 |
+
ocr_df[float_cols] = ocr_df[float_cols].round(0).astype(int)
|
| 25 |
+
ocr_df = ocr_df.replace(r'^\s*$', np.nan, regex=True)
|
| 26 |
+
ocr_df = ocr_df.dropna().reset_index(drop=True)
|
| 27 |
+
|
| 28 |
+
ocr_df
|
| 29 |
+
|
| 30 |
+
ocr_df['X1']=ocr_df['left']
|
| 31 |
+
|
| 32 |
+
ocr_df['Y1']=ocr_df['top']
|
| 33 |
+
|
| 34 |
+
ocr_df['X2']= ocr_df['left'] + ocr_df['width']
|
| 35 |
+
|
| 36 |
+
ocr_df['Y2']= ocr_df['top'] + ocr_df['height']
|
| 37 |
+
|
| 38 |
+
return ocr_df
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def image_to_text(image):
|
| 42 |
+
ocr_df= ocr_df_using_pytesseract(image)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
grouped_text = ocr_df.groupby(['block_num', 'line_num'])['text'].agg(' '.join).reset_index()
|
| 46 |
+
|
| 47 |
+
# sort the text by line numbers within each block
|
| 48 |
+
grouped_text = grouped_text.sort_values(['block_num', 'line_num'])
|
| 49 |
+
|
| 50 |
+
# join the text by blocks and add newlines
|
| 51 |
+
result = ''
|
| 52 |
+
for i, row in grouped_text.iterrows():
|
| 53 |
+
if i > 0 and row['block_num'] != grouped_text.loc[i-1, 'block_num']:
|
| 54 |
+
result += '\n\n'
|
| 55 |
+
result += row['text'].rstrip() + '\n'
|
| 56 |
+
|
| 57 |
+
return result
|
| 58 |
+
|
| 59 |
+
def getting_extractions:
|
| 60 |
+
text= image_to_text(image)
|
| 61 |
+
|
| 62 |
+
item_pattern = r"(\d+)\s*of:(.*?)\$(\d+\.\d{2})"
|
| 63 |
+
|
| 64 |
+
# Extracting the matches using regex
|
| 65 |
+
item_matches = re.findall(item_pattern, text, re.DOTALL)
|
| 66 |
+
|
| 67 |
+
items = []
|
| 68 |
+
|
| 69 |
+
for match in item_matches:
|
| 70 |
+
quantity, description, price = match
|
| 71 |
+
quantity = int(quantity)
|
| 72 |
+
description = description.strip()
|
| 73 |
+
price = float(price)
|
| 74 |
+
|
| 75 |
+
item = {
|
| 76 |
+
"quantity": quantity,
|
| 77 |
+
"description": description,
|
| 78 |
+
"price": price
|
| 79 |
+
}
|
| 80 |
+
items.append(item)
|
| 81 |
+
|
| 82 |
+
# Creating a pandas DataFrame
|
| 83 |
+
df = pd.DataFrame(items, columns=["quantity", "description", "price"])
|
| 84 |
+
|
| 85 |
+
return df
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
demo = gr.Interface(fn=image_to_text,
|
| 92 |
+
inputs= gr.Image(type="pil"),
|
| 93 |
+
outputs=["dataframe"],
|
| 94 |
+
title="Amazon_invoice_to_text",
|
| 95 |
+
description= "Upload invoice image here")
|
| 96 |
+
demo.launch(share=False)
|