เกี่ยวกับ Codelab นี้
1 ภาพรวม
Codelab นี้จะพาไปดู API แมชชีนเลิร์นนิงบางรายการได้อย่างรวดเร็ว คุณจะใช้หมายเลขต่อไปนี้
- Cloud Vision เพื่อทําความเข้าใจเนื้อหาของรูปภาพ
- การแปลงคําพูดเป็นข้อความในระบบคลาวด์ เพื่อถอดเสียงเสียงเป็นข้อความ
- Cloud Translation เพื่อแปลสตริงที่กําหนดเองเป็นภาษาใดก็ได้
- Cloud Natural Language เพื่อดึงข้อมูลจากข้อความ
สิ่งที่คุณจะสร้าง
คุณจะต้องสร้างไปป์ไลน์ที่เปรียบเทียบการบันทึกเสียงกับรูปภาพ และกําหนดความเกี่ยวข้องของกันและกัน ลองดูตัวอย่างวิธีบรรลุผลต่อไปนี้
สิ่งที่คุณจะได้เรียนรู้
- วิธีใช้ Vision, Speech-to-Text, Translation และ Natural Language API
- วิธีค้นหาตัวอย่างโค้ด
สิ่งที่ต้องมี
2 การตั้งค่าและข้อกําหนด
การตั้งค่าสภาพแวดล้อมด้วยตนเอง
- ลงชื่อเข้าใช้ Cloud Console และสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ํา หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
โปรดทราบว่ารหัสโปรเจ็กต์ ซึ่งเป็นชื่อที่ไม่ซ้ํากันสําหรับโปรเจ็กต์ Google Cloud ทั้งหมด (ชื่อข้างต้นมีผู้อื่นนําไปใช้แล้ว ขออภัยในความไม่สะดวก) และจะเรียกใน Codelab นี้ว่า PROJECT_ID
ในภายหลัง
- จากนั้นคุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากรของ Google Cloud
การเรียกใช้ Codelab นี้ไม่ควรมีค่าใช้จ่ายใดๆ หากมี อย่าลืมทําตามวิธีการในส่วน "Cleaning" เพื่อแนะนําวิธีปิดทรัพยากรเพื่อไม่ให้มีค่าใช้จ่ายนอกเหนือจากบทแนะนํานี้ ผู้ใช้ใหม่ของ Google Cloud มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรี$300 USD
เปิดใช้ API
คุณสามารถคลิกลิงก์เพื่อเปิดใช้ API ที่จําเป็นทั้งหมดได้ หลังจากตั้งค่าแล้ว โปรดไม่ต้องสนใจวิธีการตั้งค่าการตรวจสอบสิทธิ์ เราจะดําเนินการนี้ในอีกสักครู่ หรือจะเปิดใช้ API แต่ละรายการแยกกันก็ได้ โดยคลิกไอคอนเมนูที่ด้านซ้ายบนของหน้าจอ
เลือก API และบริการจากเมนูแบบเลื่อนลงแล้วคลิกที่หน้าแดชบอร์ด
คลิกเปิดใช้ API และบริการ
จากนั้นค้นหา "vision" ในช่องค้นหา คลิก Google Cloud Vision API:
คลิกเปิดใช้เพื่อเปิดใช้ Cloud Vision API
โปรดรอสักครู่เพื่อเปิดใช้ คุณจะเห็นข้อมูลนี้เมื่อเปิดใช้แล้ว
ทําขั้นตอนเดียวกันนี้ซ้ําเพื่อเปิดใช้ Cloud Speech, Cloud Translation และ Cloud Natural Language API
Cloud Shell
Google Cloud Shell คือสภาพแวดล้อมบรรทัดคําสั่งที่ทํางานในระบบคลาวด์ เครื่องเสมือนแบบ Debian นี้เต็มไปด้วยเครื่องมือสําหรับการพัฒนาทั้งหมดที่คุณต้องมี (gcloud
, bq
, git
และอื่นๆ) และให้บริการไดเรกทอรีหน้าแรกขนาด 5GB อย่างถาวร เราจะใช้ Cloud Shell เพื่อสร้างคําขอไปยัง API ของแมชชีนเลิร์นนิง
ในการเริ่มต้นใช้งาน Cloud Shell ให้คลิกไอคอน "เปิดใช้งาน Google Cloud Shell" ที่มุมขวาบนของแถบส่วนหัว
เซสชัน Cloud Shell จะเปิดในเฟรมใหม่ที่ด้านล่างของคอนโซลและแสดงข้อความแจ้งบรรทัดคำสั่ง รอจนกว่าข้อความแจ้ง user@project:~$ จะปรากฏขึ้น
ไม่บังคับ: ตัวแก้ไขโค้ด
คุณอาจต้องคลิกไอคอน "Launch Code Editor" ที่มุมบนขวาของแถบ Cloud Shell ตามความสะดวกของคุณ
บัญชีบริการ
คุณจะต้องมีบัญชีบริการในการตรวจสอบสิทธิ์ โดยให้แทนที่ [NAME] ด้วยชื่อบัญชีบริการที่ต้องการ และเรียกใช้คําสั่งต่อไปนี้ใน Cloud Shell
gcloud iam service-accounts create [NAME]
คุณก็ต้องสร้างคีย์เพื่อใช้บัญชีบริการนั้น แทนที่ [FILE_NAME] ด้วยชื่อคีย์ที่ต้องการ [NAME] ด้วยชื่อบัญชีบริการจากด้านบนและ [PROJECT_ID] ด้วยรหัสของโปรเจ็กต์ คําสั่งต่อไปนี้จะสร้างและดาวน์โหลดคีย์เป็น [FILE_NAME].json
gcloud iam service-accounts keys create [FILE_NAME].json --iam-account [NAME]@[PROJECT_ID].iam.gserviceaccount.com
หากต้องการใช้บัญชีบริการ คุณจะต้องตั้งค่าตัวแปร GOOGLE_application_CREDENTIALS เป็นเส้นทางของคีย์ โดยทําตามขั้นตอนต่อไปนี้หลังจากแทนที่ [PATH_TO_FILE] และ [FILE_NAME]
export GOOGLE_APPLICATION_CREDENTIALS=[PATH_TO_FILE]/[FILE_NAME].json
3 Cloud Vision
ไคลเอ็นต์ Python
คุณจะต้องมีไคลเอ็นต์ Python สําหรับ Cloud Vision หากต้องการติดตั้ง ให้พิมพ์คําสั่งต่อไปนี้ลงใน Cloud Shell
pip install --upgrade google-cloud-vision --user
มาลองกันเลย
มาดูตัวอย่างโค้ดสําหรับ Cloud Vision API เราสนใจที่จะค้นหาสิ่งที่อยู่ในรูปภาพที่ระบุ detect.py
ดูเหมือนจะมีประโยชน์สําหรับเรื่องนี้ งั้นมาลองดูกัน วิธีหนึ่งคือการคัดลอกเนื้อหาของตัวตรวจจับ.py สร้างไฟล์ใหม่ใน Cloud Shell ชื่อ vision.py
และวางโค้ดทั้งหมดลงใน vision.py
โดยคุณจะดําเนินการดังกล่าวเองได้ในเครื่องมือแก้ไขโค้ดของ Cloud Shell หรือเรียกใช้คําสั่ง curl นี้ใน Cloud Shell
curl https://raw.githubusercontent.com/GoogleCloudPlatform/python-docs-samples/master/vision/cloud-client/detect/detect.py -o vision.py
หลังจากที่ดําเนินการแล้ว ให้ใช้ API โดยเรียกใช้คําสั่งต่อไปนี้ใน Cloud Shell
python vision.py labels-uri gs://cloud-samples-data/ml-api-codelab/birds.jpg
คุณควรเห็นข้อมูลเกี่ยวกับนกและนกกระจอกเทศเนื่องจากภาพได้รับการวิเคราะห์แล้วที่ https://storage.googleapis.com/cloud-samples-data/ml-api-codelab/birds.jpg
เกิดอะไรขึ้น
คุณส่งอาร์กิวเมนต์ 2 รายการไปยัง vision.py
:
- labels-uri เลือกฟังก์ชัน
detect_labels_uri()
ที่จะเรียกใช้ - gs://cloud-samples-data/ml-api-codelab/birds.jpg คือตําแหน่งของรูปภาพใน Google Cloud Storage และส่งผ่านในรูปแบบ uri ไปยัง
detect_labels_uri()
มาดูรายละเอียด detect_labels_uri()
กัน โปรดสังเกตความคิดเห็นเพิ่มเติมที่แทรกเข้ามา
def detect_labels_uri(uri):
"""Detects labels in the file located in Google Cloud Storage or on the
Web."""
# relevant import from above
# from google.cloud import vision
# create ImageAnnotatorClient object
client = vision.ImageAnnotatorClient()
# create Image object
image = vision.types.Image()
# specify location of image
image.source.image_uri = uri
# get label_detection response by passing image to client
response = client.label_detection(image=image)
# get label_annotations portion of response
labels = response.label_annotations
print('Labels:')
for label in labels:
# print the label descriptions
print(label.description)
4 Cloud Speech-to-Text
ไคลเอ็นต์ Python
คุณจะต้องมีไคลเอ็นต์ Python สําหรับ Cloud Speech-to-Text หากต้องการติดตั้ง ให้พิมพ์คําสั่งต่อไปนี้ลงใน Cloud Shell
sudo pip install --upgrade google-cloud-speech
มาลองกันเลย
มาเริ่มกันที่ตัวอย่างโค้ดสําหรับ Cloud Speech-to-Text เราสนใจที่จะถอดเสียงคําพูด transcribe.py
เป็นจุดเริ่มต้นที่ดีในการเริ่มต้นใช้งาน มาเริ่มกันเลย คัดลอกเนื้อหาของ transcripte.py แล้วสร้างไฟล์ใหม่ใน Cloud Shell ชื่อ speech2text.py
และวางโค้ดทั้งหมดลงใน speech2text.py
โดยคุณจะดําเนินการดังกล่าวเองได้ในเครื่องมือแก้ไขโค้ดของ Cloud Shell หรือเรียกใช้คําสั่ง curl นี้ใน Cloud Shell
curl https://raw.githubusercontent.com/GoogleCloudPlatform/python-docs-samples/master/speech/cloud-client/transcribe.py -o speech2text.py
หลังจากที่ดําเนินการแล้ว ให้ใช้ API โดยเรียกใช้คําสั่งต่อไปนี้ใน Cloud Shell
python speech2text.py gs://cloud-samples-data/ml-api-codelab/tr-ostrich.wav
น่าจะมีข้อร้องเรียนเกี่ยวกับการเข้ารหัสที่ไม่ถูกต้องและอัตราการสุ่มตัวอย่างเฮิรตซ์ ไม่ต้องกังวล ไปที่transcribe_gcs()
ในโค้ดและลบการตั้งค่า encoding
และ sampe_hertz_rate
จากRecognitionConfig()
ในระหว่างนั้น ให้เปลี่ยนรหัสภาษาเป็น "tr-TR'" เนื่องจาก tr-ostrich.wav
เป็นเสียงพูดภาษาตุรกี
config = types.RecognitionConfig(language_code='tr-TR')
ตอนนี้ให้เรียกใช้ speech2text.py
อีกครั้ง เอาต์พุตควรเป็นข้อความภาษาตุรกีบางส่วนเนื่องจากเป็นการวิเคราะห์เสียง: https://storage.googleapis.com/cloud-samples-data/ml-api-codelab/tr-ostrich.wav
เกิดอะไรขึ้น
คุณส่ง gs://cloud-samples-data/ml-api-codelab/tr-ostrich.wav ซึ่งเป็นตําแหน่งของไฟล์เสียงใน Google Cloud Storage ไปยัง speech2text.py
ซึ่งจะส่งผ่านเป็น gcs_uri ไปยัง transcribe_uri()
มาดูรายละเอียดเกี่ยวกับ transcribe_uri()
ที่แก้ไขแล้วของเรากัน
def transcribe_gcs(gcs_uri):
"""Transcribes the audio file specified by the gcs_uri."""
from google.cloud import speech
# enums no longer used
# from google.cloud.speech import enums
from google.cloud.speech import types
# create ImageAnnotatorClient object
client = speech.SpeechClient()
# specify location of speech
audio = types.RecognitionAudio(uri=gcs_uri)
# set language to Turkish
# removed encoding and sample_rate_hertz
config = types.RecognitionConfig(language_code='tr-TR')
# get response by passing config and audio settings to client
response = client.recognize(config, audio)
# Each result is for a consecutive portion of the audio. Iterate through
# them to get the transcripts for the entire audio file.
for result in response.results:
# The first alternative is the most likely one for this portion.
# get the transcript of the first alternative
print(u'Transcript: {}'.format(result.alternatives[0].transcript))
5 Cloud Translation
ไคลเอ็นต์ Python
คุณจะต้องมีไคลเอ็นต์ Python สําหรับ Cloud Translation หากต้องการติดตั้ง ให้พิมพ์ข้อมูลต่อไปนี้ลงใน Cloud Shell
sudo pip install --upgrade google-cloud-translate
มาลองกันเลย
ต่อไปมาดูตัวอย่างโค้ดสําหรับ Cloud Translation สําหรับวัตถุประสงค์ของ Codelab นี้ เราต้องการแปลข้อความเป็นภาษาอังกฤษ snippets.py
มีลักษณะที่เราต้องการ คัดลอก contents ofsnippet.py สร้างไฟล์ใหม่ใน Cloud Shell ชื่อ translate.py
และวางโค้ดทั้งหมดลงใน translate.py
โดยคุณจะดําเนินการดังกล่าวเองได้ในเครื่องมือแก้ไขโค้ดของ Cloud Shell หรือเรียกใช้คําสั่ง curl นี้ใน Cloud Shell
curl https://raw.githubusercontent.com/GoogleCloudPlatform/python-docs-samples/master/translate/cloud-client/snippets.py -o translate.py
หลังจากที่ดําเนินการแล้ว ให้ใช้ API โดยเรียกใช้คําสั่งต่อไปนี้ใน Cloud Shell
python translate.py translate-text en '你有沒有帶外套'
คําแปลควรเป็น "คุณมี JacketCPDquot;
เกิดอะไรขึ้น
คุณส่งอาร์กิวเมนต์ 3 รายการไปยัง translate.py
:
- translate-text เลือกฟังก์ชัน
translate_text()
ที่จะเรียกใช้ - en จะส่งผ่านเป็นเป้าหมายเป็น
translate_text()
และทําหน้าที่ระบุภาษาที่จะแปล - ‘你有沒有帶外套' คือสตริงที่จะแปลและส่งผ่านเป็นข้อความเป็น
translate_text()
มาดูรายละเอียด translate_text()
กัน จดความคิดเห็นที่เพิ่มไว้
def translate_text(target, text):
"""Translates text into the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
# relevant imports from above
# from google.cloud import translate
# import six
# create Client object
translate_client = translate.Client()
# decode text if it's a binary type
# six is a python 2 and 3 compatibility library
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
# get translation result by passing text and target language to client
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language=target)
# print original text, translated text and detected original language
print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
result['detectedSourceLanguage']))
6 ภาษาธรรมชาติของ Cloud
ไคลเอ็นต์ Python
คุณจะต้องมีไคลเอ็นต์ Python สําหรับ Cloud Natural Language หากต้องการติดตั้ง ให้พิมพ์คําสั่งต่อไปนี้ลงใน Cloud Shell
sudo pip install --upgrade google-cloud-language
มาลองกันเลย
สุดท้ายเราจะดูตัวอย่างโค้ดสําหรับ Cloud Natural Language API เราต้องการตรวจหาเอนทิตีในข้อความ ดูเหมือนว่า snippets.py
จะมีโค้ดที่ทําเช่นนั้นได้ คัดลอก contents ofsnippet.py สร้างไฟล์ใหม่ใน Cloud Shell ชื่อ natural_language.py
และวางโค้ดทั้งหมดลงใน natural_language.py
โดยคุณจะดําเนินการดังกล่าวเองได้ในเครื่องมือแก้ไขโค้ดของ Cloud Shell หรือเรียกใช้คําสั่ง curl นี้ใน Cloud Shell
curl https://raw.githubusercontent.com/GoogleCloudPlatform/python-docs-samples/master/language/cloud-client/v1/snippets.py -o natural_language.py
หลังจากที่ดําเนินการแล้ว ให้ใช้ API โดยเรียกใช้คําสั่งต่อไปนี้ใน Cloud Shell
python natural_language.py entities-text 'where did you leave my bike'
API ควรระบุ "bike" เป็นเอนทิตี เอนทิตีอาจเป็นคํานามที่ถูกต้อง (บุคคลสาธารณะ จุดสังเกต ฯลฯ) หรือคํานามทั่วไป (ร้านอาหาร สนามกีฬา ฯลฯ)
เกิดอะไรขึ้น
คุณส่งอาร์กิวเมนต์ 2 รายการไปยัง natural_language.py
:
- entities-text เลือกฟังก์ชัน
entities_text()
ที่จะเรียกใช้ - "คุณปล่อยจักรยานของฉันไว้ไหน' คือสตริงที่จะวิเคราะห์เอนทิตีและส่งเป็นข้อความไปที่
entities_text()
มาดูรายละเอียด entities_text()
กัน จดความคิดเห็นใหม่ที่แทรกไว้
def entities_text(text):
"""Detects entities in the text."""
# relevant imports from above
# from google.cloud import language
# from google.cloud.language import enums
# from google.cloud.language import types
# import six
# create LanguageServiceClient object
client = language.LanguageServiceClient()
# decode text if it's a binary type
# six is a python 2 and 3 compatibility library
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
# Instantiates a plain text document.
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects entities in the document. You can also analyze HTML with:
# document.type == enums.Document.Type.HTML
entities = client.analyze_entities(document).entities
# entity types from enums.Entity.Type
entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION',
'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER')
# print information for each entity found
for entity in entities:
print('=' * 20)
print(u'{:<16}: {}'.format('name', entity.name))
print(u'{:<16}: {}'.format('type', entity_type[entity.type]))
print(u'{:<16}: {}'.format('metadata', entity.metadata))
print(u'{:<16}: {}'.format('salience', entity.salience))
print(u'{:<16}: {}'.format('wikipedia_url',
entity.metadata.get('wikipedia_url', '-')))
7 มาเริ่มผสานรวมกันเลย
มาลองเตือนตัวเองว่าคุณกําลังสร้างอะไรกัน
ทีนี้มารวบรวมทุกอย่างไว้ด้วยกัน สร้างไฟล์ solution.py
คัดลอกและวาง detect_labels_uri()
, transcribe_gcs()
, translate_text()
และ entities_text()
จากขั้นตอนก่อนหน้าลงใน solution.py
นําเข้าใบแจ้งยอด
เลิกแสดงความคิดเห็นและย้ายคําสั่งนําเข้าไปด้านบนสุด โปรดทราบว่ากําลังนําเข้าทั้ง speech.types
และ language.types
การดําเนินการนี้จะทําให้เกิดความขัดแย้ง ดังนั้นโปรดนําออกและเปลี่ยนรายการ types
แต่ละรายการใน transcribe_gcs()
และ entities_text()
เป็น speech.types
และ language.types
ตามลําดับ คุณควรมีสิ่งต่อไปนี้
from google.cloud import vision
from google.cloud import speech
from google.cloud import translate
from google.cloud import language
from google.cloud.language import enums
import six
แสดงผลลัพธ์
ให้ฟังก์ชันแสดงผลลัพธ์แทนการพิมพ์ คุณควรจะมีลักษณะดังนี้
# import statements
def detect_labels_uri(uri):
# code
# we only need the label descriptions
label_descriptions = []
for label in labels:
label_descriptions.append(label.description)
return label_descriptions
def transcribe_gcs(gcs_uri):
# code
# naive assumption that audio file is short
return response.results[0].alternatives[0].transcript
def translate_text(target, text):
# code
# only interested in translated text
return result['translatedText']
def entities_text(text):
# code
# we only need the entity names
entity_names = []
for entity in entities:
entity_names.append(entity.name)
return entity_names
ใช้ฟังก์ชัน
พอได้ทํางานแล้ว คุณก็ต้องเรียกใช้ฟังก์ชันเหล่านั้น เยี่ยมไปเลย เช่น
def compare_audio_to_image(audio, image):
"""Checks whether a speech audio is relevant to an image."""
# speech audio -> text
transcription = transcribe_gcs(audio)
# text of any language -> english text
translation = translate_text('en', transcription)
# text -> entities
entities = entities_text(translation)
# image -> labels
labels = detect_labels_uri(image)
# naive check for whether entities intersect with labels
has_match = False
for entity in entities:
if entity in labels:
# print result for each match
print('The audio and image both contain: {}'.format(entity))
has_match = True
# print if there are no matches
if not has_match:
print('The audio and image do not appear to be related.')
รองรับหลายภาษา
ก่อนหน้านี้เราฮาร์ดโค้ดตุรกีเป็นภาษาtranscribe_gcs()
มาเปลี่ยนแปลงเพื่อให้ระบุภาษาจาก compare_audio_to_image()
ได้ การเปลี่ยนแปลงที่จําเป็นมีดังนี้
def transcribe_gcs(language, gcs_uri):
...
config = speech.types.RecognitionConfig(language_code=language)
def compare_audio_to_image(language, audio, image):
transcription = transcribe_gcs(language, audio)
ลองเลย
ดูโค้ดสุดท้ายได้ใน solution.py ของที่เก็บ GitHub นี้ ต่อไปนี้เป็นคําสั่ง curl ในการดึงข้อมูล
curl https://raw.githubusercontent.com/googlecodelabs/integrating-ml-apis/master/solution.py -O
เวอร์ชันใน GitHub มี argparse ซึ่งอนุญาตให้บรรทัดคําสั่งจากคําสั่งต่อไปนี้
python solution.py tr-TR gs://cloud-samples-data/ml-api-codelab/tr-ball.wav gs://cloud-samples-data/ml-api-codelab/football.jpg
ในแต่ละรายการที่พบ โค้ดควรเอาต์พุต " เสียงและรูปภาพประกอบด้วย: " ในตัวอย่างข้างต้น ควรจะเป็น "เสียงและภาพทั้งสองประกอบด้วย: ball"
โบนัส: ลองอีกที
ต่อไปนี้เป็นตําแหน่งไฟล์เสียงและภาพเพิ่มเติม
8 ยินดีด้วย
คุณได้สํารวจและผสานรวม API แมชชีนเลิร์นนิง 4 รายการเพื่อดูว่าตัวอย่างคําพูดกําลังพูดถึงรูปภาพที่ให้ไว้หรือไม่ นี่เป็นแค่จุดเริ่มต้น เพราะยังมีวิธีอื่นๆ มากมายในการปรับปรุงไปป์ไลน์นี้
สิ่งที่เราพูดถึง
- การส่งคําขอไปยัง Cloud Vision API
- การส่งคําขอไปยัง Cloud Speech-to-Text API
- การส่งคําขอไปยัง Cloud Translation API
- การส่งคําขอไปยัง Cloud Natural Language API
- การใช้ API ข้างต้นทั้งหมดพร้อมกัน
ขั้นตอนถัดไป
- ดูที่ word2vec เพื่อให้เปรียบเทียบคําได้ดีขึ้น
- ดู Codelab ที่เจาะลึกมากขึ้นเกี่ยวกับ Vision API, Speech-to-Text API, API การแปล และ Natural Language API
- ลองแทนที่ Cloud Vision ด้วย Cloud Video Intelligence
- สังเคราะห์เสียงด้วย Cloud Text-to-Speech API
- ดูวิธีอัปโหลดออบเจ็กต์ไปยัง Cloud Storage