<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: markk39006</title>
    <description>The latest articles on DEV Community by markk39006 (@markk39006).</description>
    <link>https://dev.to/markk39006</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1060602%2Fe2713735-91de-46ff-abf3-b0ec1aad8617.png</url>
      <title>DEV Community: markk39006</title>
      <link>https://dev.to/markk39006</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/markk39006"/>
    <language>en</language>
    <item>
      <title>การเรียนรู้และจดจำเสียงใน Python</title>
      <dc:creator>markk39006</dc:creator>
      <pubDate>Sun, 09 Apr 2023 09:24:42 +0000</pubDate>
      <link>https://dev.to/markk39006/khuumuuekaareriiynruuaelacchdcchamesiiyngain-python-110g</link>
      <guid>https://dev.to/markk39006/khuumuuekaareriiynruuaelacchdcchamesiiyngain-python-110g</guid>
      <description>&lt;p&gt;&lt;strong&gt;การเรียนรู้และจำเสียงคืออะไร?&lt;/strong&gt;&lt;br&gt;
การเรียนรู้และจดจำเสียงคือการระบุคำพูดและแปลงเป็นข้อความ ช่วยให้คอมพิวเตอร์เข้าใจภาษาของมนุษย์&lt;/p&gt;

&lt;p&gt;การเรียนรู้และจดจำเสียงเริ่มต้นด้วยการนำพลังงานเสียงที่รับเข้าโดยผู้พูดมาแปลงเป็นพลังงานไฟฟ้าจากไมโครโฟน จากนั้นจะแปลงพลังงานไฟฟ้านี้จากแบบ analog เป็นแบบ digital และสุดท้ายเป็นข้อความ&lt;/p&gt;

&lt;p&gt;เป็นการแบ่งข้อมูลเสียงออกเป็นเสียงต่างๆ และวิเคราะห์เสียงโดยใช้อัลกอริทึมเพื่อค้นหาคำที่น่าจะตรงกับเสียงนั้นมากที่สุด ทั้งหมดนี้ทำได้โดยใช้ Natural Language Processing และ Neural Networks &lt;/p&gt;

&lt;p&gt;การแปลงคำพูดเป็นข้อความในภาษา Python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import speech_recognition as sr
import pyttsx3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;เริ่มสร้างโปรแกรมที่รับเสียงเป็น input และแปลงเป็นข้อความ import โมดูลที่จำเป็น&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Initialize the recognizer
r = se.Recognizer()

def SpeakText(command):
   #Initialize the engine
   engine = pyttsx3.init()
   engine.say(command)
   engine.runAndWait()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;สร้างฟังก์ชันที่รับเสียงเป็น input เข้ามาและแปลงเป็นข้อความ&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#use the microphone as source  for input.
with sr.Microphone() as source2:
   # wait for a second to let the recognitzer
   # adjust the energy threshold based on
   # the surrounding noise level
r.adjust_for_ambuent_noise(source2, duration = 0.2)

   # listens for the user input
audio2 = r.listen(source2)

   # using google to recognize audio
MyText = r.recognize_google(audio2)
MyText = MyText.lower()

print("Did you say" +MyText)
SpeakText(MyText)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ขั้นตอนนี้จะใช้ไมโครโฟนเพื่อรับ input เสียงจากผู้ใช้แบบ Realtime และแสดงผลออกมาเป็นข้อความ&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;การนำมาประยุคต์ในเกม ทายคำศัพท์ Python Demo:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;เริ่มต้นที่การ import packages ที่จำเป็นต้องใช้&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import random
import time
import speech_recognition as sr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;สร้างฟังก์ชันเพื่อรับ input ที่ผู้ใช้กำลังพูดจากไมโครโฟน&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def recognize_speech_form_mic(recognize,microphone):
"""Transcribe speech from recorded from microphone`.
Returns a dictionary with three keys:
"success": a boolean indicaating whether or not the API request was successfull
"error": `None` if no error occured, otherwise a string containing an error message if the API could not ve reached or speech was unrecognizable
"transcription": `None` if speech could not be transcribed, otherwise a string containing the transcriebed text
"""

#check that recognizer and microphone argument are appropriate type 
if not isinstance(recognizer,sr.Recoginzer):
   raise TypeError("`recognizer` must be `Recognizer` instance")

if not isinstance(microphone,sr.Microphone):
   raise TypeError("`microphone` must be `Microphone` instance")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ถัดมาเราจะทำการสร้าง class ตัวจำแนกของคุณที่รับเข้ามา และ จะต้องตรวจสอบว่าเสียงที่ได้นั้นชัดเจนหรือไม่ และทำการเรียก API ว่ามีการทำงานผิดปกติหรือไม่&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# adjust the recognizer sensitivit to ambient noise and record audio form the microphone
with microphone as source:
   recognizer.adjust_for_ambien_noise(source)
   audio = recognizer.listen(source)

# set up the response object
response ={
   "success": True,
   "error": None,
   "transciption": None
}

#try recognizer the speech in the recording
#if a RequestError or UnknowValueError exception is caught
# update the response object accordingly
try:
   response["tanscription"] = recognizer.recognizer_google(audio)
except sr.RequestError:
   # API was unreachable or unresponsive
   response["success"] = False
   response["error"] = "API unabailable"
except se.UnknowValueError:
   # speech was unintelligible
   response["error"] = "Unable to recognize speech"
return response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ต่อมาทำการสร้างรายการที่มีคำศัพท์ต่างๆ ที่ผู้เล่นจะต้องทาย&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if__name__=="__main__":
   # set the of word, maxnumber of guesses and promt limit
WORDS = ["apple","banana","grape","orange","mango","lemon"]
NUM_GUESses = 2
PROMPT_LIMIT = 5

#create recognizer and mic instances
recognizer = sr.Recognizer()
microphone = sr.Microphone()

# get a random word from the list
word = random.choice(WORDS)

# format the instuctions string
instructions = (
   "I m thinking of one these word: \n"
   "{words}\n"
   "You have {n} tries to guess which on. \n"
).format(words=', '.join(WORDS), n=NUM_GUESSES)

# show instructions and wait 3 second before starting the game
print(instructions)
time.sleep(3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ถัดมาทำการสร้างฟังก์ชันที่รับ input 3ครั้ง ได้แก่ ตรวจสอบ คำที่เลือก และพิมพ์ผลลัพธ์&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(NUM_GUesses):
# get the guess from the user
# if a transcription is returned, break out of the loop and continue
# if no transcription return and API request failed, break loop and continue
# if API request success but notranscription was returned, 
#   re-promt the user to say their guess again. Do this up
#   to PROMPT_LIMIT times
for j in range(PROMPT_LIMIT):
   print('Guess {}. Speak!'.format(i+1))
   guess = recognize_speech_from_mic(recognizer,microphone)
   if guess["transcription"]:
      break
   if not gusess["success"]:
      break
   print("I didn't catch that. What did you say?\n")

# if there was an error, stop the game
if guss["error"]:
   print("ERROR: {}".format(guess["error"]))
   break

#show the user the transcription
print("You said: {}".format(guess["transcription"]))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output ที่ได้จะแสดงข้อความต่างๆโปรแกรม&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if guess_is_correct:
   print("Correct! You win!".format(word))
   break
elif user_has_more_attempis:
   print("Incorrect. Try again.\n")
else:
   print("Sorry,you lose!\nI was thinking of '{}'.".format(word))
break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rLi2X8Rt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9anweoqu40jiox95rf6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rLi2X8Rt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9anweoqu40jiox95rf6t.png" alt="Image description" width="537" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;จากผลลัพธ์ จะเห็นว่าคำที่เลือกคือ 'apple' ผู้เล่นทายได้ทั้งหมด3ครั้งและผิดในครั้งที่ 3 และได้ทำการเฉลยออกมาว่าคำที่ถูกคือคำอะไร&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;สรุป&lt;/strong&gt;&lt;br&gt;
การเรียนรู้และจดจำเสียงใน Python จะทำงานร่วมกับอัลกอริทึมที่ทำการสร้างแบบจำลองทางภาษาและเสียง การสร้างแบบจำลองอะคูสติกใช้เพื่อจดจำฟีโนน/สัทศาสตร์ในคำพูดของเราเพื่อให้ได้ส่วนที่สำคัญของคำพูด เช่น คำและประโยค โดยจะแบ่งข้อมูลเสียงออกเป็นเสียงต่างๆ และวิเคราะห์เสียงโดยใช้อัลกอริทึมเพื่อค้นหาคำที่น่าจะใกล้เคียงกับเสียงนั้นมากที่สุด วิธีการนี้มีประโยชน์อย่างมากในการนำไปใช้ในด้านอื่นๆนอกเหนือจากที่ยกตัวอย่างไปในด้านเกี่ยวกับเกม ไม่ว่าจะเป็นตัวช่วยเราพิมพ์ตามคำพูดตอนที่มือเราไม่ว่าง เป็นต้น นี้ก็เป็นอีกหนึ่งวิธีหรือแนวคิดที่น่าสนใจและนำมาประยุคต์ใช้ได้อย่างหลากหลายและมีประสิทธิภาพ&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.simplilearn.com/tutorials/python-tutorial/speech-recognition-in-python#what_is_speech_recognition"&gt;www.simplilearn.com&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
