2016-11-19 7 views
2

음성 인식을 사용하여 목록에 색상을 확인합니다. 목록에없는 경우 '색상을 찾을 수 없습니다'라고 표시되면 '색상 '각 메시지를 한 번만 표시하고 싶습니다. 내가 겪고있는 문제는 '색상을 찾을 수 없음'메시지를 올바르게 표시하는 방법입니다.음성 인식 결과가 예상되지 않을 때 메시지 인쇄

# speech recognition 

import speech_recognition as speech 

#a lot of variables used for loops and conditions 
voice = speech.Recognizer() 
condition = False 
condition2 = False 
condition3 = False 
boolean = False 
counter = 0 
counter2 = 0 

while condition == False: 
#with microphone input as the input 
with speech.Microphone() as source: 
    print("Say something!") 

    #variable = the input from the microphone 
    audio = voice.listen(source) 
    voiceRecog = voice.recognize_google(audio) 
    try: 
     #print the word that was heard 
     print("\nYou said: " + voiceRecog) 
    except speech.UnknownValueError: 


     #if couldnt recognise then print this msg 
     print("Sorry, we couldn't make that out. Try again!") 

    except speech.RequestError as e: 
     print("Could not request results from Google Speech Recognition service; {0}".format(e)) 


    #list of colours for comparing 
    colours = ["blue", "red", "Green", "Orange", "yellow", "pink", "purple", "black"] 

    #loop to check the word spoken against each word in the list 
    for i in range(len(colours)): 



     #so that if colour found it doesnt claim colour not found 
     if boolean == False: 
      condition2 = False 

     #increase counters by 1 and change booleans to True 
     #if spoken word matches any words in the list then output 
     if colours[i] == voiceRecog: 

      boolean = True 
      condition2 = True 
      condition3 = True 
      counter += 1 
      counter2 += 1 

      print("\nColour Found!\n") 

     #if user says "quit" then all booleans = True (exit all loops) 
     elif voiceRecog == "quit": 

      boolean = True 
      condition = True 
      condition2 = True 
      condition3 = True 

     #if none of other conditions check value of i and of con2 
     else: 
      #if end of list is reached and con2 = False then con3 = False 
      if (i + 1) == len(colours) and condition2 == False: 
       condition3 = False 
       print(end = "") 
    #if con3 = False then counter increase and print 'colour not found' 
    if condition3 == False:    
     print("\nColour not found!\n\n") 
     counter += 1 

#once loop exited by saying quit, print attempts and successful attempts 
print("\nYou checked for", counter, "colours and had", counter2, "successful attempts") 

위 코드는 다음과 같습니다.

말 뭔가!

너는 말했다 : 푸른 색

색깔 발견!

무언가를 말하십시오!

너는 말했다 : 녹색

색깔 발견!

무언가를 말하십시오!

너는 말했다 : Dave Dave

말하십시오!

당신은 말했다 : 종료 당신은 2 색 검사와 3 회 만이 개 성공적인 시도가 있어야이 개 성공적으로 시도

했다

.

하지만 난 그것을 다른 방법으로 주위 할 경우 :

말 뭔가를!

너는 말했다 : Dave Dave

색깔을 찾을 수 없다!

무언가를 말하십시오!

당신은 말했다 : 스티브 스티브

컬러를 찾을 수 없습니다!

무언가를 말하십시오!

당신은 말했다 : 빨간색

컬러를 찾을!

무언가를 말하십시오!

너는 말했다 : 검정색

색깔 발견!

무언가를 말하십시오!

당신은 말했다 : 당신은 4 개 가지 색상을 검사하고 있었다

을 종료 2 개 성공적인 시도

내가이 일을하는 이유를 알고,하지만 난 주위에 방법을 알아낼 수 없습니다 .

답변

0

안녕 난 내가 파이썬 syntaxes..here에 대해 잘 모릅니다 C#을 프로그래머가 C#에서 내 생각이다는`

variable succes_Counter 
variable unsuccess_Counter = 0 
boolean color_not found = true 
for i in range(len(colours)): 
    { 

      if colours[i] == voiceRecog 
      { 
       print("color found + colours[i] + ") 
       succes_Counter += 1 

       //HERE IT EXISTS FROM THIS LOOP 

       break 
      } 
      elseif voiceRecog == "quit" 

      //HERE IT EXISTS FROM THIS LOOP 
       break 


      else 
       if color_not found == true 
       { 
       print(" color not found") 

       unsuccess_Counter +=1 

       color_not found = false 
       } 
     } 


      color_not found = true 
      print("\nYou checked for", unsuccess_Counter, "colours and had", succes_Counter, "successful attempts") 
1
# speech recognition 

import speech_recognition as speech 

#a lot of variables used for loops and conditions 
voice = speech.Recognizer() 
# Maintain status of each attempt. You can just use two variables successful_attempts and total_attempts also but status list might help you in maintaining more things like a list of tuples (color, result) etc 
status = [] 
# successful_attempts, total_attempts = 0, 0 

while True: 
    #with microphone input as the input 
    with speech.Microphone() as source: 
     print("Say something!") 

     #variable = the input from the microphone 
     audio = voice.listen(source) 
     voiceRecog = voice.recognize_google(audio) 
     try: 
      #print the word that was heard 
      print("\nYou said: " + voiceRecog) 
     except speech.UnknownValueError: 


      #if couldnt recognise then print this msg 
      print("Sorry, we couldn't make that out. Try again!") 

     except speech.RequestError as e: 
      print("Could not request results from Google Speech Recognition service; {0}".format(e)) 


     #list of colours for comparing 
     colours = ["blue", "red", "Green", "Orange", "yellow", "pink", "purple", "black"] 

     if "quit" in voiceRecog: 
      break 
     elif voiceRecog in colours: 
      status.append(1) 
      # successful_attempts += 1 
      print("\nColour Found!\n") 
     else: 
      status.append(0) 
      print("\nColour not found!\n\n") 

     # total_attempts += 1 

#once loop exited by saying quit, print attempts and successful attempts 
print("\nYou checked for", len(status), "colours and had", sum(status), "successful attempts") 
#print("\nYou checked for", total_attempts, "colours and had", successful_attempts, "successful attempts") 
작동이 시도