-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImagetoText.py
79 lines (64 loc) · 2.17 KB
/
ImagetoText.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
import cv2
import imutils
import json
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import requests
import time
from base64 import b64encode
from IPython.display import Image
from pylab import rcParams
rcParams['figure.figsize'] = 10, 20
def makeImageData(imgpath):
img_req = None
with open(imgpath, 'rb') as f:
ctxt = b64encode(f.read()).decode()
img_req = {
'image': {
'content': ctxt
},
'features': [{
'type': 'DOCUMENT_TEXT_DETECTION',
'maxResults': 1
}]
}
return json.dumps({"requests": img_req}).encode()
def requestOCR(url, api_key, imgpath):
imgdata = makeImageData(imgpath)
response = requests.post(ENDPOINT_URL,
data=imgdata,
params={'key': api_key},
headers={'Content-Type': 'application/json'})
return response
with open('calm-pagoda-337903-a2dec9d554ae.json') as f:
data = json.load(f)
for i in data:
print(i)
ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'
api_key = data["private_key"]
img_loc = "UOFA.png"
Image(img_loc)
result = requestOCR(ENDPOINT_URL, api_key, img_loc)
print('result', result)
if result.status_code != 200 or result.json().get('error'):
print("Error")
else:
result = result.json()['responses'][0]['textAnnotations']
# for index in range(len(result)):
# print(result[index]["description"])
for index in result:
print(index)
def gen_cord(r):
cord_df = pd.DataFrame(r['boundingPoly']['vertices'])
x_min, y_min = np.min(cord_df["x"]), np.min(cord_df["y"])
x_max, y_max = np.max(cord_df["x"]), np.max(cord_df["y"])
return r["description"], x_max, x_min, y_max, y_min
# print(gen_cord(result[-1]))
#text, x_max, x_min, y_max, y_min = gen_cord(result[-1])
foo=gen_cord(result[-1])
image = cv2.imread(img_loc)
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
print("Text Detected = {}".format(text))