[G-Assistant] เพิ่มคำสั่งใหม่ๆ ง่ายๆ ด้วย Cloud Speech API
วันนี้เราจะมาลองเพิ่มคำสั่งใหม่ๆ ของเราเองแบบง่ายๆ ด้วย Google Cloud Speech API กันครับ ซึ่งเจ้า API ตัวนี้จะทำหน้าที่เป็นรับฟังเสียงของเราแล้วเปลี่ยนเป็นข้อความ (Speech Recognizer) ที่เบื้องหลังเป็น Neural Network Models ที่ซับซ้อน มาเป็น API แบบให้เราใช้งานได้ง่ายๆ โดย API นี้รองรับมากกว่า 110 ภาษา
Google Cloud Speech API จำเป็นต้องตั้งค่าการจ่ายบิล (ฺBilling)ก่อนนะครับ ซึ่งแน่นอนว่าเป็นบริการที่จะมีการคิดเงิน (ถ้าเราใช้เกินโควต้าฟรี) โดยเราสามารถใช้งานได้ฟรี 60 นาทีต่อเดือน ถ้าเกินกว่านั้นจะคิด 15 วินาทีละ $0.0016
สิ่งที่ต้องเตรียมก่อนเริ่ม
Step 1: การตั้งค่าการจ่ายบิล (Billing)
- เลือก Project ที่เราจะตั้งค่าการจ่ายบิล
- ที่ Cloud Console, เปิด navigation menu ที่ด้านบนซ้าย
- เลือก Billing
- ถ้าหากยังไม่ได้ทำการตั้งค่าการจ่ายเงินไว้ ให้เลือก New billing account
- ถ้าเคยสร้างไว้แล้ว แต่ในโปรเจ็คใหม่เรายังไม่ได้กำหนดไว้ ให้สร้างใหม่ โดยการเลือก Link a billing account
- กด Create Billing Account เพื่อสร้างบิล
- ตั้งชื่อบิล ที่ช่อง Name และกด Continue
- ตั้งค่าบิล โดยเลือกประเทศไทย กด Confirm
- ที่ Payment Profile ให้ใส่รายละเอียดต่างๆลงไป เช่น ที่อยู่สำหรับการออกบิล การจ่ายเงิน (ข้อมูลบัตรเครดิต)
- กลับมาที่หน้าหลักของ Billing แล้วเลือกที่แทป Project ที่เราสร้างไว้
- เช็คดูอีกครั้งว่า Project ของเราได้ทำการเชื่อมต่อกับ Billing Account เรียบร้อยแล้ว
Step 2: เปิดใช้งาน API
- ไปที่ console (console.cloud.google.com ) ที่เมนู navigation บนขวามือ ให้เลือก APIs & services
- คลิ๊กที่ ENABLE API AND SERVICES
- พิมพ์ “Cloud Speech API” ในช่องค้นหา จากนั้นก็เลือก Google Cloud Speech API ที่แสดงขึ้นมาครับ
- เข้ามาที่หน้า Google Cloud Speech API แล้ว ให้กด ENABLE เพื่อเปิดใช้งาน
Step 3: สร้าง Service Account และ Credentials
- ที่หน้า APIs & Services ให้เราเลือกเมนู Credentials
- กด Create credentials และเลือก Service account key จากลูกศรชี้ลง
- ที่ช่อง Service Account เลือก New Service Account
- จากนั้นให้เรากำหนดชื่อเรียก voice recognizer ของเรา ดังตัวยอย่าง “SS Voice credentials” (อะไรก็ได้)
- ที่ช่อง Role เลือก Project viewer
- ใช้ key แบบ JSON
- เสร็จแล้วกด Create ได้เลยครับ
- หลังจากที่เรากด create เสร็จ ระบบจะดาวโหลดไฟล์มาให้เราอัตโนมัติครับ ซึ่งไฟล์จะชื่อโปรเจ็คของเราตามด้วยตัวเลข
- ให้เปลี่ยนชื่อไฟล์เป็น cloud_speech.json
- ย้ายไฟล์ดังกล่าวมาไว้ที่ Raspberry Pi ที่ทำแหน่ง
/home/pi
- เท่านี้ก็เสร็จแล้วครับ
Step 4: มาลองเล่นกันเลย
ในตัวอย่าง Demo ของ Google AIY จะมีโค๊ดตัวอย่างนี้มาให้ด้วยคือ src/cloudspeech_demo.py
ซึ่งตัวอย่างนี้เราจะใช้ปุ่มกด เพื่อ Activate คำสั่งเสียง จากนั้นเราสามารถสั่งคำสั่ง ปิด/เปิด LED ได้ ด้วยคำสั่งต่อไปนี้
- “Turn on the light” > ไฟ LED ติดค้าง
- “Turn off the light” > ไฟ LED ดับ
- “Blink” > ไฟ LED กระพริบ
- “Goodbye” > เพื่อออกจากโปรแกรม
- ลองเล่นโดยการเปิด Dev Terminal แล้วพิมพ์คำสั่ง
src/cloudspeech_demo.py
โค๊ด
- ไฟล์
src/cloudspeech_demo.py
"""A demo of the Google CloudSpeech recognizer."""import aiy.audio
import aiy.cloudspeech
import aiy.voicehatdef main():
recognizer = aiy.cloudspeech.get_recognizer()
recognizer.expect_phrase('turn off the light')
recognizer.expect_phrase('turn on the light')
recognizer.expect_phrase('blink')button = aiy.voicehat.get_button()
led = aiy.voicehat.get_led()
aiy.audio.get_recorder().start()while True:
print('Press the button and speak')
button.wait_for_press()
print('Listening...')
text = recognizer.recognize()
if text is None:
print('Sorry, I did not hear you.')
else:
print('You said "', text, '"')
if 'turn on the light' in text:
led.set_state(aiy.voicehat.LED.ON)
elif 'turn off the light' in text:
led.set_state(aiy.voicehat.LED.OFF)
elif 'blink' in text:
led.set_state(aiy.voicehat.LED.BLINK)
elif 'goodbye' in text:
breakif __name__ == '__main__':
main()
อธิบายโค๊ด
- จากโค๊ดตัวอย่างจะเห็นว่า จะมีการเรียกใช้ Library ของ Cloud speech คือ
import aiy.cloudspeech
- คำสั่งที่เราจะเพิ่มเข้าไป คือคำหรือประโยคที่อยู่ในเครื่องหมาย ‘ ’ ซึ่งในตัวอย่างนี้คือ ‘turn on the light’
recognizer.expect_phrase('turn on the light')
- เมื่อ recongnizer รู้จักคำสั่งใหม่ที่เราเพิ่มไปแล้ว เราก็กำหนดให้ว่าเมื่อจับคำสั่งได้ให้ทำงานต่อไป ตามที่เราตั้งไว้
if 'turn on the light' in text:
led.set_state(aiy.voicehat.LED.ON)
elif 'turn off the light' in text:
led.set_state(aiy.voicehat.LED.OFF)
- ถ้าได้ยินคำสั่งว่า “turn on the light” ให้ปิดไฟ LED ด้วยคำสั่ง
led.set_state(aiy.voicehat.LED.ON)
- ถ้าได้ยินคำสั่งว่า “turn off the light” ให้ปิดไฟ LED ด้วยคำสั่ง
led.set_state(aiy.voicehat.LED.OFF)
ทดสอบการทำงาน
- ตัวอย่างนี้จะมีแต่สถานะการทำงานบอก
เพิ่มเสียงตอบรับคำสั่ง
เราสามารถเพิ่มเสียงตอบรับการทำงานได้ เช่น เมื่อเราสั่ง “turn on the light” ให้ G-Assistant ตอบกลับเราว่า “the light is on”
- ที่ไฟล์ตัวอย่างเดิม
src/cloudspeech_demo.py
เราจะทำการเพิ่มคำสั่งเสียง - เพิ่มคำสั่งเสียงตอบกลับ เวลาที่ไฟติด โดยให้เราเพิ่มคำสั่งดังต่อไปนี้ ก่อนหรือหลัง คำสั่งที่จะทำการเปิด/ปิดไฟ
aiy.audio.say(‘the light is on’)
- เพิ่มคำสั่งเสียงตอบกลับ เวลาที่ไฟดับ
aiy.audio.say(‘the light is off’)
- เพิ่มคำสั่งเสียง เวลาที่ไฟกระพริบ
aiy.audio.say(‘the light is blink’)
เพิ่มคำสั่ง Shutdown และ Reboot
- ที่ไฟล์ตัวอย่างเดิม
src/cloudspeech_demo.py
- เราจะทำการเพิ่ม library ที่ชื่อว่า subprocess ไว้ที่ด้านบนของโค๊ดด้วยครับ เพื่อใช้ในการสั่ง Raspbian ให้ทำคำสั่งของ Linux ได้
import subprocess
- คำสั่ง shutdown ใน Raspberry Pi ปกติ คือ
sudo shutdown -h now
โดยเราจะเพิ่มโค๊ดคำสั่งดังนี้ (หมายเหตุ h=halt)
subprocess.call(["sudo", "shutdown", "-h", "now"])
- คำสั่ง rebootใน Raspberry Pi ปกติ คือ
sudo shutdown -r now
โดยเราจะเพิ่มโค๊ดคำสั่งดังนี้ (หมายเหตุ r=reboot)
subprocess.call(["sudo", "shutdown", "-r", "now"])
โค๊ดทั้งหมด
"""A demo of the Google CloudSpeech recognizer."""import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
import subprocessdef main():
recognizer = aiy.cloudspeech.get_recognizer()
recognizer.expect_phrase('turn off the light')
recognizer.expect_phrase('turn on the light')
recognizer.expect_phrase('blink')
recognizer.expect_phrase('shutdown')
recognizer.expect_phrase('reboot')button = aiy.voicehat.get_button()
led = aiy.voicehat.get_led()
aiy.audio.get_recorder().start()while True:
print('Press the button and speak')
button.wait_for_press()
print('Listening...')
text = recognizer.recognize()
if text is None:
print('Sorry, I did not hear you.')
else:
print('You said "', text, '"')
if 'turn on the light' in text:
led.set_state(aiy.voicehat.LED.ON)
aiy.audio.say('the light is on')
elif 'turn off the light' in text:
led.set_state(aiy.voicehat.LED.OFF)
aiy.audio.say('the light is off')
elif 'blink' in text:
led.set_state(aiy.voicehat.LED.BLINK)
aiy.audio.say('the light is blink')
elif 'shutdown' in text:
aiy.audio.say('good bye')
subprocess.call(["sudo", "shutdown", "-h", "now"])
elif 'reboot' in text:
aiy.audio.say('see you in a bit')
subprocess.call(["sudo", "shutdown", "-r", "now"])
elif 'goodbye' in text:
breakif __name__ == '__main__':
main()
ทดสอบการทำงาน
- ตัวอย่างนี้จะมีเสียงตอบรับ บอกสถานะการทำงานด้วย
G-Assistant The Series
- [G-Assistant] ก้าวแรกสู่ AI ด้วย AIY Projects — Voice Kit
- [G-Assistant] ตั้งค่าพื้นฐาน Raspberry Pi โดยไม่ใช้จอ HDMI และคีย์บอร์ด
- [G-Assistant] สร้าง Google Assistant ด้วยตัวเองกับ Raspberry Pi แบบไม่ง้อชุด Kit
- [G-Assistant] เพิ่มคำสั่งใหม่ๆ ง่ายๆ ด้วย Cloud Speech API