Skip to content

Commit 9e4da93

Browse files
authored
Merge pull request #99 from PracticalDL/siddha/chapter-8
Add OCR
2 parents e54f70e + 9be7017 commit 9e4da93

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import http.client, urllib, base64
2+
import sys
3+
import os
4+
from os import listdir
5+
from os.path import isfile, join
6+
import json
7+
try:
8+
from urllib.parse import urlparse
9+
except ImportError:
10+
from urlparse import urlparse
11+
import boto3
12+
13+
def amazon(filename):
14+
with open(filename, "rb") as image_file:
15+
image_bytes = image_file.read()
16+
client = boto3.client('rekognition')
17+
response = client.detect_text(Image={'Bytes': image_bytes })
18+
text_detections = response[u'TextDetections']
19+
result = []
20+
for each in text_detections:
21+
result.append(each[u'DetectedText'])
22+
return result
23+
24+
def getopts(argv):
25+
opts = {} # Empty dictionary to store key-value pairs.
26+
while argv: # While there are arguments left to parse.
27+
if argv[0][0] == '-': # Found a "-name value" pair.
28+
opts[argv[0]] = argv[1] # Add key and value to the dictionary.
29+
argv = argv[1:] # Reduce the argument list by copying it starting from index 1.
30+
return opts
31+
32+
def process_image(image):
33+
results = {}
34+
results[image] = amazon(image)
35+
return results
36+
37+
def process_images(directory):
38+
images = [join(directory, f) for f in listdir(directory) if isfile(join(directory, f)) and (f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png"))]
39+
results = {}
40+
for image in images:
41+
results[image] = amazon(image)
42+
return results
43+
44+
if __name__ == '__main__':
45+
from sys import argv
46+
input_args = getopts(argv)
47+
results = []
48+
if '-i' in input_args and '-o' in input_args:
49+
image = input_args['-i']
50+
results = process_image(image)
51+
output_path = input_args['-o']
52+
with open(output_path, "w") as write_file:
53+
json.dump(results, write_file)
54+
elif '-d' in input_args and '-o' in input_args:
55+
directory = input_args['-d']
56+
results = process_images(directory)
57+
output_path = input_args['-o']
58+
with open(output_path, "w") as write_file:
59+
json.dump(results, write_file)
60+
else:
61+
print("Usage: python amazon.py [-i path to an image | -d path to directory of images] [-o output path]")
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import http.client, urllib, base64
2+
import sys
3+
import os
4+
from os import listdir
5+
from os.path import isfile, join
6+
import json
7+
try:
8+
from urllib.parse import urlparse
9+
except ImportError:
10+
from urlparse import urlparse
11+
import boto3
12+
13+
google_api_key = 'ADD_YOUR_KEY_HERE'
14+
15+
def google(filename):
16+
with open(filename, "rb") as image_file:
17+
encoded_string = base64.b64encode(image_file.read())
18+
19+
endpoint = "/v1/images:annotate?key=" + google_api_key
20+
21+
detection_type = "TEXT_DETECTION"
22+
request_body = {
23+
"requests":[
24+
{
25+
"image":{
26+
"content":encoded_string
27+
},
28+
"features":[
29+
{
30+
"type":detection_type,
31+
"maxResults":10
32+
}
33+
]
34+
}
35+
]
36+
}
37+
38+
try:
39+
conn = http.client.HTTPSConnection('vision.googleapis.com')
40+
conn.request("POST", endpoint, json.dumps(request_body))
41+
response = conn.getresponse()
42+
data = response.read()
43+
json_data = json.loads(data)
44+
result = []
45+
annotations = json_data['responses'][0]['textAnnotations']
46+
for annotation in annotations:
47+
result.append(annotation['description'])
48+
return result
49+
conn.close()
50+
except Exception as e:
51+
print(e)
52+
53+
def getopts(argv):
54+
opts = {} # Empty dictionary to store key-value pairs.
55+
while argv: # While there are arguments left to parse.
56+
if argv[0][0] == '-': # Found a "-name value" pair.
57+
opts[argv[0]] = argv[1] # Add key and value to the dictionary.
58+
argv = argv[1:] # Reduce the argument list by copying it starting from index 1.
59+
return opts
60+
61+
def process_image(image):
62+
results = {}
63+
results[image] = google(image)
64+
return results
65+
66+
def process_images(directory):
67+
images = [join(directory, f) for f in listdir(directory) if isfile(join(directory, f)) and (f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png"))]
68+
results = {}
69+
for image in images:
70+
results[image] = google(image)
71+
return results
72+
73+
74+
if __name__ == '__main__':
75+
input_args = getopts(sys.argv)
76+
results = []
77+
if '-i' in input_args and '-o' in input_args:
78+
image = input_args['-i']
79+
results = process_image(image)
80+
output_path = input_args['-o']
81+
with open(output_path, "w") as write_file:
82+
json.dump(results, write_file)
83+
elif '-d' in input_args and '-o' in input_args:
84+
directory = input_args['-d']
85+
results = process_images(directory)
86+
output_path = input_args['-o']
87+
with open(output_path, "w") as write_file:
88+
json.dump(results, write_file)
89+
else:
90+
print("Usage: python google.py [-i path to an image | -d path to directory of images] [-o output path]")
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import http.client, urllib
2+
from sys import argv
3+
import json
4+
import os
5+
from os import listdir
6+
from os.path import isfile, join
7+
try:
8+
from urllib.parse import urlparse
9+
except ImportError:
10+
from urlparse import urlparse
11+
12+
microsoft_api_key = 'ADD_YOUR_KEY_HERE'
13+
path_to_images = "http://ADD_YOUR_IP_ADDRESS_HERE/images/"
14+
15+
def microsoft_phase_1(filename):
16+
headers = {
17+
'Content-Type': 'application/json',
18+
'Ocp-Apim-Subscription-Key': microsoft_api_key,
19+
}
20+
21+
endpoint = "/vision/v2.0/recognizeText?mode=Printed"
22+
params = urllib.urlencode({
23+
# Request parameters
24+
# 'mode': 'Printed'
25+
})
26+
try:
27+
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
28+
request_body = "{\"url\":\"" + path_to_images + filename + "\"}"
29+
conn.request("POST", endpoint, request_body, headers)
30+
response = conn.getresponse()
31+
url = response.getheader("Operation-Location")
32+
recognition_id = urlparse(url).path.split('/')[-1]
33+
return recognition_id
34+
conn.close()
35+
except Exception as e:
36+
print(e)
37+
print("Error")
38+
39+
40+
def getopts(argv):
41+
opts = {} # Empty dictionary to store key-value pairs.
42+
while argv: # While there are arguments left to parse.
43+
if argv[0][0] == '-': # Found a "-name value" pair.
44+
opts[argv[0]] = argv[1] # Add key and value to the dictionary.
45+
argv = argv[1:] # Reduce the argument list by copying it starting from index 1.
46+
return opts
47+
48+
def process_image(image):
49+
results = {}
50+
results[image] = microsoft_phase_1(image)
51+
return results
52+
53+
def process_images(directory):
54+
results = {}
55+
images = [f for f in listdir(directory) if isfile(join(directory, f))]
56+
for image in images:
57+
results[image] = microsoft_phase_1(image)
58+
return results
59+
60+
if __name__ == '__main__':
61+
input_args = getopts(argv)
62+
results = []
63+
if '-i' in input_args:
64+
image = input_args['-i']
65+
results = process_image(image)
66+
for key, value in results.iteritems():
67+
print(key + "," + value)
68+
elif '-d' in input_args:
69+
directory = input_args['-d']
70+
results = process_images(directory)
71+
for key, value in results.iteritems():
72+
print(key + "," + value)
73+
else:
74+
print("Usage: python microsoft-phase-1.py [-i path to an image | -d path to directory of images]")
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import http.client, urllib, base64
2+
import sys
3+
import os
4+
from os import listdir
5+
from os.path import isfile, join
6+
import json
7+
import base64
8+
try:
9+
from urllib.parse import urlparse
10+
except ImportError:
11+
from urlparse import urlparse
12+
import boto3
13+
14+
microsoft_api_key = 'ADD_YOUR_KEY_HERE'
15+
16+
def microsoft_phase_2(recognition_id):
17+
headers = {
18+
'Ocp-Apim-Subscription-Key': microsoft_api_key,
19+
}
20+
21+
try:
22+
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
23+
conn.request("GET", "/vision/v2.0/textOperations/" + recognition_id, "", headers)
24+
result = []
25+
response = conn.getresponse()
26+
return response.read()
27+
except Exception as e:
28+
print(e)
29+
30+
def getopts(argv):
31+
opts = {} # Empty dictionary to store key-value pairs.
32+
while argv: # While there are arguments left to parse.
33+
if argv[0][0] == '-': # Found a "-name value" pair.
34+
opts[argv[0]] = argv[1] # Add key and value to the dictionary.
35+
argv = argv[1:] # Reduce the argument list by copying it starting from index 1.
36+
return opts
37+
38+
def process_images(recognition_id_filename):
39+
data = []
40+
with open(recognition_id_filename) as file:
41+
data = file.readlines()
42+
results = {}
43+
for each in data:
44+
image_name = each.split(",")[0].strip()
45+
recognition_id = each.split(",")[-1].strip("\n").strip()
46+
results[image_name] = microsoft_phase_2(recognition_id)
47+
return results
48+
49+
50+
if __name__ == '__main__':
51+
from sys import argv
52+
input_args = getopts(argv)
53+
results = []
54+
if '-i' in input_args and '-o' in input_args:
55+
recognition_ids_filename = input_args['-i']
56+
results = process_images(recognition_ids_filename)
57+
output_path = input_args['-o']
58+
with open(output_path, "w") as write_file:
59+
json.dump(results, write_file)
60+
else:
61+
print("Usage: python microsoft-phase-2.py [-i path to recognition IDs file] [-o output path]")

0 commit comments

Comments
 (0)