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

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)


statue
landmark
monument
sky
organism
artwork
classical sculpture
national historic landmark
religion
 
=======================================================
Formatted message is shown below for virtual assistant:
=======================================================

[ label_detection 物体识别 ]
( 0.981  statue )
( 0.950  landmark )
( 0.932  monument )
( 0.798  sky )
( 0.649  organism )
( 0.634  artwork )
( 0.603  classical sculpture )
( 0.601  national historic landmark )
( 0.515  religion )

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

[2] 地标名 (Landmark Object)

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)


Landmarks:
Statue of Liberty
Latitude  : 40.689261
Longitude : -74.044482
 
=======================================================
Formatted message is shown below for virtual assistant:
=======================================================

[ landmark_detection 地标识别 ]
( Statue of Liberty )
  * Latitude  : 40.689261
  * Longitude : -74.044482

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

[3] 商标名 (Logo Object)

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)


Logos:
LinkedIn
 
=======================================================
Formatted message is shown below for virtual assistant:
=======================================================

[ logo_detection 商标识别 ]
( LinkedIn )

* 识别图片消息中的文字 (OCR: Extract text from image)


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)


Texts:

"Linked in
Zhan GU
Data Scientist, PMP
Singapore Information Technology and Services
500+
connections
View Zhan's full profile. It's free!
Your colleagues, classmates, and 400 million other professionals are on Linkedln.
View Zhan's Full Profile
"
bounds: (17,14),(604,14),(604,422),(17,422)

"Linked"
bounds: (17,14),(89,14),(89,32),(17,32)

"in"
bounds: (97,14),(116,14),(116,32),(97,32)

"Zhan"
bounds: (224,107),(280,107),(280,124),(224,124)

"GU"
bounds: (290,107),(323,107),(323,124),(290,124)

"Data"
bounds: (225,135),(255,135),(255,146),(225,146)

"Scientist,"
bounds: (260,135),(319,135),(319,147),(260,147)

"PMP"
bounds: (326,135),(356,135),(356,145),(326,145)

"Singapore"
bounds: (225,153),(278,153),(278,167),(225,167)

"Information"
bounds: (290,153),(352,153),(352,167),(290,167)

"Technology"
bounds: (358,153),(417,153),(417,167),(358,167)

"and"
bounds: (421,153),(441,153),(441,167),(421,167)

"Services"
bounds: (445,153),(491,153),(491,167),(445,167)

"500+"
bounds: (567,107),(604,109),(603,122),(566,120)

"connections"
bounds: (549,126),(602,126),(602,133),(549,133)

"View"
bounds: (50,335),(127,335),(127,363),(50,363)

"Zhan's"
bounds: (136,335),(243,335),(243,363),(136,363)

"full"
bounds: (253,334),(303,334),(303,363),(253,363)

"profile."
bounds: (314,334),(428,334),(428,370),(314,370)

"It's"
bounds: (437,336),(485,336),(485,363),(437,363)

"free!"
bounds: (495,334),(566,334),(566,363),(495,363)

"Your"
bounds: (50,380),(77,380),(77,389),(50,389)

"colleagues,"
bounds: (80,379),(148,379),(148,392),(80,392)

"classmates,"
bounds: (152,379),(223,379),(223,392),(152,392)

"and"
bounds: (228,379),(250,379),(250,389),(228,389)

"400"
bounds: (254,380),(275,380),(275,389),(254,389)

"million"
bounds: (280,379),(321,379),(321,389),(280,389)

"other"
bounds: (325,379),(358,379),(358,389),(325,389)

"professionals"
bounds: (362,379),(442,379),(442,392),(362,392)

"are"
bounds: (447,382),(465,382),(465,389),(447,389)

"on"
bounds: (469,382),(484,382),(484,389),(469,389)

"Linkedln."
bounds: (489,379),(543,379),(543,390),(489,390)

"View"
bounds: (63,414),(86,414),(86,422),(63,422)

"Zhan's"
bounds: (90,414),(123,414),(123,422),(90,422)

"Full"
bounds: (127,414),(145,414),(145,422),(127,422)

"Profile"
bounds: (149,414),(182,414),(182,422),(149,422)
 
=======================================================
Formatted message is shown below for virtual assistant:
=======================================================

[ text_detection 文字提取 ]
( Linked in
Zhan GU
Data Scientist, PMP
Singapore Information Technology and Services
500+
connections
View Zhan's full profile. It's free!
Your colleagues, classmates, and 400 million other professionals are on Linkedln.
View Zhan's Full Profile
 )
( Linked )
( in )
( Zhan )
( GU )
( Data )
( Scientist, )
( PMP )
( Singapore )
( Information )
( Technology )
( and )
( Services )
( 500+ )
( connections )
( View )
( Zhan's )
( full )
( profile. )
( It's )
( free! )
( Your )
( colleagues, )
( classmates, )
( and )
( 400 )
( million )
( other )
( professionals )
( are )
( on )
( Linkedln. )
( View )
( Zhan's )
( Full )
( Profile )

* 人脸检测 (Recognize human face)

* 基于人脸的表情来识别喜怒哀乐等情绪 (Identify sentiment and emotion from human face)


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)


Found 1 face  : Emotion 
  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (53,108),(174,108),(174,249),(53,249)

 
=======================================================
Formatted message is shown below for virtual assistant:
=======================================================

[ face_detection_emotion 面部表情 ]

----- No.1 Face -----
  * Anger 愤怒    : VERY_UNLIKELY
  * Joy 喜悦      : VERY_LIKELY
  * Sorrow 悲伤   : VERY_UNLIKELY
  * Surprise 惊奇 : VERY_UNLIKELY

* 受限内容识别 (Explicit Content Detection)

Detect explicit content like adult content or violent content within an image.


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)


Safe search:
  * adult    : VERY_UNLIKELY
  * medical  : UNLIKELY
  * spoofed  : UNLIKELY
  * violence : VERY_UNLIKELY
  * racy     : VERY_UNLIKELY
 
=======================================================
Formatted message is shown below for virtual assistant:
=======================================================

[ safe_search_detection 受限内容 ]
  * adult    : VERY_UNLIKELY
  * medical  : UNLIKELY
  * spoofed  : UNLIKELY
  * violence : VERY_UNLIKELY
  * racy     : VERY_UNLIKELY

* 线上实体检测 (Detecting Web Entities and Pages)

Web Detection detects Web references to an image.


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)


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
 
=======================================================
Formatted message is shown below for virtual assistant:
=======================================================

[ web_detection 线上实体 ]

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

Start interactive conversational virtual assistant (VA):

Import ItChat, etc. 导入需要用到的一些功能程序库:


In [56]:
# import io, os, subprocess, sys, time, datetime, requests, itchat
import itchat
from itchat.content import *
# from googleapiclient.discovery import build


机器智能API接口控制参数 (Define control parameters for API)


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

Log in using QR code image / 用微信App扫QR码图片来自动登录


In [58]:
# itchat.auto_login(hotReload=True) # hotReload=True: 退出程序后暂存登陆状态。即使程序关闭,一定时间内重新开启也可以不用重新扫码。
itchat.auto_login(enableCmdQR=-2) # enableCmdQR=-2: 命令行显示QR图片


Getting uuid of QR code.
Downloading QR code.
                                                                              
  ██████████████    ██      ████      ██████      ██  ██      ██████████████  
  ██          ██    ██  ████████                    ██        ██          ██  
  ██  ██████  ██  ██████  ██████████  ██    ████    ██  ████  ██  ██████  ██  
  ██  ██████  ██  ██  ██  ██  ████  ██  ████        ████████  ██  ██████  ██  
  ██  ██████  ██    ██      ████  ██  ██  ██    ██  ████      ██  ██████  ██  
  ██          ██      ██    ██████  ██    ██    ██    ██      ██          ██  
  ██████████████  ██  ██  ██  ██  ██  ██  ██  ██  ██  ██  ██  ██████████████  
                    ██  ██    ██████        ████  ████  ████                  
        ████  ████  ██████              ██      ████                ████      
    ████    ██  ████          ██  ██      ████  ██  ██  ████    ████    ██    
    ██    ██████  ██  ██████  ██    ██      ████  ████  ████████          ██  
    ██      ██  ██      ██  ██    ████  ██  ██    ██  ████  ████    ████  ██  
  ██  ████    ████████████      ██  ██  ████            ████  ████    ██████  
        ██      ████    ██████    ██  ██████  ██      ████████    ██  ██      
      ████████████  ██  ██    ██████████  ████    ████  ██  ████  ██████████  
  ██    ██  ██    ██      ██    ████  ██  ██  ██        ██  ██    ████████    
        ████████████  ██    ██████  ██    ██████    ██    ██  ████    ██  ██  
  ████████  ██    ████  ████  ████              ██████  ██  ██  ██      ██    
  ██████  ██  ████  ██  ██    ████  ████████  ██  ██    ██      ██    ██  ██  
    ██  ██        ████  ██  ██  ██  ████    ████  ████      ██      ██        
  ██    ██  ████████████        ████    ██  ████████  ██  ██████  ████        
                    ██████████  ████              ██████████    ████  ████    
  ██████  ██  ████████  ████  ████      ██    ██  ██    ██        ██      ██  
  ████████████      ██    ████  ████    ██████  ████  ████████  ██    ██  ██  
      ██    ██████      ████████  ██  ██  ██████    ████  ██  ████████  ████  
  ██████  ██      ████████      ██████  ██████    ██  ██████  ██  ██    ██    
  ██████  ██  ████  ████    ██    ██  ██████  ██    ████████  ████  ██  ████  
  ██  ██            ████  ██      ██  ██    ████  ██  ██    ██      ████      
  ██          ████████  ██    ████████  ████  ████  ██  ████████████          
                  ██████████    ██    ██    ██    ████    ██      ████████    
  ██████████████  ██████████  ██████  ██        ██        ██  ██  ██████  ██  
  ██          ██      ██    ████    ████    ██    ██████  ██      ████  ██    
  ██  ██████  ██  ████    ██████████  ██  ██    ████    ████████████    ██    
  ██  ██████  ██  ██  ██████        ████    ██        ██████  ██    ██        
  ██  ██████  ██    ██    ██      ████  ██        ████    ██    ████████████  
  ██          ██      ██      ██      ██████████      ██      ██  ██  ██████  
  ██████████████    ██      ██    ████  ██    ██    ██    ██  ██    ██    ██  
                                                                              
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 白黑

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


Start auto replying.
=====================================================

Downloaded image file name is: 181025-185815.png
retail
display window
interior design
Landmarks:
Logos:
Texts:

"ASLANe
Lutong Pinoy 2
SPARKLE
"
bounds: (6,623),(2466,623),(2466,2055),(6,2055)

"ASLANe"
bounds: (2414,2054),(669,1746),(720,1459),(2465,1766)

"Lutong"
bounds: (618,1104),(569,1106),(568,1088),(617,1086)

"Pinoy"
bounds: (564,1107),(522,1109),(521,1090),(563,1088)

"2"
bounds: (517,1110),(507,1110),(506,1094),(516,1094)

"SPARKLE"
bounds: (121,659),(6,649),(9,622),(123,632)
Found 0 faces  : Emotion 
Safe search:
  * adult    : VERY_UNLIKELY
  * medical  : VERY_UNLIKELY
  * spoofed  : VERY_UNLIKELY
  * violence : VERY_UNLIKELY
  * racy     : VERY_UNLIKELY

Best guess label: display window

4 Web entities found: 

	Score       : 0.574
	Description : Window

	Score       : 0.540
	Description : Display window

	Score       : 0.519
	Description : Interior Design Services

	Score       : 0.400
	Description : Design

10 visually similar images found:

	Image url    : http://2.bp.blogspot.com/-7GPqM8zJZfk/TdhoUWoCzbI/AAAAAAAAAMg/HqiB2idP-4w/s1600/P1000314%25281%2529.JPG
	Image url    : https://scontent-atl3-1.cdninstagram.com/vp/09cf901449dd23b51165b1082c1fe661/5C4D696B/t51.2885-15/e35/30954027_154124265429301_5734239492911398912_n.jpg
	Image url    : https://media-cdn.tripadvisor.com/media/photo-s/11/56/72/6a/a-decent-mexican-restaurant.jpg
	Image url    : https://a.1stdibscdn.com/archivesE/upload/f_21403/f_77344331497359343314/_DSC1341_8bit_l.JPG
	Image url    : https://novelnoshesdotcom.files.wordpress.com/2018/01/img_7746.jpg?w=656
	Image url    : https://s3-media4.fl.yelpcdn.com/bphoto/KBVKAJhQ1a68lZ4tAgE9og/o.jpg
	Image url    : x-raw-image:///f752fc63174f791c6cb8fcc1c1a05b1406582127303a4150869d062adb0e9b7a
	Image url    : https://mipcbarato.com/wp-content/uploads/2018/09/interior-design-classes-sacramento-elegant-good-eats-cooking-classes-128-s-amp-17-reviews-cooking.jpg
	Image url    : https://lookaside.fbsbx.com/lookaside/crawler/media/?media_id=1607176342694429
	Image url    : https://s3-media2.fl.yelpcdn.com/bphoto/bAOgeDWuqflvHXTCRY5rWg/o.jpg
Compeleted: Image Analysis API ...
=====================================================


=====================================================

Downloaded image file name is: 181025-190019.png
presentation
text
communication
conversation
vision care
human behavior
glasses
media
collaboration
website
Landmarks:
Logos:
Texts:

"Alvin Khoo, Singapore
Regional IT Director
Royal DSM
Master of Technology in Knowledge Engineering (MTech KE), Class of 1997
"One of the most useful things we learnt at ISS was to be analytical and to be able to
adapt, especially in an industry that is changing so quickly. What ISS gave us was an
invaluable reference point."
Discover Life with Us
"
bounds: (600,58),(1670,58),(1670,824),(600,824)

"Alvin"
bounds: (616,60),(737,60),(737,102),(616,102)

"Khoo,"
bounds: (756,58),(896,58),(896,110),(756,110)

"Singapore"
bounds: (916,58),(1169,58),(1169,113),(916,113)

"Regional"
bounds: (619,125),(714,125),(714,149),(619,149)

"IT"
bounds: (723,125),(744,125),(744,144),(723,144)

"Director"
bounds: (752,125),(838,125),(838,144),(752,144)

"Royal"
bounds: (620,167),(681,167),(681,191),(620,191)

"DSM"
bounds: (689,168),(743,168),(743,187),(689,187)

"Master"
bounds: (620,239),(695,239),(695,258),(620,258)

"of"
bounds: (701,240),(723,240),(723,258),(701,258)

"Technology"
bounds: (728,239),(853,239),(853,264),(728,264)

"in"
bounds: (860,239),(879,239),(879,258),(860,258)

"Knowledge"
bounds: (886,240),(1009,240),(1009,263),(886,263)

"Engineering"
bounds: (1016,238),(1147,238),(1147,263),(1016,263)

"(MTech"
bounds: (1155,237),(1235,237),(1235,261),(1155,261)

"KE),"
bounds: (1244,237),(1291,237),(1291,261),(1244,261)

"Class"
bounds: (1298,237),(1361,237),(1361,256),(1298,256)

"of"
bounds: (1368,236),(1391,236),(1391,255),(1368,255)

"1997"
bounds: (1397,236),(1452,236),(1452,254),(1397,254)

""One"
bounds: (618,313),(682,313),(682,334),(618,334)

"of"
bounds: (690,314),(715,314),(715,334),(690,334)

"the"
bounds: (721,314),(759,314),(759,334),(721,334)

"most"
bounds: (768,315),(828,315),(828,335),(768,335)

"useful"
bounds: (835,312),(909,312),(909,334),(835,334)

"things"
bounds: (916,313),(991,313),(991,339),(916,339)

"we"
bounds: (997,313),(1027,313),(1027,334),(997,334)

"learnt"
bounds: (1038,313),(1112,313),(1112,334),(1038,334)

"at"
bounds: (1118,312),(1142,312),(1142,333),(1118,333)

"ISS"
bounds: (1150,310),(1194,310),(1194,333),(1150,333)

"was"
bounds: (1202,316),(1252,316),(1252,332),(1202,332)

"to"
bounds: (1259,312),(1283,312),(1283,332),(1259,332)

"be"
bounds: (1291,311),(1321,311),(1321,332),(1291,332)

"analytical"
bounds: (1329,310),(1447,310),(1447,337),(1329,337)

"and"
bounds: (1455,308),(1502,308),(1502,331),(1455,331)

"to"
bounds: (1509,310),(1533,310),(1533,330),(1509,330)

"be"
bounds: (1541,308),(1572,308),(1572,330),(1541,330)

"able"
bounds: (1581,308),(1635,308),(1635,330),(1581,330)

"to"
bounds: (1642,309),(1667,309),(1667,330),(1642,330)

"adapt,"
bounds: (621,359),(696,359),(696,385),(621,385)

"especially"
bounds: (705,359),(827,359),(827,386),(705,386)

"in"
bounds: (835,358),(855,358),(855,379),(835,379)

"an"
bounds: (864,363),(894,363),(894,379),(864,379)

"industry"
bounds: (903,358),(1000,358),(1000,385),(903,385)

"that"
bounds: (1007,358),(1054,358),(1054,379),(1007,379)

"is"
bounds: (1062,358),(1081,358),(1081,379),(1062,379)

"changing"
bounds: (1089,357),(1201,357),(1201,384),(1089,384)

"so"
bounds: (1210,363),(1239,363),(1239,379),(1210,379)

"quickly."
bounds: (1246,357),(1336,357),(1336,384),(1246,384)

"What"
bounds: (1346,356),(1412,356),(1412,377),(1346,377)

"ISS"
bounds: (1421,355),(1465,355),(1465,377),(1421,377)

"gave"
bounds: (1472,361),(1534,361),(1534,382),(1472,382)

"us"
bounds: (1542,360),(1572,360),(1572,376),(1542,376)

"was"
bounds: (1580,360),(1631,360),(1631,376),(1580,376)

"an"
bounds: (1639,359),(1670,359),(1670,376),(1639,376)

"invaluable"
bounds: (621,405),(745,405),(745,427),(621,427)

"reference"
bounds: (753,404),(870,404),(870,427),(753,427)

"point.""
bounds: (878,404),(953,404),(953,431),(878,431)

"Discover"
bounds: (600,776),(840,776),(840,824),(600,824)

"Life"
bounds: (860,775),(956,775),(956,824),(860,824)

"with"
bounds: (975,776),(1085,776),(1085,824),(975,824)

"Us"
bounds: (1107,776),(1179,776),(1179,824),(1107,824)
Found 9 faces  : Emotion 
  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (167,104),(509,104),(509,502),(167,502)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (70,910),(147,910),(147,999),(70,999)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_UNLIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (348,866),(430,866),(430,961),(348,961)

  * anger       : VERY_UNLIKELY
  * joy         : LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (540,889),(610,889),(610,970),(540,970)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (733,868),(837,868),(837,989),(733,989)

  * anger       : VERY_UNLIKELY
  * joy         : LIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (621,908),(691,908),(691,989),(621,989)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_UNLIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (1145,866),(1214,866),(1214,946),(1145,946)

  * anger       : VERY_UNLIKELY
  * joy         : POSSIBLE
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (187,894),(239,894),(239,954),(187,954)

  * anger       : VERY_UNLIKELY
  * joy         : VERY_UNLIKELY
  * sorrow      : VERY_UNLIKELY
  * surprise    : VERY_UNLIKELY
  * face bounds : (1081,873),(1144,873),(1144,946),(1081,946)

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

Best guess label: presentation

8 Web entities found: 

	Score       : 0.595
	Description : Public Relations

	Score       : 0.561
	Description : Human behavior

	Score       : 0.534
	Description : New media

	Score       : 0.489
	Description : Human

	Score       : 0.472
	Description : Glasses

	Score       : 0.449
	Description : Media

	Score       : 0.418
	Description : Behavior

	Score       : 0.382
	Description : Public

10 visually similar images found:

	Image url    : https://media.licdn.com/media-proxy/ext?w=800&h=800&hash=wLHVOs69MdDubvNy2rtSTwLzrgo%3D&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R6jnhodx1Ey9KGTqAGj6E5DQJHUA3L0CHH05IbfPWjsKMLXebHyo0AQfH9SjQAxfue1SWTgFI7of4u6L9twjpTicMP5aRUPbhU4hGUB5sE-Pg
	Image url    : https://ranyasagittarius.files.wordpress.com/2017/09/321.jpg
	Image url    : https://pbs.twimg.com/media/DLhK2_5V4AAS8dA.jpg
	Image url    : https://pbs.twimg.com/media/DnjSwmXU4AAJJkt.jpg
	Image url    : x-raw-image:///cdeba9f1bfecda822f044587ee5b7c4f2cb46852a0bb481b0ebccda1b221cdf1
	Image url    : https://image-media.gloria.tv/bonifacius/k/8o/bs0lmgc0pk7t0ym2hwycivhpw0ym2hwycivhs.jpg
	Image url    : https://78.media.tumblr.com/247584f4f25e82d65c1bda07cbe89610/tumblr_okcr9ywjVA1uv55tgo1_500.jpg
	Image url    : https://fiverr-res.cloudinary.com/images/t_main1,q_auto,f_auto/gigs/110474513/original/f9225ffe4fb5663f401f6e9f1ec21f66f6a19544/create-responsive-cms-based-website-or-landing-page.jpg
	Image url    : https://image.slidesharecdn.com/australia2020schools-030206-100108162226-phpapp02/95/education-in-a-globally-connected-world-29-728.jpg?cb=1262967819
	Image url    : https://slideplayer.com/slide/3294938/11/images/1/Napoleon%E2%80%99s+Empire+Collapses.jpg
Compeleted: Image Analysis API ...
=====================================================

Bye~

In [62]:
# interupt kernel, then logout
itchat.logout() # 安全退出


Out[62]:
<ItchatReturnValue: {'BaseResponse': {'ErrMsg': '请求成功', 'Ret': 0, 'RawMsg': 'logout successfully.'}}>
LOG OUT!

恭喜您!已经完成了:

第二课:图像识别和处理

Lesson 2: Image Recognition & Processing

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

下一课是:

第三课:自然语言处理:语音合成和识别

Lesson 3: Natural Language Processing 1

  • 消息文字转成语音 (Speech synthesis: text to voice)
  • 语音转换成消息文字 (Speech recognition: voice to text)