In [1]:
import IPython.display
IPython.display.YouTubeVideo('FPgo-hI7OiE')


Out[1]:

如何使用和开发微信聊天机器人的系列教程

A workshop to develop & use an intelligent and interactive chat-bot in WeChat

by: GU Zhan (Sam)

October 2018 : Update to support Python 3 in local machine, e.g. iss-vm.

April 2017 ======= Scan the QR code to become trainer's friend in WeChat =====>>

第二课:图像识别和处理

Lesson 2: Image Recognition & Processing

  • 识别图片消息中的物体名字 (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)

Using Google Cloud Platform's Machine Learning APIs


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:

    **
  1. Google Cloud Vision API
  2. **

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

Install the client library


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

Before proceeding, ensure to ENABLE this cloud API, e.g. via web console https://console.cloud.google.com


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")

Use cloud APIs in native forms:


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')


Labels:
text
web page
font
product
product
line
software
website
media
online advertising
Out[8]:
[mid: "/m/07s6nbt"
description: "text"
score: 0.9207388162612915
topicality: 0.9207388162612915
, mid: "/m/086nh"
description: "web page"
score: 0.8943907618522644
topicality: 0.8943907618522644
, mid: "/m/03gq5hm"
description: "font"
score: 0.7709308862686157
topicality: 0.7709308862686157
, mid: "/m/02n3pb"
description: "product"
score: 0.7523669600486755
topicality: 0.7523669600486755
, mid: "/m/01jwgf"
description: "product"
score: 0.7448583245277405
topicality: 0.7448583245277405
, mid: "/m/03scnj"
description: "line"
score: 0.6943584680557251
topicality: 0.6943584680557251
, mid: "/m/01mf0"
description: "software"
score: 0.6695477962493896
topicality: 0.6695477962493896
, mid: "/m/085n4"
description: "website"
score: 0.6695384383201599
topicality: 0.6695384383201599
, mid: "/m/03qh03g"
description: "media"
score: 0.6557212471961975
topicality: 0.6557212471961975
, mid: "/m/05b1rx"
description: "online advertising"
score: 0.6428131461143494
topicality: 0.6428131461143494
]

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')


Landmarks:
Statue of Liberty
Latitude 40.689261
Longitude -74.044482

In [12]:
didi_landmark_detection('image/ISSGZ.png')


Landmarks:

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')


Logos:
NUS Business School

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')


Texts:

"NUS
National University
of Singapore
NLE Course Finder Contact Us Login : Sign Up
INSTITUTE OF SYSTEMS SCIENCE
Home
Executive Education ▼
Graduate Programmes ▼
Stackable Programmes ▼
Centres of Excellence ▼
Community▼
About Us▼
A- A A+
GU Zhan
Profile
GU Zhan (Sam) lectures Master of Technology programme in the areas of data science, machine intelligence, soft
computing, and applied deep learning. Prior to joining ISS, he was in New Zealand running Kudos Data start-up,
which focussed on training programs to democratize artificial intelligence and machine learning. Before that, he
was a data scientist in Yokogawa Engineering, leading industrial innovation initiatives. Sam had also spent many
years in financial sector wearing versatile hats: project manager, consultant, and business analyst in Barclays bank,
system manager and senior software engineer at Citibank, leading to experience in actualizing information
technology values in complex business environment
Lecturer & Consultant,
Analytics & Intelligent
Systems Practice
He devotes himself into pedagogy, and is very passionate in inspiring next generation of artificial intelligence lovers
and leaders
issgz@nus.edu.sg
Educational Ọualifications/Professional Certifications
Academic and Professional Experience
"
bounds: (128,29),(1772,29),(1772,1053),(128,1053)

"NUS"
bounds: (218,29),(339,29),(339,80),(218,80)

"National"
bounds: (217,88),(273,88),(273,99),(217,99)

"University"
bounds: (279,88),(346,88),(346,102),(279,102)

"of"
bounds: (217,105),(229,105),(229,116),(217,116)

"Singapore"
bounds: (234,105),(303,105),(303,119),(234,119)

"NLE"
bounds: (1216,88),(1258,88),(1258,102),(1216,102)

"Course"
bounds: (1282,88),(1348,88),(1348,102),(1282,102)

"Finder"
bounds: (1355,87),(1414,87),(1414,102),(1355,102)

"Contact"
bounds: (1436,88),(1508,88),(1508,102),(1436,102)

"Us"
bounds: (1515,88),(1539,88),(1539,102),(1515,102)

"Login"
bounds: (1592,87),(1641,87),(1641,106),(1592,106)

":"
bounds: (1665,84),(1680,84),(1680,106),(1665,106)

"Sign"
bounds: (1695,88),(1735,88),(1735,106),(1695,106)

"Up"
bounds: (1743,88),(1769,88),(1769,106),(1743,106)

"INSTITUTE"
bounds: (392,129),(458,129),(458,137),(392,137)

"OF"
bounds: (466,129),(481,129),(481,137),(466,137)

"SYSTEMS"
bounds: (488,129),(547,129),(547,137),(488,137)

"SCIENCE"
bounds: (555,129),(607,129),(607,137),(555,137)

"Home"
bounds: (128,200),(181,200),(181,214),(128,214)

"Executive"
bounds: (213,199),(302,199),(302,214),(213,214)

"Education"
bounds: (311,199),(401,199),(401,214),(311,214)

"▼"
bounds: (406,203),(417,203),(417,210),(406,210)

"Graduate"
bounds: (448,199),(533,199),(533,214),(448,214)

"Programmes"
bounds: (542,200),(660,200),(660,217),(542,217)

"▼"
bounds: (664,203),(675,203),(675,209),(664,209)

"Stackable"
bounds: (707,199),(797,199),(797,214),(707,214)

"Programmes"
bounds: (805,200),(923,200),(923,217),(805,217)

"▼"
bounds: (927,203),(938,203),(938,210),(927,210)

"Centres"
bounds: (970,200),(1042,200),(1042,214),(970,214)

"of"
bounds: (1049,199),(1066,199),(1066,214),(1049,214)

"Excellence"
bounds: (1073,199),(1172,199),(1172,214),(1073,214)

"▼"
bounds: (1176,203),(1187,203),(1187,209),(1176,209)

"Community"
bounds: (1219,199),(1323,199),(1323,217),(1219,217)

"▼"
bounds: (1327,203),(1338,203),(1338,209),(1327,209)

"About"
bounds: (1369,200),(1424,200),(1424,214),(1369,214)

"Us"
bounds: (1431,200),(1455,200),(1455,214),(1431,214)

"▼"
bounds: (1459,203),(1470,203),(1470,209),(1459,209)

"A-"
bounds: (1584,196),(1605,196),(1605,217),(1584,217)

"A"
bounds: (1614,196),(1640,196),(1640,217),(1614,217)

"A+"
bounds: (1660,196),(1694,196),(1694,217),(1660,217)

"GU"
bounds: (132,312),(238,312),(238,366),(132,366)

"Zhan"
bounds: (265,310),(434,310),(434,366),(265,366)

"Profile"
bounds: (556,477),(721,477),(721,519),(556,519)

"GU"
bounds: (552,573),(585,573),(585,590),(552,590)

"Zhan"
bounds: (593,573),(646,573),(646,590),(593,590)

"(Sam)"
bounds: (655,573),(718,573),(718,595),(655,595)

"lectures"
bounds: (727,573),(809,573),(809,590),(727,590)

"Master"
bounds: (819,573),(890,573),(890,590),(819,590)

"of"
bounds: (897,573),(917,573),(917,590),(897,590)

"Technology"
bounds: (924,573),(1045,573),(1045,595),(924,595)

"programme"
bounds: (1053,577),(1174,577),(1174,595),(1053,595)

"in"
bounds: (1183,573),(1198,573),(1198,590),(1183,590)

"the"
bounds: (1207,573),(1239,573),(1239,590),(1207,590)

"areas"
bounds: (1247,577),(1306,577),(1306,590),(1247,590)

"of"
bounds: (1314,573),(1333,573),(1333,590),(1314,590)

"data"
bounds: (1341,574),(1386,574),(1386,590),(1341,590)

"science,"
bounds: (1394,573),(1479,573),(1479,592),(1394,592)

"machine"
bounds: (1490,573),(1578,573),(1578,590),(1490,590)

"intelligence,"
bounds: (1587,573),(1710,573),(1710,595),(1587,595)

"soft"
bounds: (1719,573),(1758,573),(1758,590),(1719,590)

"computing,"
bounds: (552,609),(670,609),(670,630),(552,630)

"and"
bounds: (680,609),(712,609),(712,628),(680,628)

"applied"
bounds: (722,609),(797,609),(797,630),(722,630)

"deep"
bounds: (806,609),(857,609),(857,630),(806,630)

"learning."
bounds: (866,609),(955,609),(955,630),(866,630)

"Prior"
bounds: (965,609),(1014,609),(1014,625),(965,625)

"to"
bounds: (1021,610),(1040,610),(1040,625),(1021,625)

"joining"
bounds: (1047,609),(1116,609),(1116,630),(1047,630)

"ISS,"
bounds: (1125,609),(1167,609),(1167,625),(1125,625)

"he"
bounds: (1177,609),(1201,609),(1201,625),(1177,625)

"was"
bounds: (1209,613),(1251,613),(1251,625),(1209,625)

"in"
bounds: (1259,609),(1275,609),(1275,625),(1259,625)

"New"
bounds: (1285,609),(1331,609),(1331,625),(1285,625)

"Zealand"
bounds: (1339,609),(1423,609),(1423,625),(1339,625)

"running"
bounds: (1433,609),(1510,609),(1510,630),(1433,630)

"Kudos"
bounds: (1520,609),(1585,609),(1585,625),(1520,625)

"Data"
bounds: (1595,609),(1643,609),(1643,625),(1595,625)

"start-up,"
bounds: (1651,610),(1736,610),(1736,630),(1651,630)

"which"
bounds: (551,645),(610,645),(610,661),(551,661)

"focussed"
bounds: (618,645),(713,645),(713,661),(618,661)

"on"
bounds: (721,649),(746,649),(746,661),(721,661)

"training"
bounds: (754,645),(831,645),(831,666),(754,666)

"programs"
bounds: (841,649),(940,649),(940,666),(841,666)

"to"
bounds: (948,646),(967,646),(967,661),(948,661)

"democratize"
bounds: (975,645),(1103,645),(1103,661),(975,661)

"artificial"
bounds: (1112,645),(1193,645),(1193,661),(1112,661)

"intelligence"
bounds: (1199,645),(1319,645),(1319,666),(1199,666)

"and"
bounds: (1327,645),(1364,645),(1364,661),(1327,661)

"machine"
bounds: (1374,645),(1458,645),(1458,661),(1374,661)

"learning."
bounds: (1467,645),(1560,645),(1560,666),(1467,666)

"Before"
bounds: (1577,645),(1645,645),(1645,661),(1577,661)

"that,"
bounds: (1653,645),(1697,645),(1697,664),(1653,664)

"he"
bounds: (1707,645),(1732,645),(1732,661),(1707,661)

"was"
bounds: (551,685),(592,685),(592,698),(551,698)

"a"
bounds: (600,685),(613,685),(613,698),(600,698)

"data"
bounds: (620,681),(666,681),(666,698),(620,698)

"scientist"
bounds: (674,681),(759,681),(759,698),(674,698)

"in"
bounds: (767,681),(784,681),(784,698),(767,698)

"Yokogawa"
bounds: (792,681),(901,681),(901,702),(792,702)

"Engineering,"
bounds: (910,681),(1041,681),(1041,702),(910,702)

"leading"
bounds: (1051,681),(1122,681),(1122,702),(1051,702)

"industrial"
bounds: (1130,681),(1230,681),(1230,702),(1130,702)

"innovation"
bounds: (1236,681),(1344,681),(1344,698),(1236,698)

"initiatives."
bounds: (1353,681),(1456,681),(1456,698),(1353,698)

"Sam"
bounds: (1472,681),(1519,681),(1519,698),(1472,698)

"had"
bounds: (1528,681),(1565,681),(1565,698),(1528,698)

"also"
bounds: (1574,681),(1617,681),(1617,698),(1574,698)

"spent"
bounds: (1625,682),(1683,682),(1683,702),(1625,702)

"many"
bounds: (1691,685),(1748,685),(1748,702),(1691,702)

"years"
bounds: (551,721),(608,721),(608,738),(551,738)

"in"
bounds: (617,717),(633,717),(633,734),(617,734)

"financial"
bounds: (641,717),(727,717),(727,734),(641,734)

"sector"
bounds: (736,718),(800,718),(800,734),(736,734)

"wearing"
bounds: (807,717),(890,717),(890,738),(807,738)

"versatile"
bounds: (898,717),(986,717),(986,734),(898,734)

"hats:"
bounds: (996,717),(1044,717),(1044,734),(996,734)

"project"
bounds: (1054,717),(1125,717),(1125,738),(1054,738)

"manager,"
bounds: (1133,721),(1234,721),(1234,738),(1133,738)

"consultant,"
bounds: (1243,717),(1352,717),(1352,736),(1243,736)

"and"
bounds: (1361,717),(1400,717),(1400,734),(1361,734)

"business"
bounds: (1409,717),(1501,717),(1501,734),(1409,734)

"analyst"
bounds: (1510,717),(1585,717),(1585,738),(1510,738)

"in"
bounds: (1593,717),(1609,717),(1609,734),(1593,734)

"Barclays"
bounds: (1618,717),(1708,717),(1708,738),(1618,738)

"bank,"
bounds: (1717,717),(1772,717),(1772,736),(1717,736)

"system"
bounds: (552,754),(625,754),(625,774),(552,774)

"manager"
bounds: (634,757),(727,757),(727,774),(634,774)

"and"
bounds: (735,753),(773,753),(773,769),(735,769)

"senior"
bounds: (781,753),(846,753),(846,769),(781,769)

"software"
bounds: (853,753),(942,753),(942,769),(853,769)

"engineer"
bounds: (951,753),(1043,753),(1043,774),(951,774)

"at"
bounds: (1051,754),(1070,754),(1070,769),(1051,769)

"Citibank,"
bounds: (1078,753),(1168,753),(1168,772),(1078,772)

"leading"
bounds: (1178,753),(1252,753),(1252,774),(1178,774)

"to"
bounds: (1261,754),(1280,754),(1280,769),(1261,769)

"experience"
bounds: (1288,753),(1404,753),(1404,774),(1288,774)

"in"
bounds: (1413,753),(1429,753),(1429,769),(1413,769)

"actualizing"
bounds: (1437,753),(1549,753),(1549,774),(1437,774)

"information"
bounds: (1558,753),(1674,753),(1674,769),(1558,769)

"technology"
bounds: (551,789),(666,789),(666,810),(551,810)

"values"
bounds: (673,789),(742,789),(742,806),(673,806)

"in"
bounds: (750,789),(766,789),(766,806),(750,806)

"complex"
bounds: (775,789),(863,789),(863,810),(775,810)

"business"
bounds: (871,789),(964,789),(964,806),(871,806)

"environment"
bounds: (972,789),(1104,789),(1104,806),(972,806)

"Lecturer"
bounds: (129,835),(246,835),(246,855),(129,855)

"&"
bounds: (256,835),(276,835),(276,855),(256,855)

"Consultant,"
bounds: (286,833),(447,833),(447,859),(286,859)

"Analytics"
bounds: (128,869),(259,869),(259,896),(128,896)

"&"
bounds: (269,870),(289,870),(289,891),(269,891)

"Intelligent"
bounds: (300,869),(440,869),(440,896),(300,896)

"Systems"
bounds: (128,907),(249,907),(249,927),(128,927)

"Practice"
bounds: (260,905),(374,905),(374,927),(260,927)

"He"
bounds: (552,855),(580,855),(580,872),(552,872)

"devotes"
bounds: (588,855),(671,855),(671,872),(588,872)

"himself"
bounds: (679,855),(754,855),(754,872),(679,872)

"into"
bounds: (762,859),(798,859),(798,872),(762,872)

"pedagogy,"
bounds: (808,855),(919,855),(919,876),(808,876)

"and"
bounds: (929,855),(961,855),(961,874),(929,874)

"is"
bounds: (971,855),(986,855),(986,872),(971,872)

"very"
bounds: (994,859),(1039,859),(1039,876),(994,876)

"passionate"
bounds: (1047,855),(1160,855),(1160,876),(1047,876)

"in"
bounds: (1169,855),(1186,855),(1186,872),(1169,872)

"inspiring"
bounds: (1195,855),(1281,855),(1281,876),(1195,876)

"next"
bounds: (1291,856),(1335,856),(1335,872),(1291,872)

"generation"
bounds: (1342,855),(1453,855),(1453,876),(1342,876)

"of"
bounds: (1462,855),(1482,855),(1482,872),(1462,872)

"artificial"
bounds: (1489,855),(1570,855),(1570,872),(1489,872)

"intelligence"
bounds: (1576,855),(1691,855),(1691,876),(1576,876)

"lovers"
bounds: (1700,855),(1766,855),(1766,872),(1700,872)

"and"
bounds: (552,891),(589,891),(589,907),(552,907)

"leaders"
bounds: (598,891),(675,891),(675,907),(598,907)

"issgz@nus.edu.sg"
bounds: (174,969),(368,969),(368,990),(174,990)

"Educational"
bounds: (575,969),(696,969),(696,986),(575,986)

"Ọualifications"
bounds: (705,969),(848,969),(848,990),(705,990)

"/"
bounds: (849,969),(855,969),(855,986),(849,986)

"Professional"
bounds: (857,969),(986,969),(986,986),(857,986)

"Certifications"
bounds: (995,969),(1132,969),(1132,986),(995,986)

"Academic"
bounds: (574,1035),(677,1035),(677,1048),(574,1048)

"and"
bounds: (686,1035),(723,1035),(723,1048),(686,1048)

"Professional"
bounds: (733,1035),(862,1035),(862,1048),(733,1048)

"Experience"
bounds: (871,1035),(989,1035),(989,1053),(871,1053)

Face Detection : Output new image with framed face(s)

https://cloud.google.com/vision/docs/face-tutorial


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')


Found 10 faces
Writing to file image//ISS-testimony-sentimentDetectedFace.png

In [23]:
IPython.display.Image(filename='image//ISS-testimony-sentimentDetectedFace.png')


Out[23]:

Face Detection : Identify sentiment and emotion from human face

https://cloud.google.com/vision/docs/detecting-faces


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')


Found 10 faces  : Emotion 
  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (250,125),(583,125),(583,512),(250,512)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_UNLIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (421,865),(501,865),(501,958),(421,958)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (148,906),(224,906),(224,995),(148,995)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (789,852),(903,852),(903,985),(789,985)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (610,888),(677,888),(677,966),(610,966)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (686,899),(757,899),(757,981),(686,981)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_UNLIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (1200,869),(1267,869),(1267,946),(1200,946)

  * anger       : VERY_UNLIKELY
  * joy         : LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (352,876),(402,876),(402,934),(352,934)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (265,892),(316,892),(316,951),(265,951)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_UNLIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (1140,871),(1203,871),(1203,944),(1140,944)


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]:

Safe Search Detection

Safe Search Detection detects explicit content such as adult content or violent content within an image.

https://cloud.google.com/vision/docs/detecting-safe-search


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')


Safe search:
  * adult    : VERY_UNLIKELY
  * medical  : VERY_UNLIKELY
  * spoofed  : VERY_UNLIKELY
  * violence : VERY_UNLIKELY
  * racy     : VERY_UNLIKELY

In [33]:
IPython.display.Image(filename='image/adult0.jpg')


Out[33]:

In [34]:
didi_safe_search_detection('image/adult0.jpg')


Safe search:
  * adult    : UNLIKELY
  * medical  : UNLIKELY
  * spoofed  : VERY_UNLIKELY
  * violence : VERY_UNLIKELY
  * racy     : VERY_LIKELY

https://en.oxforddictionaries.com/definition/racy

Definition of racy in English:

racy

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.


  • 2 (of a vehicle or animal) designed or bred to be suitable for racing.
‘The yacht is fast and racy.’


racy

形容词

  • 1 活泼,有趣,通常是性感的。

     "这部小说在当时被认为是相当活泼的。"

      1.1 表现出活力或精神。

      1.2(葡萄酒,香精等)具有高度特征品质。

  • 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')


Best guess label: first lady of the internet

10 Pages with matching images found:

	Page url   : https://imgur.com/gallery/XXaCRKT
	1 Partial Matches found: 
		Image url  : https://i.imgur.com/XXaCRKT.jpg

	Page url   : https://pssiusa.blog/2018/03/15/the-first-lady-of-the-internet/
	2 Partial Matches found: 
		Image url  : https://pssiusa.files.wordpress.com/2018/03/lenapic.jpg?w=584
		Image url  : https://pssiusa.files.wordpress.com/2018/03/lenapic.jpg

	Page url   : https://www.pinterest.com/pin/54746951690331166/
	1 Partial Matches found: 
		Image url  : https://i.pinimg.com/originals/81/4a/00/814a0034f5549e957ee61360d87457e5.png

	Page url   : https://imgur.com/gallery/KAVjr8b
	1 Partial Matches found: 
		Image url  : https://i.imgur.com/KAVjr8b.jpg

	Page url   : http://busy.org/@aimal/did-you-know-the-first-lady-of-internet-lena-or-lenna-safe-for-work
	1 Partial Matches found: 
		Image url  : https://steemitimages.com/0x0/https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png

	Page url   : https://steemit.com/playboy/@aimal/did-you-know-the-first-lady-of-internet-lena-or-lenna-safe-for-work
	1 Partial Matches found: 
		Image url  : https://steemitimages.com/0x0/https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png

	Page url   : https://www.pinterest.co.uk/pin/167266573630417915/
	1 Partial Matches found: 
		Image url  : https://i.pinimg.com/originals/81/4a/00/814a0034f5549e957ee61360d87457e5.png

	Page url   : https://www.ign.com/boards/threads/this-was-the-first-photo-uploaded-on-the-internet.452631464/
	1 Partial Matches found: 
		Image url  : http://assets.motherboard.tv/post_images/assets/000/013/409/Lenna_original.jpg?1345236372

	Page url   : https://steemit.com/playboy/@aimal/did-you-know-the-first-lady-of-internet-lena-or-lenna-safe-for-work?sort=new
	1 Partial Matches found: 
		Image url  : https://steemitimages.com/0x0/https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png

	Page url   : https://steemit.com/playboy/@aimal/did-you-know-the-first-lady-of-internet-lena-or-lenna-safe-for-work?sort=author_reputation
	1 Partial Matches found: 
		Image url  : https://steemitimages.com/0x0/https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png

10 Web entities found: 

	Score       : 8.652
	Description : Lena Söderberg

	Score       : 0.725
	Description : Lenna

	Score       : 0.664
	Description : Internet

	Score       : 0.525
	Description : Standard test image

	Score       : 0.519
	Description : Playboy

	Score       : 0.487
	Description : Image

	Score       : 0.443
	Description : JPEG

	Score       : 0.386
	Description : Photograph

	Score       : 0.378
	Description : 

	Score       : 0.342
	Description : China Girl

10 visually similar images found:

	Image url    : https://i.pinimg.com/originals/81/4a/00/814a0034f5549e957ee61360d87457e5.png
	Image url    : https://pbs.twimg.com/media/DJjGHANUEAADO2c.jpg
	Image url    : https://static.pulse.ng/img/incoming/origs3594017/1921853270-w640-h640/Chantal-Biya-Selfie.jpg
	Image url    : https://static1.squarespace.com/static/5862cbadbebafb9764eb8ad9/t/58c0271d1b631b30c79df428/1488988024120/
	Image url    : https://www.bet.com/style/living/2017/10/20/the-internet-reacts-to-the-playboy-s-first-transgender-playmate/_jcr_content/image.medium2x1image.dimg/__1508523093197__1508521293134/102017-style-living-the-internet-reacts-to-the-playboy-s-first-transgender-playmate.jpg
	Image url    : http://besidemag.co.uk/wp-content/uploads/2016/08/nerve-review-2016.jpg
	Image url    : https://media.guestofaguest.com/t_article_content/wp-content/uploads/2011/05/kreayshawn.jpg
	Image url    : https://i2-prod.mirror.co.uk/incoming/article4670855.ece/ALTERNATES/s615b/Liz-Kershaw.jpg
	Image url    : http://1.bp.blogspot.com/-0k178axY2nM/Txr2zI1tUpI/AAAAAAAAAd8/J1664Btg3cY/s1600/Most-Beautiful-Woman-In-The-World-Ever-Photos-02.jpg
	Image url    : http://cdn.extra.ie/wp-content/uploads/2017/02/03120934/Milana-First-Dates-Ireland-1.jpg

Wrap cloud APIs into Functions() for conversational virtual assistant (VA):

* 识别图片消息中的物体名字 (Recognize objects in image)

[1] 物体名 (General Object)

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]: