Ejemplo n.º 1
0
class GameScreen(
        Widget
):  #This class has all of the code for the actual game, and sets up the screen, makes things move, react to the player
    """This makes certain parts of the tutorial happen. The first part puts the target on the screen, with its text. the second part (which is called when they click the rectangle) removes the rectangle and tells them to click on the other side. the third part puts a lot of instructions on the screen. the fourth part takes them down and starts the game.--takes in the part of the tutorial to run."""
    def tutorial(self, part):
        if part == 0:
            self.Tutorial_text = Label(text="Press the Rectangle to start",
                                       font_size=20,
                                       center_x=app.game.center_x,
                                       center_y=app.game.center_y * 1.75)
            self.add_widget(self.Tutorial_text)
            Clock.schedule_interval(self.update, 1.0 / 60.0)
            self.target = Target()
            self.target.move_to(
                280, 155
            )  #puts the target rectangle in a good place for their first click
            self.add_widget(self.target)
        if part == 1:
            self.remove_widget(self.target)
            self.Tutorial_text.text = "Your next click moves the other side of the line"
            self.Tutorial_text.font_size = 15
        if part == 2:
            Clock.unschedule(self.update)
            self.remove_widget(self.Tutorial_text)
            self.Tutorial_text = []
            self.texts = [
                "You can only move the line once per bounce",
                "Otherwise, it counts as a double-move",
                "the top left number is your score",
                "top right is your number of double-moves",
                "+1 double-moves for every spaceship",
                "You'll stop scrolling if you miss a spaceship",
                "if the ball hits any side, you lose",
                "Bumpers push the ball away from the sides, and",
                "can be turned on and off in settings.",
                "Have Fun! Click to play"
            ]
            for i in range(10):  #puts the instructions on the screen
                self.Tutorial_text.append(
                    Label(text=self.texts[i],
                          font_size=15,
                          center_x=app.game.center_x,
                          center_y=470 - i * 20))
                self.add_widget(self.Tutorial_text[i])
        if part == 3:
            for i in self.Tutorial_text:
                self.remove_widget(i)
                self.touched = False
            Clock.schedule_interval(self.update, 1.0 / 60.0)

    """This moves the ball and the lines around, and runs other functions that make the game work. it checks whether they lose, keeps score, speeds the game up, moves the screen up, a few other things to facilitate the game. details in comments below."""

    def update(self, dt):
        if self.ball.collide():  #checks for collision
            self.ball.bounce(
                15 * self.speed
            )  #bounces according to the speed of the ball (to make up for the speed of the ball)
        if self.first_line_complete > 1:
            self.ball.move()
            self.ball.velocity_y -= self.speed
            self.speed *= 1.0002
            self.line_speed *= 1.00012  #these two make the game slowly speed up
        if self.can_draw:  #these move the point that is moving toward where they last clicked
            self.point[self.point_to_move].x += (
                (self.next_point.x - self.point[self.point_to_move].x) *
                self.line_speed)
            self.point[self.point_to_move].y += (
                (self.next_point.y - self.point[self.point_to_move].y) *
                self.line_speed)
        self.remove_widget(self.drawn_line)
        self.drawn_line = Drawn_line()
        self.drawn_line.draw()
        self.add_widget(self.drawn_line)
        if self.touched:  #checks that they are touching the screen, then puts the hypothetical line there
            self.remove_widget(self.hypothetical_line)
            self.hypothetical_line = Hypothetical_line()
            self.hypothetical_line.draw(self.point[self.point_to_move],
                                        Point(self.Mouse_x, self.Mouse_y))
            self.add_widget(self.hypothetical_line)
        elif self.hypothetical_line in self.children:  #takes out the hypothetical line if the are not touching the screen
            self.remove_widget(self.hypothetical_line)
        self.up_token.move()
        self.move_screen_up()
        if self.ball.collide_widget(self.up_token):
            self.up_token.spawn()  #this moves the up_token upwards
            self.double_touches += 1
        if (self.ball.y + self.ScrollHeight) / 24 > self.score:
            self.score = int((self.ball.y + self.ScrollHeight -
                              ((self.ball.y + self.ScrollHeight) % 24)) / 24)
            self.Score.text = str(self.score)
        if self.ball.x < 0 or self.ball.x > 270 or self.ball.y < 0 or self.ball.y > 430:  #checks if the ball went off of fthe screen, and they lost
            Clock.unschedule(self.update)
            app.game.GameOn = False
            Clock.schedule_once(app.game.start_game_over
                                )  #goes to the game over screen when they die.
        self.ball.bumpers()
        self.Double_touches.text = str(
            self.double_touches
        )  #sets the double touches text in the top right to the new amount

    """This changes the Scroll Height and the points' positions relative to how high on the screen the ball is."""

    def move_screen_up(self):
        if self.ball.y_pos - self.ScrollHeight > 240 and self.ScrollHeight < self.up_token.times_spawned * 300:
            amount_to_scroll = (self.ball.y_pos - self.ScrollHeight -
                                240) * self.line_speed / 20
            self.ScrollHeight += amount_to_scroll
            self.next_point.y -= amount_to_scroll
            self.point[0].y -= amount_to_scroll
            self.point[1].y -= amount_to_scroll
            for i in self.background_lines:
                i.move(amount_to_scroll)

    """This changes the point that moves toward where they clicked. if the tutorial is on, it checks whether they clicked in the box, and runs the tutorial text for that, and also deals with the second part of the tutorial."""

    def new_lines(self):
        if app.game.tutorial_on and self.part_of_tutorial == 1:
            self.part_of_tutorial = 2
            self.tutorial(2)

        self.next_point = Point(
            self.Mouse_x, self.Mouse_y
        )  #this makes the point that the line moves towards where they let go of the mouse
        if app.game.tutorial_on and self.part_of_tutorial == 0:
            if self.Mouse_x > 270 and self.Mouse_x < 290 and self.Mouse_y > 145 and self.Mouse_y < 165:
                self.has_touched = False
                self.can_draw = True
                self.first_line_complete = 1
                self.part_of_tutorial = 1
                self.tutorial(1)

        elif self.first_line_complete == 0:  #This is if it is the first time, (which happens by default becasue of kivy), and doesn't do anything (sets things to false)
            self.has_touched = False
            self.can_draw = True
            self.point_to_move = 1

        elif self.has_touched == False:  #if it is the first click per bounce, it says that they have already clicked, says that they can move the line
            self.has_touched = True
            self.can_draw = True
        else:
            if self.double_touches > 0:  #if this is their second click and they have any more double moves, they lose 1 double move, and can draw.
                self.double_touches -= 1
                self.can_draw = True
            else:
                self.can_draw = False  #they can't draw if they have already clicked and haven't any double moves, nothing happens
        if self.can_draw:
            if self.point_to_move == 0:
                self.point_to_move = 1  #changes the point to move to
            else:
                self.point_to_move = 0
            self.first_line_complete += 1

    """Updates the Mouse positions when they touch down"""

    def on_touch_down(self, touch):
        if app.game.GameOn:
            self.Mouse_x = touch.x
            self.Mouse_y = touch.y
            self.touched = True

    """Updates the Mouse positions when the Mouse moves"""

    def on_touch_move(self, touch):
        if app.game.GameOn:
            self.Mouse_x = touch.x
            self.Mouse_y = touch.y

    """Calls new lines when they lift their mouse (finger)"""

    def on_touch_up(self, touch):
        if app.game.tutorial_on and self.part_of_tutorial == 2:  #for a specific part of the tutorial
            self.part_of_tutorial = 3
            self.tutorial(3)
        elif app.game.GameOn:
            self.new_lines()
            self.touched = False

    """initializes a bunch of Widgets and variables for the game, everything that is neccessary to make a new game."""

    def initialize(self):

        self.ScrollHeight = 0
        self.ball = Ball()
        self.ball.initialize()
        self.add_widget(self.ball)

        sleep(.1)
        self.point = [Point(40, 70), Point(280,
                                           70)]  #makes the starting 2 points

        self.drawn_line = Drawn_line()
        self.drawn_line.draw(
        )  #draws the line so that it shows up in the beggining
        self.add_widget(self.drawn_line)

        self.hypothetical_line = Hypothetical_line()
        self.add_widget(self.hypothetical_line)

        self.line_speed = .1
        self.point_to_move = 0
        self.next_point = Point(
            self.point[0].x, self.point[0].y
        )  #this and the past line make sure that if the line moves toward

        self.speed = (.025)
        self.Mouse_x = self.point[0].x
        self.Mouse_y = self.point[0].y

        self.up_token = Up_token()
        self.up_token.times_spawned = -1  #so that the first time, it sets to 0, which works with the upward scrolling
        self.up_token.spawn()
        self.add_widget(self.up_token)

        self.background_lines = []  #creates the lines in the background evenly
        for i in range(20):
            background_line = Background_Line()
            background_line.create(i * 24)
            self.add_widget(background_line)
        app.game.GameOn = True
        self.touched = False

        self.first_line_complete = 0

        self.score = 0
        self.Score = Label(text=str(self.score),
                           font_size=20,
                           center_x=20,
                           center_y=460)
        self.add_widget(self.Score)

        self.can_draw = False
        self.double_touches = 1
        self.Double_touches = Label(text=str(self.double_touches),
                                    font_size=20,
                                    center_x=300,
                                    center_y=460)
        self.add_widget(self.Double_touches)

        self.ball.move()  #so that it spawns in the middle, not the bottom left
        self.part_of_tutorial = 10  #this makes it so that the varibale exists, but won't interact with anything, because its too high (fixes an error)

        if app.game.tutorial_on:
            self.part_of_tutorial = 0
            self.tutorial(0)
        else:
            Clock.schedule_interval(self.update, 1.0 / 60.0)
Ejemplo n.º 2
0
 def Compare_Res(self, StudId, Test_File):
     db = mysql.connector.connect(host='localhost',
                                  user='******',
                                  passwd='P@$$W0RD')
     my = db.cursor()
     my.execute('use Voice_Comparer')
     sql = 'select * from Student_Data_By_Path where StudentID = %s'
     val = (StudId, )
     Label = []
     my.execute(
         sql,
         val,
     )
     res = my.fetchall()
     for i in res:
         for rep in range(5):
             Label.append(i[2])
     set_Labels = set(Label)
     set_Labels = list(set_Labels)
     content = BoxLayout(orientation='vertical',
                         padding=20,
                         size_hint=(1, 1))
     model = load_model(f"{StudId}_.h5")
     audio = []
     Files = list(Test_File.split(","))
     Test_File = Files[0]
     print(Test_File)
     try:
         Match_Perc = 95
         data, sr = librosa.core.load(Test_File, res_type='kaiser_best')
         audio.append(self.get_MFCC(Test_File, sr, data))
         audio = np.array(audio)
         pred = model.predict(audio)
         print("Predicted Result : ")
         print(" ==>> " + str(pred), ' - ', str(pred.argmax(-1)),
               set_Labels[pred.argmax(axis=-1)[0]])
         c = 0
         res = 1
         res_Label = ''
         for i in pred[0]:
             string = set_Labels[c] + "'s Mataching - " + str(i * 100) + '%'
             print(string)
             if i * 100 > res:
                 res = i * 100
                 res_Label = set_Labels[c]
             c += 1
             content.add_widget(
                 MDLabel(text=string, theme_text_color='Primary'))
         if res > Match_Perc:
             content.add_widget(
                 MDLabel(text=' • It is not a fake call It Matches with ' +
                         res_Label + ' voice',
                         theme_text_color='Primary'))
         else:
             content.add_widget(
                 MDLabel(
                     text=
                     ' • It is a fake call It is not matching with any voice',
                     theme_text_color='Primary'))
     except Exception as e:
         if Test_File.endswith('.wav'):
             content.add_widget(MDLabel(text='' + e))
         else:
             content.add_widget(
                 MDLabel(
                     text=
                     ' • The File format is not acceptable .wav only acceptable '
                 ))
     popup = Popup(title='Percentage of matching ',
                   size_hint=(.8, .5),
                   content=content)
     popup.open()
Ejemplo n.º 3
0
    def Compare_Res(self, StudId, Test_File):
        db = mysql.connector.connect(host='localhost',
                                     user='******',
                                     passwd='root')
        my = db.cursor()
        my.execute('use Voice_Comparer')
        content = BoxLayout(orientation='vertical',
                            padding=20,
                            size_hint=(1, 1))
        sql = 'select * from Student_Data_By_Path where StudentID = %s'
        val = (StudId, )
        path = []
        Label = []
        my.execute(
            sql,
            val,
        )
        res = my.fetchall()
        for i in res:
            path.append(i[1])
            Label.append(i[2])

        print(Test_File)

        print(path)
        print(Label)

        set_Labels = set(Label)
        set_Labels = list(set_Labels)

        print(set_Labels)

        Train_audio = []
        Train_label = []
        epochs = 50
        duration = 30
        sr = 22050
        batch_size = 10
        for i in path:
            data, sr = librosa.load(i,
                                    sr=sr,
                                    duration=duration,
                                    res_type='kaiser_best')
            #data = librosa.decompose.nn_filter(data,aggregate=np.median)
            Train_audio.append(self.get_MFCC(i, sr, data))

        count = 0
        features = np.asarray(())
        gmm = GMM(n_components=16)
        for i in path:
            sr, audio = read(i)
            vector = self.get_MFCC(i, sr, audio)
            if features.size == 0:
                features = vector
            else:
                features = np.vstack((features, vector))
            if count == len(path):
                gmm = GMM(n_components=16,
                          n_iter=100,
                          covariance_type='diag',
                          n_init=3)
                gmm.fit(features)
                #picklefile = path.split("-")[0]+".gmm"
                #cPickle.dump(gmm,open(dest + picklefile,'w'))
                #print '+ modeling completed for speaker:',picklefile," with data point = ",features.shape
                features = np.asarray(())
                count = 0
            count = count + 1

        for i in Label:
            index = set_Labels.index(i)
            Train_label.append(index)

        #num_labels = len(set_Labels)
        audio = []
        Files = list(Test_File.split(","))
        Test_File = Files[0]
        print(Test_File)
        try:
            sr, audio = read(Test_File)
            vec = self.get_MFCC(Test_File, sr, audio)
            pred = gmm.predict(vec)
            print("Predicted Result : ")
            print(pred)
            print(" ==>> " + str(pred), ' - ', str(pred.argmax(-1)),
                  set_Labels[pred.argmax(axis=-1)[0]])
            c = 0
            res = 1
            res_Label = ''
            for i in pred[0]:
                string = set_Labels[c] + "'s Mataching - " + str(i * 100) + '%'
                if i * 100 > res:
                    #print(i , ' - ' , res)
                    res = i * 100
                    res_Label = set_Labels[c]
                #print(string)
                #print(res)
                c += 1
                content.add_widget(
                    MDLabel(text=string, theme_text_color='Primary'))

            if res > 70:
                content.add_widget(
                    MDLabel(text=' • It is not a fake call It Matches with ' +
                            res_Label + ' voice',
                            theme_text_color='Primary'))
            else:
                content.add_widget(
                    MDLabel(
                        text=
                        ' • It is a fake call It is not matching with any voice',
                        theme_text_color='Primary'))
        except Exception as e:
            if Test_File.endswith('.wav'):
                content.add_widget(
                    MDLabel(text='Inserted File is not readable'))
                print(e)
            else:
                content.add_widget(
                    MDLabel(
                        text=
                        ' • The File format is not acceptable .wav only acceptable '
                    ))
        popup = Popup(title='Percentage of matching ',
                      size_hint=(.8, .5),
                      content=content)
        popup.open()
Ejemplo n.º 4
0
 def Insert_DB(self, StudId, File, show_label):
     db = mysql.connector.connect(host='localhost',
                                  user='******',
                                  passwd='P@$$W0RD')
     my = db.cursor()
     my.execute('use Voice_Comparer')
     sql = 'select count(*) from student where StudentId = %s'
     val = (StudId, )
     my.execute(sql, val)
     res = my.fetchall()
     print(File)
     if res[0][0] == 1:
         Files = list(File.split(","))
         for i in Files:
             if i.endswith('.wav'):
                 sql = 'Insert into Student_Data_By_Path values (%s , %s , %s)'
                 val = (
                     StudId,
                     i,
                     show_label,
                 )
                 my.execute(
                     sql,
                     val,
                 )
                 db.commit()
                 print(i + ' Inserted')
             else:
                 print('Sorry!!! only .wav files accepted')
         Snackbar('Inserted ').show()
         self.root.ids.scr_mngr.current = 'Inserting_Screen'
     else:
         Snackbar('student not exist').show()
     sql = 'select * from Student_Data_By_Path where StudentID = %s'
     val = (StudId, )
     path = []
     Label = []
     my.execute(
         sql,
         val,
     )
     res = my.fetchall()
     for i in res:
         for rep in range(5):
             path.append(i[1])
             Label.append(i[2])
     set_Labels = set(Label)
     set_Labels = list(set_Labels)
     Train_audio = []
     Train_label = []
     epochs = 100
     num_labels = len(set_Labels)
     if num_labels > 1:
         for i in path:
             data, sr = librosa.load(i, res_type='kaiser_best')
             Train_audio.append(self.get_MFCC(i, sr, data))
         for i in Label:
             index = set_Labels.index(i)
             Train_label.append(index)
         self.num_labels = len(set_Labels)
         Train_audio = np.array(Train_audio, )
         Train_audio = Train_audio
         Train_label = np.array(Train_label, dtype=np.int)
         print('Model Preparing : =====>>>>>')
         model = Sequential()
         activation_fn = 'glorot_normal'
         model.add(
             layers.Dense(256,
                          input_shape=Train_audio[0].shape,
                          activation='tanh'))
         model.add(Dropout(.3))
         #model.add(layers.Dense(64,activation='tanh'))
         #model.add(Dropout(.3))
         model.add(
             layers.Dense(num_labels, kernel_initializer=activation_fn))
         model.add(Activation('softmax'))
         sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
         model.compile(loss='sparse_categorical_crossentropy',
                       optimizer=sgd,
                       metrics=['accuracy'])
         model.summary()
         model.fit(Train_audio,
                   Train_label,
                   epochs=epochs,
                   validation_split=.1)
         model.save(f"{StudId}_.h5")
         for i in res:
             print(i[0])
             print(i[1])
             print(i[2])
         pass
Ejemplo n.º 5
0
 def Compare_Res(self, StudId, Test_File):
     db = mysql.connector.connect(host='localhost',
                                  user='******',
                                  passwd='root')
     my = db.cursor()
     my.execute('use Voice_Comparer')
     content = BoxLayout(orientation='vertical',
                         padding=20,
                         size_hint=(1, 1))
     sql = 'select * from Student_Data_By_Path where StudentID = %s'
     val = (StudId, )
     path = []
     Label = []
     my.execute(
         sql,
         val,
     )
     res = my.fetchall()
     print('Loading Data : ======>>>>>')
     for i in res:
         path.append(i[1])
         #print(i[1],' - ',i[2])
         Label.append(i[2])
     set_Labels = set(Label)
     set_Labels = list(set_Labels)
     Train_audio = []
     Train_label = []
     epochs = 50
     num_labels = len(set_Labels)
     Match_Perc = 85
     for i in path:
         data, sr = librosa.load(i, res_type='kaiser_best')
         Train_audio.append(self.get_MFCC(i, sr, data))
     for i in Label:
         index = set_Labels.index(i)
         Train_label.append(index)
     self.num_labels = len(set_Labels)
     Train_audio = np.array(Train_audio, )
     Train_label = np.array(Train_label, dtype=np.int)
     print('Model Preparing : =====>>>>>')
     model = Sequential()
     activation_fn = 'glorot_normal'
     model.add(
         layers.Dense(256,
                      input_shape=Train_audio[0].shape,
                      activation='tanh'))
     model.add(Dropout(.3))
     model.add(layers.Dense(64, activation='tanh'))
     model.add(Dropout(.3))
     model.add(layers.Dense(num_labels, init=activation_fn))
     model.add(Activation('softmax'))
     sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
     model.compile(loss='sparse_categorical_crossentropy',
                   optimizer=sgd,
                   metrics=['accuracy'])
     model.summary()
     history = model.fit(Train_audio,
                         Train_label,
                         epochs=epochs,
                         validation_split=.1)
     print(history.history.keys())
     plt.plot(history.history['accuracy'])
     plt.plot(history.history['val_accuracy'])
     plt.title('model accuracy')
     plt.ylabel('accuracy')
     plt.xlabel('epoch')
     plt.legend(['train', 'test'], loc='upper left')
     plt.show()
     plt.plot(history.history['loss'])
     plt.plot(history.history['val_loss'])
     plt.title('model loss')
     plt.ylabel('loss')
     plt.xlabel('epoch')
     plt.legend(['train', 'test'], loc='upper left')
     plt.show()
     audio = []
     Files = list(Test_File.split(","))
     Test_File = Files[0]
     print(Test_File)
     try:
         data, sr = librosa.core.load(Test_File, res_type='kaiser_best')
         audio.append(self.get_MFCC(Test_File, sr, data))
         audio = np.array(audio)
         pred = model.predict(audio)
         print("Predicted Result : ")
         print(" ==>> " + str(pred), ' - ', str(pred.argmax(-1)),
               set_Labels[pred.argmax(axis=-1)[0]])
         c = 0
         res = 1
         res_Label = ''
         for i in pred[0]:
             string = set_Labels[c] + "'s Mataching - " + str(i * 100) + '%'
             print(string)
             if i * 100 > res:
                 res = i * 100
                 res_Label = set_Labels[c]
             c += 1
             content.add_widget(
                 MDLabel(text=string, theme_text_color='Primary'))
         if res > Match_Perc:
             content.add_widget(
                 MDLabel(text=' • It is not a fake call It Matches with ' +
                         res_Label + ' voice',
                         theme_text_color='Primary'))
         else:
             content.add_widget(
                 MDLabel(
                     text=
                     ' • It is a fake call It is not matching with any voice',
                     theme_text_color='Primary'))
     except:
         if Test_File.endswith('.wav'):
             content.add_widget(
                 MDLabel(text='Inserted File is not readable'))
         else:
             content.add_widget(
                 MDLabel(
                     text=
                     ' • The File format is not acceptable .wav only acceptable '
                 ))
     popup = Popup(title='Percentage of matching ',
                   size_hint=(.8, .5),
                   content=content)
     popup.open()