3

I'm trying to write a code in pygame that uses speech recognition and different images. The code should function as follow: The user will be shown different images of animals and will have to say what they are seeing. After that, the speech recognization will take the user's input and convert it to a string and compare it with the string associated with the shown image. And determine whether the answer is correct or incorrect and move to the other image.

The difficulty I'm having right now are uploading and updating the image each time the user provide an answer

https://github.com/Naif94/Displaing-IMG.git

import os
import glob
import pygame
import time
import speech_recognition as sr

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
alpha = (0,88,255)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('GUI Speech Recognition')

current_path = os.path.dirname(__file__) 
resource_path = os.path.join(current_path, 'test') 
image_path = os.path.join(resource_path, 'MG') 

#image_list = []
#for filename in glob.glob('MG/*.jpg'):
    #image_list.append(filename)
    


def close():
   pygame.quit()
   quit()

def message_display(text):
   largeText = pygame.font.Font('freesansbold.ttf',30)
   TextSurf, TextRect = text_objects(text, largeText)
   TextRect.center = ((display_width/2),(display_height/2))
   gameDisplay.blit(TextSurf, TextRect)

   pygame.display.update()


def text_objects(text, font):
   textSurface = font.render(text, True, alpha)
   return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ic,ac,action=None):
   mouse = pygame.mouse.get_pos()
   click = pygame.mouse.get_pressed()
   if x+w > mouse[0] > x and y+h > mouse[1] > y:
       pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

       if click[0] == 1 and action != None:
           action()         
   else:
       pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

   smallText = pygame.font.SysFont("comicsansms",20)
   textSurf, textRect = text_objects(msg, smallText)
   textRect.center = ( (x+(w/2)), (y+(h/2)) )
   gameDisplay.blit(textSurf, textRect)

def load_the_image(image):
    return pygame.image.load(os.path.join(image_path,image))

images = [
    load_the_image('cat.jpg'),
    load_the_image('monkey.jpg'),
    load_the_image('dog.jpg')
]

WINDOW = pygame.display.set_mode((800,800))


def s2t():
  # gameDisplay.blit(carImg,(0,0))
   r = sr.Recognizer()

   with sr.Microphone() as source:
       print ('Say Something!')
       audio = r.listen(source)
       print ('Done!')

   text = r.recognize_google(audio)
   print(text)
   
   


   for i in range(len(images)):
       WINDOW.blit(i)
       if i == 1:
           index=0
           gameDisplay.fill(white)
           carImg = pygame.image.load(images[index])
           pygame.display.update()
           gameDisplay.blit(carImg,(130,0))
           
           if text == 'cat':
               message_display('good job')
           else:
               message_display('wrong')
               
       elif i== 2:
           index=1
           gameDisplay.fill(white)
           carImg = pygame.image.load(images[index])
           pygame.display.update()
           gameDisplay.blit(carImg,(130,0))
           
           if text == 'monkey':
               message_display('good job')
           else:
               message_display('wrong')
       elif i== 3:
           index=2
           gameDisplay.fill(white)
           carImg = pygame.image.load(images[index])
           pygame.display.update()
           gameDisplay.blit(carImg,(130,0))
           
           if text == 'dog':
               message_display('good job')
           else:
               message_display('wrong')
               

def main():
   while True:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               pygame.quit()
               quit()
       button("Speak!",150,450,100,50,green,bright_green,s2t)
       button("Quit",550,450,100,50,red,bright_red,close)
       pygame.display.update()

if __name__ == '__main__':
   main()

2 Answers2

1

Create a list of images, rather than a list of filenames:

image_list = []
for filename in glob.glob('MG/*.jpg'):
    image_list.append(pygame.image.load(filename))

Create a list of names. The list of names has to correspond to the list of images:

name_list = ['dog', 'cat', 'rat']

Find the index of the element in name_list, which matches text. There are different ways to do that. See Finding the index of an item in a list. For instance:

try:
    index = name_list.index(text)
except ValueError:
    index = -1

Example:

image_list = []
for filename in glob.glob('MG/*.jpg'):
    image_list.append(pygame.image.load(filename))

name_list = ['dog', 'cat', 'rat'] 
text = r.recognize_google(audio)

try:
    index = name_list.index(text)
except ValueError:
    index = -1

gameDisplay.fill(white)
if 0 <= index <= len(image_list): 
    gameDisplay.blit(image_list[index], (130,0))
pygame.display.update()

if index > 0:
    message_display('good job')
else:
    message_display('wrong')
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • https://github.com/Naif94/Displaing-IMG.git I am not able to display any pic and updating it, can u please take a look and help me :) – Naif Alhwsawi Sep 12 '20 at 15:18
  • @NaifAlhwsawi The images are read form the current working directory. The working directory can be different form the directory of the python script. Read the answer to [BG = pygame.image.load('sprite/test_bg.jpg') pygame.error: Couldn't open sprite/test_bg.jpg](https://stackoverflow.com/questions/58177145/bg-pygame-image-loadsprite-test-bg-jpg-pygame-error-couldnt-open-sprite/58178276#58178276) – Rabbid76 Sep 12 '20 at 20:01
  • I tried using list of name instead of files, I also changed the working directory, but i am still not able to run it successfully. I have updated the code please help . Thanks again – Naif Alhwsawi Sep 14 '20 at 21:11
0
# Heres a much better way of doing this

# First you want to create an actual function that loads the image.

def load_the_image(image):
    return pygame.image.load(image)

images = [
    load_the_image('image_1.png'),
    load_the_image('image_2.png'),
    load_the_image('image_3.png')
]
# Make a window or something
WINDOW = pygame.display.set_mode((800,800))# WHATVER SIZE




for i in range(len(images)):

    WINDOW.blit(i)
    input("what is this image")

    # do code stuff here
    #
    # This sequentually runs through a sequence so you can do your if statements here ect...
    #