-
Notifications
You must be signed in to change notification settings - Fork 1
/
visionapi.py
106 lines (90 loc) · 3.33 KB
/
visionapi.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
#!/usr/bin/env python
"""
Uses the Google Cloud Vision API,
for the annalysis of image
--important note--
as we have the
"""
#importing all the neccesarry files
import argparse
import base64
import csv
import httplib2
import datetime
import json
import os
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
import logging
logging.basicConfig(filename='debug.log',level=logging.DEBUG)
#for processing image
def process_images(image_input):
image_exts = ['.bmp', '.gif', '.jpg', '.jpeg', '.png']
if image_input[-1] == "/":
dir_name = image_input
for file_name in os.listdir(dir_name):
ext = os.path.splitext(file_name)
if file_name not in ignore_files and ext[1].lower() in image_exts and not os.path.isdir(fn):
print(file_name)
main(dir_name + file_name)
else:
print(image_input)
main(image_input)
def main(photo_file):
"""Run a request on a single image"""
API_DISCOVERY_FILE = 'https://vision.googleapis.com/$discovery/rest?version=v1'
http = httplib2.Http()
credentials = GoogleCredentials.get_application_default().create_scoped(
['https://www.googleapis.com/auth/cloud-platform'])
credentials.authorize(http)
service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE)
with open(photo_file, 'rb') as image:
image_content = base64.b64encode(image.read())
service_request = service.images().annotate(
body={
'requests': [{
'image': {
'content': image_content
},
'features': [{
'type': 'LABEL_DETECTION',
'maxResults': 20,
},
{
'type': 'TEXT_DETECTION',
'maxResults': 20,
}]
}]
})
response = service_request.execute()
# Prepare parsing of responses into relevant fields
query = photo_file
all_labels = ''
all_text = ''
try:
labels = response['responses'][0]['labelAnnotations']
for label in labels:
# label = response['responses'][0]['labelAnnotations'][0]['description']
label_val = label['description']
score = str(label['score'])
print('Found label: "%s" with score %s' % (label_val, score))
all_labels += label_val.encode('utf-8') + ' @ ' + score + ', '
except KeyError:
print("N/A labels found")
print('\n')
try:
texts = response['responses'][0]['textAnnotations']
for text in texts:
# text = response['responses'][0]['textAnnotations'][0]['description']
text_val = text['description']
print('Found text: "%s"' % text_val)
all_text += text_val.encode('utf-8') + ', '
except KeyError:
print("N/A text found")
print('\nIMAGE PROCESSING DONE\n')
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('image_input', help='The folder containing images or the image you\'d like to query')
args = parser.parse_args()
process_images(args.image_input)