index.py 856 Bytes
Newer Older
Zhongfu Bai's avatar
Zhongfu Bai committed
1
2
from google.cloud import vision as gcv
from google.cloud.vision import types
Zhongfu Bai's avatar
Zhongfu Bai committed
3
4
import flask
import vision
Zhongfu Bai's avatar
Zhongfu Bai committed
5
6
import io
import os
Zhongfu Bai's avatar
Zhongfu Bai committed
7

Zhongfu Bai's avatar
Zhongfu Bai committed
8
os.environ['GOOGLE_APPLICATION_CREDENTIALS']='vision/gcv project-b03618cdc671.json'
Zhongfu Bai's avatar
Zhongfu Bai committed
9
10
11
12
13

@vision.app.route('/')
def show_index():
    """Display / route."""
    context = {}
Zhongfu Bai's avatar
Zhongfu Bai committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    # Instantiates a client
    client = gcv.ImageAnnotatorClient()

    # read image
    file_name = os.path.abspath('vision/public/favicon.ico')
    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()
    image = types.Image(content=content)

    # Performs label detection on the image file
    response = client.label_detection(image=image)
    labels = response.label_annotations

    print('Labels:')
    for label in labels:
        print(label.description)
        
Zhongfu Bai's avatar
Zhongfu Bai committed
31
    return flask.render_template("index.html", **context)