File size: 1,535 Bytes
b7f8371
 
 
 
 
 
 
90ad422
 
 
b7f8371
 
 
 
 
 
 
 
 
 
 
d37ff71
b7f8371
c1ded46
d37ff71
c1ded46
b7f8371
 
 
 
 
207fbf1
90ad422
207fbf1
 
90ad422
 
 
 
c1ded46
d37ff71
fd8da39
d37ff71
767cd3f
d37ff71
 
 
207fbf1
d37ff71
 
767cd3f
 
8f35abd
767cd3f
d37ff71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os

# os.system('chmod 777 /tmp')
# os.system('apt-get update -y')
# os.system('apt-get install tesseract-ocr -y')
# os.system('pip install -q pytesseract')

from base64 import b64decode, b64encode
from io import BytesIO



import tesserocr
from fastapi import FastAPI, File, Form
from PIL import Image
from transformers import pipeline
#import streamlit as st

# pytesseract.pytesseract.tesseract_cmd = r’./Tesseract-OCR/tesseract.exe’
choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
description = """
Upload Receipt and get
"""
app = FastAPI(
    title="ReceiptOCR",
    docs_url="/", description=description)

pipe = pipeline("document-question-answering", model="impira/layoutlm-document-qa")



@app.post("/image")
def read_root(image_file: bytes = File(...)):
    

    #img = Image.open(io.BytesIO(base64.decodebytes(bytes(imageBase64, "utf-8"))))
    #print(img)
    #image = img
    image = Image.open(BytesIO(image_file))

    question_1 = "What is the Total amount?"
    question_2 = "What is the VAT amount?"
    question_3 = "What is the Date?"
    question_4 = "What is the currency code?"
    output_1 = pipe(image, question_1)
    output_2 = pipe(image, question_2)
    output_3 = pipe(image, question_3)
    output_4 = pipe(image, question_4)
    
    response = {}
    response['totalAmount'] = output_1[0]['answer']
    response['totalVat'] = output_2[0]['answer']
    response['date'] = output_3[0]['answer']
    response['currencyCode'] = output_4[0]['answer']
    return response