Created
July 28, 2019 03:24
-
-
Save roycrxtw/96a5ab0793c702c4f1840f84b2b48f86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# coding=utf8 | |
import io | |
import json | |
import os | |
# Imports the Google Cloud client library | |
from google.cloud import vision | |
from google.cloud.vision import types | |
path = 'resources/bike_03.jpg' | |
bike_labels = [ | |
'bicycle', 'bicycle_wheel', 'bicycle_part', 'bicycle_frame', 'bicycle_tire', 'vehicle', | |
'spoke', 'bicycle_saddle', 'bicycle_fork', 'bicycle_drivetrain_part' | |
] | |
# Instantiates a client | |
client = vision.ImageAnnotatorClient() | |
def detect_text(path): | |
print('detect_text() started with path:', path) | |
with io.open(path, 'rb') as image_file: | |
content = image_file.read() | |
# print('content', content) | |
print('Before parse image.') | |
image = types.Image(content=content) | |
# print('image:', image) | |
# difference here | |
response = client.text_detection(image=image) | |
print('response:', response) | |
texts = response.text_annotations | |
print('Texts:') | |
for text in texts: | |
print('描述:', text.description) | |
# end of detect_text | |
# Detects labels in the file | |
def detect_labels(path): | |
print('detect_labels() started.') | |
with io.open(path, 'rb') as image_file: | |
content = image_file.read() | |
image = types.Image(content=content) | |
response = client.label_detection(image=image) | |
labels = response.label_annotations | |
print('detect_labels() label results:', labels) | |
return labels | |
# end of detect_labels() | |
def get_bike_scores(labels): | |
print('labels length:', len(labels)) | |
ret = dict() | |
for label in labels: | |
description = label.description.lower() | |
print('Processing label:', description) | |
if description in bike_labels: | |
print('{} in'.format(description)) | |
ret[description] = label.score | |
else: | |
print('{} not in'.format(description)) | |
return ret | |
# function call here | |
image_labels = detect_labels(path) | |
scores = get_bike_scores(image_labels) | |
print('final score:', scores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment