-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathocr.py
More file actions
121 lines (87 loc) · 3.55 KB
/
ocr.py
File metadata and controls
121 lines (87 loc) · 3.55 KB
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
import requests
import json
import base64
from google.auth.transport.requests import Request
from google.oauth2 import service_account
#from reportlab.pdfgen import canvas
from dotenv import load_dotenv
import os
from dotenv import *
from flask import jsonify
import google.generativeai as genai
import google.ai.generativelanguage as glm
from google.generativeai.types.content_types import *
from PIL import Image
import json
VISION_GDCH_ENDPOINT = "vision.googleapis.com"
load_dotenv()
keyfile_path = os.getenv("fpath")
with open(keyfile_path, "r") as keyfile:
credentials_info = json.load(keyfile)
credentials = service_account.Credentials.from_service_account_info(
info=credentials_info,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
credentials.refresh(Request())
ACCESS_TOKEN = credentials.token
def get_response(imgbase64, access_token):
annotate_image_request = {
"requests": [
{
"image": {"content": imgbase64},
"features": [
{"type": "TEXT_DETECTION"},
],
}
]
}
json_data = json.dumps(annotate_image_request)
url = f"https://{VISION_GDCH_ENDPOINT}/v1/images:annotate"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}",
}
#print(headers)
response = requests.post(url, data=json_data, headers=headers)
#print(response)
return response
def parse_text(response,line_height=40):
if response.status_code == 200:
result = response.json()
text_annotations = result['responses'][0]['textAnnotations']
sorted_words = sorted(text_annotations[1:], key=lambda x: (x['boundingPoly']['vertices'][0]['y']//line_height, x['boundingPoly']['vertices'][0]['x']))
prev_line_y = sorted_words[0]['boundingPoly']['vertices'][0]['y']
text = ""
for word in sorted_words:
word_text = word['description']
text += word_text + " "
word_y = word['boundingPoly']['vertices'][0]['y']
if abs(word_y - prev_line_y) > line_height:
text += "\n"
prev_line_y = word_y
return text
else:
print(f"Error: {response.status_code}, {response.text}")
def get_text(imgbase64, access_token=ACCESS_TOKEN):
response = get_response(imgbase64, access_token)
text = parse_text(response)
return text
def generate_description(out):
load_dotenv()
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel("gemini-pro")
responses = model.generate_content([out, "Get the name of the proc/cpt code which and the price(without dollar sign) which is after the proc code. Output a json in the following format {\"proc code\": \"price\"}. Don't include ```json or anything else. ALso do not include newline characters."])
return responses.text
def get_text_from_image(image):
return generate_description(out=get_text(image, ACCESS_TOKEN))
# def main():
# image_path = "insuaid/documents/doc2.jpg"
# with open(image_path, "rb") as image_file:
# imgbase64 = base64.b64encode(image_file.read()).decode("utf-8")
# fin=generate_description(out=get_text(imgbase64, ACCESS_TOKEN))
# fin_json = json.loads(fin)
# url = 'http://127.0.0.1:5000/query-prices'
# ffin={'insurance_provider':'UNITED HEALTHCARE', 'procedures': fin_json}
# return_json = requests.post(url, json=ffin)
# print(return_json.text)