In [1]:
import IPython.display
IPython.display.YouTubeVideo('FPgo-hI7OiE')
Out[1]:
识别图片消息中的物体名字 (Recognize objects in image)
[1] 物体名 (General Object)
[2] 地标名 (Landmark Object)
[3] 商标名 (Logo Object)
识别图片消息中的文字 (OCR: Extract text from image)
识别图片消息中的文字 (OCR: Extract text from image)
识别人脸 (Recognize human face)
人脸检测和画框 (Output new image with framed face)
人脸表情识别: 喜怒哀乐等情绪 (Identify sentiment and emotion from human face)
不良内容识别 (Explicit Content Detection)
线上实体检测 (Detecting Web Entities and Pages)
In [2]:
# [ Video Guide ] S IPA Create Google Cloud Account and APIs 1:
IPython.display.YouTubeVideo('ObVORs_UGhs')
Out[2]:
In [3]:
# [ Video Guide ] S IPA Create Google Cloud Account and APIs 2:
IPython.display.YouTubeVideo('DJVURzM3_ZM')
Out[3]:
From the same API console, choose "Dashboard" on the left-hand menu and "Enable API".
Enable the following APIs for your project (search for them) if they are not already enabled:
Finally, because we are calling the APIs from Python (clients in many other languages are available), let's install the Python package (it's not installed by default on Datalab)
In [2]:
# Copyright 2016 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# !pip install --upgrade google-api-python-client
In [3]:
!pip install --upgrade google-cloud-vision
In [2]:
import io
import os
# import IPython.display
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
In [5]:
# Vision API
# (1) Instantiates a client - using GOOGLE_APPLICATION_CREDENTIALS
# client = vision.ImageAnnotatorClient()
# (2) Instantiates a client - using 'service account json' file
client = vision.ImageAnnotatorClient.from_service_account_json(
"/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
In [6]:
def didi_label_detection(image_file, max_results=4):
"""Uses the Vision API to detect labels in the given file.
Args:
face_file: A file-like object containing an image with faces.
Returns:
An array of lABEL objects with information about the picture.
"""
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
# Loads the image into memory
# with open(image_file, 'rb') as image:
with io.open(image_file, 'rb') as image:
content = image.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)
# return client.label_detection(image=image).label_annotations
return labels
In [7]:
IPython.display.Image(filename='image/ISSGZ.png')
Out[7]:
In [8]:
didi_label_detection('image/ISSGZ.png')
Out[8]:
In [9]:
def didi_landmark_detection(image):
"""Detects landmarks in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.landmark_detection(image=image)
landmarks = response.landmark_annotations
print('Landmarks:')
for landmark in landmarks:
print(landmark.description)
for location in landmark.locations:
lat_lng = location.lat_lng
print('Latitude {}'.format(lat_lng.latitude))
print('Longitude {}'.format(lat_lng.longitude))
In [10]:
IPython.display.Image(filename='image/new-york-statue-of-liberty.jpg')
Out[10]:
In [11]:
didi_landmark_detection('image/new-york-statue-of-liberty.jpg')
In [12]:
didi_landmark_detection('image/ISSGZ.png')
In [13]:
def didi_logo_detection(image):
"""Detects logos in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.logo_detection(image=image)
logos = response.logo_annotations
print('Logos:')
for logo in logos:
print(logo.description)
In [14]:
didi_logo_detection('image/ISSGZ.png')
In [15]:
def didi_text_detection(image):
"""Detects text in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')
for text in texts:
print('\n"{}"'.format(text.description))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
In [16]:
didi_text_detection('image/ISSGZ.png')
In [17]:
from PIL import Image, ImageDraw
In [18]:
def detect_face(face_file, max_results=20):
"""Uses the Vision API to detect faces in the given file.
Args:
face_file: A file-like object containing an image with faces.
Returns:
An array of Face objects with information about the picture.
"""
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
content = face_file.read()
image = types.Image(content=content)
return client.face_detection(image=image).face_annotations
In [19]:
def highlight_faces(image, faces, output_filename):
"""Draws a polygon around the faces, then saves to output_filename.
Args:
image: a file containing the image with the faces.
faces: a list of faces found in the file. This should be in the format
returned by the Vision API.
output_filename: the name of the image file to be created, where the
faces have polygons drawn around them.
"""
im = Image.open(image)
draw = ImageDraw.Draw(im)
for face in faces:
box = [(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices]
draw.line(box + [box[0]], width=5, fill='#00ff00')
im.save(output_filename)
In [20]:
def didi_face_detection(input_filename, output_filename='image/DetectedFace.png', max_results=20):
with open(input_filename, 'rb') as image:
faces = detect_face(image, max_results)
print('Found {} face{}'.format(
len(faces), '' if len(faces) == 1 else 's'))
print('Writing to file {}'.format(output_filename))
# Reset the file pointer, so we can read the file again
image.seek(0)
highlight_faces(image, faces, output_filename)
In [21]:
IPython.display.Image(filename='image/ISS-testimony-sentiment.png')
Out[21]:
In [22]:
didi_face_detection('image//ISS-testimony-sentiment.png', 'image//ISS-testimony-sentimentDetectedFace.png')
In [23]:
IPython.display.Image(filename='image//ISS-testimony-sentimentDetectedFace.png')
Out[23]:
In [24]:
def didi_face_detection_emotion(image):
"""Detects faces in an image."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Found {} face{} : Emotion '.format(len(faces), '' if len(faces) == 1 else 's'))
for face in faces:
print(' * anger : {}'.format(likelihood_name[face.anger_likelihood]))
print(' * joy : {}'.format(likelihood_name[face.joy_likelihood]))
print(' * sorrow : {}'.format(likelihood_name[face.sorrow_likelihood]))
print(' * surprise : {}'.format(likelihood_name[face.surprise_likelihood]))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices])
print(' * face bounds : {}\n'.format(','.join(vertices)))
In [26]:
didi_face_detection_emotion('image/ISS-testimony-sentiment.png')
In [27]:
import argparse
from enum import Enum
import io
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image, ImageDraw
class FeatureType(Enum):
PAGE = 1
BLOCK = 2
PARA = 3
WORD = 4
SYMBOL = 5
def draw_boxes(image, bounds, color):
"""Draw a border around the image using the hints in the vector list."""
draw = ImageDraw.Draw(image)
for bound in bounds:
draw.polygon([
bound.vertices[0].x, bound.vertices[0].y,
bound.vertices[1].x, bound.vertices[1].y,
bound.vertices[2].x, bound.vertices[2].y,
bound.vertices[3].x, bound.vertices[3].y], None, color)
return image
def get_document_bounds(image_file, feature):
"""Returns document bounds given an image."""
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
bounds = []
with io.open(image_file, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.document_text_detection(image=image)
document = response.full_text_annotation
# Collect specified feature bounds by enumerating all document features
for page in document.pages:
for block in page.blocks:
for paragraph in block.paragraphs:
for word in paragraph.words:
for symbol in word.symbols:
if (feature == FeatureType.SYMBOL):
bounds.append(symbol.bounding_box)
if (feature == FeatureType.WORD):
bounds.append(word.bounding_box)
if (feature == FeatureType.PARA):
bounds.append(paragraph.bounding_box)
if (feature == FeatureType.BLOCK):
bounds.append(block.bounding_box)
if (feature == FeatureType.PAGE):
bounds.append(block.bounding_box)
# The list `bounds` contains the coordinates of the bounding boxes.
return bounds
def didi_document_text_detection(filein, fileout):
image = Image.open(filein)
bounds = get_document_bounds(filein, FeatureType.PAGE)
draw_boxes(image, bounds, 'blue')
bounds = get_document_bounds(filein, FeatureType.PARA)
draw_boxes(image, bounds, 'red')
bounds = get_document_bounds(filein, FeatureType.WORD)
draw_boxes(image, bounds, 'yellow')
if fileout is not 0:
image.save(fileout)
else:
image.show()
In [28]:
didi_document_text_detection('image/ISS-testimony-sentiment.png', 'image/ISS-testimony-sentimentDocumentText.png')
In [29]:
IPython.display.Image(filename='image/ISS-testimony-sentimentDocumentText.png')
Out[29]:
In [30]:
def didi_safe_search_detection(image):
"""Detects unsafe features in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.safe_search_detection(image=image)
safe = response.safe_search_annotation
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Safe search:')
print(' * adult : {}'.format(likelihood_name[safe.adult]))
print(' * medical : {}'.format(likelihood_name[safe.medical]))
print(' * spoofed : {}'.format(likelihood_name[safe.spoof]))
print(' * violence : {}'.format(likelihood_name[safe.violence]))
print(' * racy : {}'.format(likelihood_name[safe.racy]))
In [31]:
IPython.display.Image(filename='image/normal_valve_39.jpg')
Out[31]:
In [32]:
didi_safe_search_detection('image/normal_valve_39.jpg')
In [33]:
IPython.display.Image(filename='image/adult0.jpg')
Out[33]:
In [34]:
didi_safe_search_detection('image/adult0.jpg')
https://en.oxforddictionaries.com/definition/racy
Definition of racy in English:
ADJECTIVE
1 Lively, entertaining, and typically sexually titillating.
‘The novel was considered rather racy at the time.’
1.1 Showing vigour or spirit.
1.2 (of a wine, flavour, etc.) having a characteristic quality in a high degree.
‘The yacht is fast and racy.’
形容词
"这部小说在当时被认为是相当活泼的。"
1.1 表现出活力或精神。
1.2(葡萄酒,香精等)具有高度特征品质。
"这艘游艇快速而且充满活力。"
In [35]:
def didi_web_detection(image):
"""Detects web annotations given an image."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.web_detection(image=image)
annotations = response.web_detection
if annotations.best_guess_labels:
for label in annotations.best_guess_labels:
print('\nBest guess label: {}'.format(label.label))
if annotations.pages_with_matching_images:
print('\n{} Pages with matching images found:'.format(
len(annotations.pages_with_matching_images)))
for page in annotations.pages_with_matching_images:
print('\n\tPage url : {}'.format(page.url))
if page.full_matching_images:
print('\t{} Full Matches found: '.format(
len(page.full_matching_images)))
for image in page.full_matching_images:
print('\t\tImage url : {}'.format(image.url))
if page.partial_matching_images:
print('\t{} Partial Matches found: '.format(
len(page.partial_matching_images)))
for image in page.partial_matching_images:
print('\t\tImage url : {}'.format(image.url))
if annotations.web_entities:
print('\n{} Web entities found: '.format(
len(annotations.web_entities)))
for entity in annotations.web_entities:
print('\n\tScore : {0:.3f}'.format(entity.score))
print (u'\tDescription : {}'.format(entity.description))
if annotations.visually_similar_images:
print('\n{} visually similar images found:\n'.format(
len(annotations.visually_similar_images)))
for image in annotations.visually_similar_images:
print('\tImage url : {}'.format(image.url))
In [36]:
IPython.display.Image(filename='image/Lenna.png')
Out[36]:
In [37]:
didi_web_detection('image/Lenna.png')
In [38]:
# Running Vision API
# 'LABEL_DETECTION'
def va_didi_label_detection(image_file, API_type='label_detection', maxResults=20):
"""Uses the Vision API to detect labels in the given file.
Args:
face_file: A file-like object containing an image with faces.
Returns:
An array of lABEL objects with information about the picture.
"""
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
# Loads the image into memory
# with open(image_file, 'rb') as image:
with io.open(image_file, 'rb') as image:
content = image.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
image_analysis_reply = u'\n[ ' + API_type + u' 物体识别 ]\n'
# 'LABEL_DETECTION'
if labels[0].description != "":
for label in labels:
# Debug starts
print(label.description)
# Debug ends
image_analysis_reply += '( ' + str("{0:.3f}".format(label.score)) + ' ' + label.description + ' )\n'
else:
image_analysis_reply += u'[ Nill 无结果 ]\n'
return image_analysis_reply
In [39]:
IPython.display.Image(filename='image/new-york-statue-of-liberty.jpg')
Out[39]:
In [40]:
image_analysis_reply = va_didi_label_detection('image/new-york-statue-of-liberty.jpg')
print(" ")
print("=======================================================")
print("Formatted message is shown below for virtual assistant:")
print("=======================================================")
print(image_analysis_reply)
In [41]:
def va_didi_landmark_detection(image, API_type='landmark_detection', maxResults=20):
"""Detects landmarks in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.landmark_detection(image=image)
landmarks = response.landmark_annotations
print('Landmarks:')
image_analysis_reply = u'\n[ ' + API_type + u' 地标识别 ]\n'
for landmark in landmarks:
print(landmark.description)
image_analysis_reply += '( ' + landmark.description + ' )\n'
for location in landmark.locations:
lat_lng = location.lat_lng
print('Latitude : {}'.format(lat_lng.latitude))
print('Longitude : {}'.format(lat_lng.longitude))
image_analysis_reply += ' ' + '* Latitude : {}'.format(lat_lng.latitude) + '\n'
image_analysis_reply += ' ' + '* Longitude : {}'.format(lat_lng.longitude) + '\n'
return image_analysis_reply
In [42]:
IPython.display.Image(filename='image/new-york-statue-of-liberty.jpg')
Out[42]:
In [43]:
image_analysis_reply = va_didi_landmark_detection('image/new-york-statue-of-liberty.jpg')
print(" ")
print("=======================================================")
print("Formatted message is shown below for virtual assistant:")
print("=======================================================")
print(image_analysis_reply)
In [44]:
def va_didi_logo_detection(image, API_type='logo_detection', maxResults=20):
"""Detects logos in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.logo_detection(image=image)
logos = response.logo_annotations
print('Logos:')
image_analysis_reply = u'\n[ ' + API_type + u' 商标识别 ]\n'
for logo in logos:
print(logo.description)
image_analysis_reply += '( ' + logo.description + ' )\n'
return image_analysis_reply
In [45]:
IPython.display.Image(filename='image/ZhanGu.png')
Out[45]:
In [46]:
image_analysis_reply = va_didi_logo_detection('image/ZhanGu.png')
print(" ")
print("=======================================================")
print("Formatted message is shown below for virtual assistant:")
print("=======================================================")
print(image_analysis_reply)
In [47]:
def va_didi_text_detection(image, API_type='text_detection', maxResults=20):
"""Detects text in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')
image_analysis_reply = u'\n[ ' + API_type + u' 文字提取 ]\n'
for text in texts:
print('\n"{}"'.format(text.description))
image_analysis_reply += '( ' + text.description + ' )\n'
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
return image_analysis_reply
In [48]:
image_analysis_reply = va_didi_text_detection('image/ZhanGu.png')
print(" ")
print("=======================================================")
print("Formatted message is shown below for virtual assistant:")
print("=======================================================")
print(image_analysis_reply)
In [49]:
def va_didi_face_detection_emotion(image, API_type='face_detection_emotion', maxResults=20):
"""Detects faces in an image."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Found {} face{} : Emotion '.format(len(faces), '' if len(faces) == 1 else 's'))
image_analysis_reply = u'\n[ ' + API_type + u' 面部表情 ]\n'
face_count = 0
for face in faces:
print(' * anger : {}'.format(likelihood_name[face.anger_likelihood]))
print(' * joy : {}'.format(likelihood_name[face.joy_likelihood]))
print(' * sorrow : {}'.format(likelihood_name[face.sorrow_likelihood]))
print(' * surprise : {}'.format(likelihood_name[face.surprise_likelihood]))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices])
print(' * face bounds : {}\n'.format(','.join(vertices)))
face_count += 1
image_analysis_reply += u'\n----- No.' + str(face_count) + ' Face -----\n'
image_analysis_reply += u' * Anger 愤怒 : ' \
+ likelihood_name[face.anger_likelihood] + '\n'
image_analysis_reply += u' * Joy 喜悦 : ' \
+ likelihood_name[face.joy_likelihood] + '\n'
image_analysis_reply += u' * Sorrow 悲伤 : ' \
+ likelihood_name[face.sorrow_likelihood] + '\n'
image_analysis_reply += u' * Surprise 惊奇 : ' \
+ likelihood_name[face.surprise_likelihood] + '\n'
return image_analysis_reply
In [50]:
image_analysis_reply = va_didi_face_detection_emotion('image/ZhanGu.png')
print(" ")
print("=======================================================")
print("Formatted message is shown below for virtual assistant:")
print("=======================================================")
print(image_analysis_reply)
In [51]:
def va_didi_safe_search_detection(image, API_type='safe_search_detection', maxResults=20):
"""Detects unsafe features in the file."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.safe_search_detection(image=image)
safe = response.safe_search_annotation
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Safe search:')
print(' * adult : {}'.format(likelihood_name[safe.adult]))
print(' * medical : {}'.format(likelihood_name[safe.medical]))
print(' * spoofed : {}'.format(likelihood_name[safe.spoof]))
print(' * violence : {}'.format(likelihood_name[safe.violence]))
print(' * racy : {}'.format(likelihood_name[safe.racy]))
image_analysis_reply = u'\n[ ' + API_type + u' 受限内容 ]\n'
image_analysis_reply += (' * adult : {}\n'.format(likelihood_name[safe.adult]))
image_analysis_reply += (' * medical : {}\n'.format(likelihood_name[safe.medical]))
image_analysis_reply += (' * spoofed : {}\n'.format(likelihood_name[safe.spoof]))
image_analysis_reply += (' * violence : {}\n'.format(likelihood_name[safe.violence]))
image_analysis_reply += (' * racy : {}\n'.format(likelihood_name[safe.racy]))
return image_analysis_reply
In [52]:
image_analysis_reply = va_didi_safe_search_detection('image/ZhanGu.png')
print(" ")
print("=======================================================")
print("Formatted message is shown below for virtual assistant:")
print("=======================================================")
print(image_analysis_reply)
In [53]:
def va_didi_web_detection(image, API_type='web_detection', maxResults=20):
"""Detects web annotations given an image."""
from google.cloud import vision
##################################################################
# client = vision.ImageAnnotatorClient()
#
# client = vision.ImageAnnotatorClient.from_service_account_json(
# "/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
with io.open(image, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.web_detection(image=image)
annotations = response.web_detection
image_analysis_reply = u'\n[ ' + API_type + u' 线上实体 ]\n'
if annotations.best_guess_labels:
for label in annotations.best_guess_labels:
print('\nBest guess label: {}'.format(label.label))
image_analysis_reply += ('\nBest guess label: {}'.format(label.label))
if annotations.pages_with_matching_images:
print('\n{} Pages with matching images found:'.format(
len(annotations.pages_with_matching_images)))
image_analysis_reply += ('\n{} Pages with matching images found:'.format(
len(annotations.pages_with_matching_images)))
for page in annotations.pages_with_matching_images:
print('\n\tPage url : {}\n'.format(page.url))
image_analysis_reply += ('\n\tPage url : {}\n'.format(page.url))
if page.full_matching_images:
print('\t{} Full Matches found: '.format(
len(page.full_matching_images)))
image_analysis_reply += ('\t{} Full Matches found: '.format(
len(page.full_matching_images)))
for image in page.full_matching_images:
print('\t\tImage url : {}'.format(image.url))
image_analysis_reply += ('\n\t\tImage url : {}'.format(image.url))
if page.partial_matching_images:
print('\t{} Partial Matches found: '.format(
len(page.partial_matching_images)))
image_analysis_reply += ('\t{} Partial Matches found: '.format(
len(page.partial_matching_images)))
for image in page.partial_matching_images:
print('\t\tImage url : {}'.format(image.url))
image_analysis_reply += ('\n\t\tImage url : {}'.format(image.url))
if annotations.web_entities:
print('\n{} Web entities found: '.format(
len(annotations.web_entities)))
image_analysis_reply += ('\n{} Web entities found: '.format(
len(annotations.web_entities)))
for entity in annotations.web_entities:
print('\n\tScore : {0:.3f}'.format(entity.score))
image_analysis_reply += ('\n\tScore : {0:.3f}\n'.format(entity.score))
print (u'\tDescription : {}'.format(entity.description))
image_analysis_reply += (u'\tDescription : {}\n'.format(entity.description))
if annotations.visually_similar_images:
print('\n{} visually similar images found:\n'.format(
len(annotations.visually_similar_images)))
image_analysis_reply += ('\n{} visually similar images found:\n'.format(
len(annotations.visually_similar_images)))
for image in annotations.visually_similar_images:
print('\tImage url : {}'.format(image.url))
image_analysis_reply += ('\tImage url : {}\n'.format(image.url))
return image_analysis_reply
In [54]:
IPython.display.Image(filename='image/Lenna.png')
Out[54]:
In [55]:
image_analysis_reply = va_didi_web_detection('image/Lenna.png')
print(" ")
print("=======================================================")
print("Formatted message is shown below for virtual assistant:")
print("=======================================================")
print(image_analysis_reply)
In [56]:
# import io, os, subprocess, sys, time, datetime, requests, itchat
import itchat
from itchat.content import *
# from googleapiclient.discovery import build
In [57]:
# 2018 Oct: Parameters in this cell are not used for py3_local version at the moment.
# control parameter for Image API:
parm_image_maxResults = 20 # max objects or faces to be extracted from image analysis
# control parameter for Language Translation API:
parm_translation_origin_language = '' # original language in text: to be overwriten by TEXT_DETECTION
parm_translation_target_language = 'zh' # target language for translation: Chinese
In [58]:
# itchat.auto_login(hotReload=True) # hotReload=True: 退出程序后暂存登陆状态。即使程序关闭,一定时间内重新开启也可以不用重新扫码。
itchat.auto_login(enableCmdQR=-2) # enableCmdQR=-2: 命令行显示QR图片
In [60]:
# @itchat.msg_register([PICTURE], isGroupChat=True)
@itchat.msg_register([PICTURE])
def download_files(msg):
parm_translation_origin_language = 'zh' # will be overwriten by TEXT_DETECTION
msg.download(msg.fileName)
print('\n=====================================================')
print('\nDownloaded image file name is: %s' % msg['FileName'])
# 2018 Oct: Manual image file conversion is no longer needed! Pass the file path directly. Yeah!
# image_base64 = encode_image(msg['FileName'])
##############################################################################################################
# call image analysis APIs #
##############################################################################################################
image_analysis_reply = u'[ Image Analysis 图像分析结果 ]\n'
# 1. LABEL_DETECTION:
# image_analysis_reply += va_didi_label_detection(msg['FileName'], 'LABEL_DETECTION', parm_image_maxResults)
image_analysis_reply += va_didi_label_detection(msg['FileName'])
# 2. LANDMARK_DETECTION:
# image_analysis_reply += va_didi_landmark_detection(msg['FileName'], 'LANDMARK_DETECTION', parm_image_maxResults)
image_analysis_reply += va_didi_landmark_detection(msg['FileName'])
# 3. LOGO_DETECTION:
# image_analysis_reply += va_didi_logo_detection(msg['FileName'], 'LOGO_DETECTION', parm_image_maxResults)
image_analysis_reply += va_didi_logo_detection(msg['FileName'])
# 4. TEXT_DETECTION:
# image_analysis_reply += va_didi_text_detection(msg['FileName'], 'TEXT_DETECTION', parm_image_maxResults)
image_analysis_reply += va_didi_text_detection(msg['FileName'])
# 5. FACE_DETECTION:
# image_analysis_reply += va_didi_face_detection_emotion(msg['FileName'], 'FACE_DETECTION', parm_image_maxResults)
image_analysis_reply += va_didi_face_detection_emotion(msg['FileName'])
# 6. SAFE_SEARCH_DETECTION:
# image_analysis_reply += va_didi_safe_search_detection(msg['FileName'], 'SAFE_SEARCH_DETECTION', parm_image_maxResults)
image_analysis_reply += va_didi_safe_search_detection(msg['FileName'])
# 7. WEB_DETECTION:
# image_analysis_reply += va_didi_web_detection(msg['FileName'], 'WEB_DETECTION', parm_image_maxResults)
image_analysis_reply += va_didi_web_detection(msg['FileName'])
print('Compeleted: Image Analysis API ...')
print('=====================================================\n')
return image_analysis_reply
In [61]:
itchat.run()
In [62]:
# interupt kernel, then logout
itchat.logout() # 安全退出
Out[62]:
识别图片消息中的物体名字 (Recognize objects in image)
[1] 物体名 (General Object)
[2] 地标名 (Landmark Object)
[3] 商标名 (Logo Object)
识别图片消息中的文字 (OCR: Extract text from image)
包含简单文本翻译 (Call text translation API)
识别人脸 (Recognize human face)
基于人脸的表情来识别喜怒哀乐等情绪 (Identify sentiment and emotion from human face)
不良内容识别 (Explicit Content Detection)