-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
136 lines (105 loc) · 4.17 KB
/
app.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
########### Python Form Recognizer Async Receipt #############
import pyrebase as pyrebase
from flask import Flask, jsonify, request
import json
import time
from requests import get, post
d = {}
config = {
"apiKey": "AIzaSyApwm_OA6QKatzNVcYOuom7F0-gJ8Gtg6g",
"authDomain": "trialapp-515fc.firebaseapp.com",
"projectId": "trialapp-515fc",
"storageBucket": "trialapp-515fc.appspot.com",
"messagingSenderId": "106875930587",
"appId": "1:106875930587:web:4be17c4b8882529a5dc9fe",
"measurementId": "G-0PS6SE48G0",
"databaseURL": ""
}
def download_img():
firebase = pyrebase.initialize_app(config)
storage = firebase.storage()
storage.child('image/img.jpg').download('form.png')
def receipt(apim_key, post_url, data_bytes, headers, params):
try:
resp = post(url=post_url, data=data_bytes, headers=headers, params=params)
if resp.status_code != 202:
print("POST analyze failed:\n%s" % resp.text)
quit()
print("POST analyze succeeded:\n%s" % resp.headers)
get_url = resp.headers["operation-location"]
except Exception as e:
print("POST analyze failed:\n%s" % str(e))
quit()
n_tries = 10
n_try = 0
wait_sec = 6
while n_try < n_tries:
try:
resp = get(url=get_url, headers={"Ocp-Apim-Subscription-Key": apim_key})
resp_json = json.loads(resp.text)
if resp.status_code != 200:
print("GET Receipt results failed:\n%s" % resp_json)
quit()
status = resp_json["status"]
if status == "succeeded":
# print("Receipt Analysis succeeded:\n%s" % resp_json)
return resp_json
break
if status == "failed":
print("Analysis failed:\n%s" % resp_json)
quit()
# Analysis still running. Wait and retry.
time.sleep(wait_sec)
n_try += 1
except Exception as e:
msg = "GET analyze results failed:\n%s" % str(e)
print(msg)
quit()
import os
#
ASSETS_DIR = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__)
@app.route('/', methods = ["GET"])
def hello_world():
if request.method == 'GET':
download_img()
return {}
@app.route('/api', methods=["GET"])
def hello():
if request.method == 'GET':
endpoint = r"https://covidformrec.cognitiveservices.azure.com/"
apim_key = "bdcfc1800c2f414d8f6a1c1e5d7566a3"
source = r"form.png"
headers = {
# Request headers
'Content-Type': 'image/png',
'Ocp-Apim-Subscription-Key': apim_key,
}
params = {
"includeTextDetails": True,
"locale": "en-US"
}
with open(source, "rb") as f:
data_bytes = f.read()
post_url = endpoint + "/formrecognizer/v2.1-preview.2/prebuilt/businesscard/analyze"
resp_json = receipt(apim_key, post_url, data_bytes, headers, params)
resp_json = resp_json['analyzeResult']['documentResults'][0]['fields']['Emails']['valueArray'][0]
d['email'] = resp_json['valueString']
post_url = endpoint + "/formrecognizer/v2.1-preview.2/prebuilt/invoice/analyze"
resp_json = receipt(apim_key, post_url, data_bytes, headers, params)
# for name
resp_json_temp = resp_json['analyzeResult']['documentResults'][0]['fields']['CustomerName']
d['name'] = resp_json_temp['valueString']
# # for ID
# resp_json_temp = resp_json['analyzeResult']['documentResults'][0]['fields']['InvoiceId']
#
# d['id'] = resp_json_temp['text']
# # for date
# resp_json_temp = resp_json['analyzeResult']['documentResults'][0]['fields']['InvoiceDate']
#
# d['date'] = resp_json_temp['text']
return resp_json['analyzeResult']['documentResults'][0]['fields']
if __name__ == "__main__":
# port = int(os.environ.get("PORT", 5000))
# app.run(host="0.0.0.0", port=port)
app.run()