Exemplo n.º 1
0
 def __init__(self, gestures_file, wiimotes, first_wm, num_motes,
              frame_freq):
     self.writer_reader = GestureWriterReader(gestures_file)
     self.wiimotes = wiimotes
     self.first_wm = first_wm
     self.num_motes = num_motes
     self.frame_freq = frame_freq
     self.known_gestures = self.writer_reader.get_learned_gestures()
Exemplo n.º 2
0
 def __init__(self, gestures_file, wiimotes, first_wm, num_motes, frame_freq):
     self.writer_reader = GestureWriterReader(gestures_file)
     self.wiimotes = wiimotes
     self.first_wm = first_wm
     self.num_motes = num_motes
     self.frame_freq = frame_freq
     self.known_gestures = self.writer_reader.get_learned_gestures()
Exemplo n.º 3
0
 def __init__(self, stat_file, gesture_file):
     self.stat_wr = StatWR(stat_file)
     self.gesture_writer_reader = GestureWriterReader(gesture_file)
     self.total_attempts = self.stat_wr.get_attempts()
     self.total_successes = self.stat_wr.get_successes()
Exemplo n.º 4
0
class GeStat:
    def __init__(self, stat_file, gesture_file):
        self.stat_wr = StatWR(stat_file)
        self.gesture_writer_reader = GestureWriterReader(gesture_file)
        self.total_attempts = self.stat_wr.get_attempts()
        self.total_successes = self.stat_wr.get_successes()

    # Updates a gesture's statistics according to whether or not
    # a gesture was performed successfully.
    def confirm(self, gesture, was_successful):

        # Increment the gesture attempts, and the total attempts
        gesture.incr_attempts()
        self.incr_total_attempts()

        # If the gesture was confirmed to be successful, increment the gesture successes
        # and the total successes.
        if was_successful:
            gesture.incr_successes()
            self.incr_total_successes()

        self.gesture_writer_reader.update_gesture(gesture)

    # Returns the success rate of a gesture.
    # If the gesture has never been recognized, returns a success rate of 0.
    @staticmethod
    def get_gesture_success_rate(gesture):

        if gesture.get_attempts() != 0:
            success_rate = float(gesture.get_successes()) / float(
                gesture.get_attempts())
            return success_rate
        else:
            # Gesture has never been recognized.
            return 0

    # Convenience method. Returns failure rate of a Gesture.
    # i.e., the rate of times this gesture was mistakenly performed.
    def get_failure_rate(self, gesture):
        return 1 - self.get_gesture_success_rate(gesture)

    # Returns the total success rate of BlueMote. (total successes over total attempts).
    def get_total_success_rate(self):
        if self.total_attempts != 0:
            return float(self.total_successes) / float(self.total_attempts)
        else:
            return 0

    # Increments successes, and updates the stats file.
    def incr_total_successes(self):
        self.total_successes += 1
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Increments attempts. Also updates the stats file.
    def incr_total_attempts(self):
        self.total_attempts += 1
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Sets total successes to the given number.  Updates stats file.
    def set_total_successes(self, num_successes):
        self.total_successes = num_successes
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Sets total attempts to the given number.  Updates stats file.
    def set_total_attempts(self, num_attempts):
        self.total_attempts = num_attempts
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Resets the total statistics to 0 attempts and 0 successes.
    def reset_total_stats(self):
        self.stat_wr.reset_stats()
        self.set_total_attempts(0)
        self.set_total_successes(0)

    # Resets all gesture statistics to 0 attempts and 0 successes
    def reset_gesture_stats(self):
        known_gestures = self.gesture_writer_reader.get_learned_gestures()
        for g in known_gestures:
            g.reset_stats()

        self.gesture_writer_reader.overwrite_gestures(known_gestures)

    # Resets all gesture statistics and the total statistics.
    def reset_all_stats(self):
        self.reset_total_stats()
        self.reset_gesture_stats()

    # Prints the statistics of each gesture and the total stats.
    def print_stats(self):
        known_gestures = self.gesture_writer_reader.get_learned_gestures()
        print "\n".ljust(15 + 18 + 12 + 12, '*')
        print "Statistics:"
        print "\n\n" + "Number of known gestures: " + str(len(known_gestures))
        print "\n\n   Name".ljust(15) + "Success rate".ljust(18) + "Successes".ljust(12) + "Attempts".ljust(12) \
              + "\n"
        for g in known_gestures:
            print "   " + g.get_name().ljust(15) + str(self.get_gesture_success_rate(g)).ljust(18) + \
            str(g.get_successes()).ljust(12) + str(g.get_attempts()).ljust(12)

        print "\n   Total Success Rate".ljust(25) + "Total Successes".ljust(
            20) + "Total Attempts".ljust(20)
        print "   " + str(self.get_total_success_rate()).ljust(25) + str(self.total_successes).ljust(20) \
        + str(self.total_attempts).ljust(20) + "\n\n"
Exemplo n.º 5
0
# i.e., every nth frame is captured and stored.
REPETITION_LIMIT = 5
STANDARD_SLEEP_TIME = 0.1

gestures_file = 'gestures.txt'
stats_file = 'gesture_stats.txt'
if len(sys.argv) > 2:
    if sys.argv[1] == 't':
        testing = True
    else:
        gestures_file = str(sys.argv[1])
        stats_file = str(sys.argv[2])

full_gestures_file = 'full_' + gestures_file

writer_reader = GestureWriterReader(
    gestures_file)  # Initializes the writer-reader to the either general use or testing.
stat = GeStat(stats_file, gestures_file)
full_writer_reader = GestureWriterReader(full_gestures_file)  # writer-reader for full gestures.
wiimotes = wiiuse.init(num_motes)
first_wm = wiimotes[0]
# Handles button press events of the wiimote.
button_handler = ButtonHandler(wiimotes, first_wm, num_motes)
# Object to perform gesture comparison.
gesture_matcher = GestureMatcher(writer_reader)
# Object that creates gesture objects.
gesture_creator = GestureCreator(gestures_file, wiimotes, first_wm, num_motes, FRAME_FREQ)


# The main prompt of the program
def main_prompt():
    print "\n\n**********************"
Exemplo n.º 6
0
class GestureCreator:

    def __init__(self, gestures_file, wiimotes, first_wm, num_motes, frame_freq):
        self.writer_reader = GestureWriterReader(gestures_file)
        self.wiimotes = wiimotes
        self.first_wm = first_wm
        self.num_motes = num_motes
        self.frame_freq = frame_freq
        self.known_gestures = self.writer_reader.get_learned_gestures()
        # self.full_gestures = self.writer_reader.get_full_gestures()

    # Takes iterations of a gesture, and averages them frame-by-frame.
    def average_gesture(self, frame_arr):
        min_length = float('inf')

        # Find the shortest of the gestures for indexing purposes.
        for frames in frame_arr:
            if len(frames) < min_length:
                min_length = len(frames)

        if min_length == 0:
            print "Error:  A gesture had no length.  Exiting."
            exit(1)

        avg_frames = []

        for i in range(0, min_length):
            roll_sum = 0.0
            pitch_sum = 0.0
            acc_x_sum = 0.0
            acc_y_sum = 0.0
            acc_z_sum = 0.0
            for j in range (0, len(frame_arr)):
                roll_sum += frame_arr[j][i][0]   # Roll reading for this frame
                pitch_sum += frame_arr[j][i][1]  # Pitch reading for this frame
                acc_x_sum += frame_arr[j][i][2]  # X accelerometer
                acc_y_sum += frame_arr[j][i][3]  # Y accelerometer
                acc_z_sum += frame_arr[j][i][4]  # Z accelerometer
            avg_frames.append((roll_sum/len(frame_arr), pitch_sum/len(frame_arr), acc_x_sum/len(frame_arr),
                               acc_y_sum/len(frame_arr), acc_z_sum/len(frame_arr)))

        return avg_frames

    # Prompts the user to go through the process of learning a gesture,
    # (Repeating the gesture several times, averaging these repetitions,
    # and writing this average to the gestures.txt file.)
    # rep_limit:  The number of repetitions to learn the gesture.
    def learn_gesture(self, rep_limit):

        repetitions = 0         # How many times a gesture has been repeated.
        done = False
        confirmed = False

        while not confirmed:
            gest_name = raw_input("Please name your gesture: ")

            if not (self.get_gesture_names().__contains__(gest_name) or
                    len(gest_name) > 15):
                confirmed = True
            else:
                print "\nGesture name taken or too long.  Please choose another.\n"
        gest_action = raw_input("Please specify an command for your gesture: ")

        gest_arg_arr = shlex.split(gest_action)
        frame_arr = []

        print "\nPress and hold B, perform your gesture, and then release B."

        i = 0
        while not done:
            r = wiiuse.poll(self.wiimotes, self.num_motes)
            if r != 0:

                if wiiuse.is_just_pressed(self.first_wm[0], wiiuse.button['B']):
                    print "Learning gesture, round " + str(repetitions+1) + "."

                    frames = []

                if wiiuse.is_held(self.first_wm[0], wiiuse.button['B']):
                    roll = self.first_wm[0].orient.roll
                    pitch = self.first_wm[0].orient.pitch
                    acc_x = self.first_wm[0].gforce.x
                    acc_y = self.first_wm[0].gforce.y
                    acc_z = self.first_wm[0].gforce.z

                    frame = (roll, pitch, acc_x, acc_y, acc_z)

                    # Only add every nth frame to the list.
                    if i%self.frame_freq == 0:
                        frames.append(frame)

                if repetitions >= rep_limit:
                    done = True
                    print "Good! Press A to see your gesture added to the list of known gestures."
                    continue

                if wiiuse.is_released(self.first_wm[0], wiiuse.button['B']):
                    repetitions += 1
                    frame_arr.append(frames)
                    print str(rep_limit-repetitions) + " repetition(s) remaining."

                i += 1

        # Creating the average of the repetitions to create the learned gesture.
        gesture_average = self.average_gesture(frame_arr)

        # Writing Gesture to the gesture file.
        gesture = LearnedGesture(gest_name, gesture_average, gest_arg_arr, 0, 0, frame_arr)
        self.writer_reader.write_gesture(gesture)

        # Update the current known gestures.
        self.update_gestures()

    # Collects the data of a gesture to be compared to known gestures.
    def perform_gesture(self):
        done = False

        if len(self.known_gestures) == 0:
            return
        i = 0
        frames = []
        while not done:
            r = wiiuse.poll(self.wiimotes, self.num_motes)
            if r != 0:

                if wiiuse.is_held(self.first_wm[0], wiiuse.button['B']):
                    roll = self.first_wm[0].orient.roll
                    pitch = self.first_wm[0].orient.pitch
                    acc_x = self.first_wm[0].gforce.x
                    acc_y = self.first_wm[0].gforce.y
                    acc_z = self.first_wm[0].gforce.z

                    frame = (roll, pitch, acc_x, acc_y, acc_z)

                    # Only add every nth frame to the list.
                    if i % self.frame_freq == 0:
                        frames.append(frame)

                if wiiuse.is_released(self.first_wm[0], wiiuse.button['B']):
                    done = True
                i += 1

        gesture = PerformedGesture(frames)
        return gesture

    def update_gestures(self):
        self.known_gestures = self.writer_reader.get_learned_gestures()
        # self.full_gestures = self.writer_reader.get_full_gestures()

    def get_gesture_names(self):
        names = []
        for g in self.known_gestures:
            names.append(g.get_name())

        return names
Exemplo n.º 7
0
class GestureCreator:
    def __init__(self, gestures_file, wiimotes, first_wm, num_motes,
                 frame_freq):
        self.writer_reader = GestureWriterReader(gestures_file)
        self.wiimotes = wiimotes
        self.first_wm = first_wm
        self.num_motes = num_motes
        self.frame_freq = frame_freq
        self.known_gestures = self.writer_reader.get_learned_gestures()
        # self.full_gestures = self.writer_reader.get_full_gestures()

    # Takes iterations of a gesture, and averages them frame-by-frame.
    def average_gesture(self, frame_arr):
        min_length = float('inf')

        # Find the shortest of the gestures for indexing purposes.
        for frames in frame_arr:
            if len(frames) < min_length:
                min_length = len(frames)

        if min_length == 0:
            print "Error:  A gesture had no length.  Exiting."
            exit(1)

        avg_frames = []

        for i in range(0, min_length):
            roll_sum = 0.0
            pitch_sum = 0.0
            acc_x_sum = 0.0
            acc_y_sum = 0.0
            acc_z_sum = 0.0
            for j in range(0, len(frame_arr)):
                roll_sum += frame_arr[j][i][0]  # Roll reading for this frame
                pitch_sum += frame_arr[j][i][1]  # Pitch reading for this frame
                acc_x_sum += frame_arr[j][i][2]  # X accelerometer
                acc_y_sum += frame_arr[j][i][3]  # Y accelerometer
                acc_z_sum += frame_arr[j][i][4]  # Z accelerometer
            avg_frames.append(
                (roll_sum / len(frame_arr), pitch_sum / len(frame_arr),
                 acc_x_sum / len(frame_arr), acc_y_sum / len(frame_arr),
                 acc_z_sum / len(frame_arr)))

        return avg_frames

    # Prompts the user to go through the process of learning a gesture,
    # (Repeating the gesture several times, averaging these repetitions,
    # and writing this average to the gestures.txt file.)
    # rep_limit:  The number of repetitions to learn the gesture.
    def learn_gesture(self, rep_limit):

        repetitions = 0  # How many times a gesture has been repeated.
        done = False
        confirmed = False

        while not confirmed:
            gest_name = raw_input("Please name your gesture: ")

            if not (self.get_gesture_names().__contains__(gest_name)
                    or len(gest_name) > 15):
                confirmed = True
            else:
                print "\nGesture name taken or too long.  Please choose another.\n"
        gest_action = raw_input("Please specify an command for your gesture: ")

        gest_arg_arr = shlex.split(gest_action)
        frame_arr = []

        print "\nPress and hold B, perform your gesture, and then release B."

        i = 0
        while not done:
            r = wiiuse.poll(self.wiimotes, self.num_motes)
            if r != 0:

                if wiiuse.is_just_pressed(self.first_wm[0],
                                          wiiuse.button['B']):
                    print "Learning gesture, round " + str(repetitions +
                                                           1) + "."

                    frames = []

                if wiiuse.is_held(self.first_wm[0], wiiuse.button['B']):
                    roll = self.first_wm[0].orient.roll
                    pitch = self.first_wm[0].orient.pitch
                    acc_x = self.first_wm[0].gforce.x
                    acc_y = self.first_wm[0].gforce.y
                    acc_z = self.first_wm[0].gforce.z

                    frame = (roll, pitch, acc_x, acc_y, acc_z)

                    # Only add every nth frame to the list.
                    if i % self.frame_freq == 0:
                        frames.append(frame)

                if repetitions >= rep_limit:
                    done = True
                    print "Good! Press A to see your gesture added to the list of known gestures."
                    continue

                if wiiuse.is_released(self.first_wm[0], wiiuse.button['B']):
                    repetitions += 1
                    frame_arr.append(frames)
                    print str(rep_limit -
                              repetitions) + " repetition(s) remaining."

                i += 1

        # Creating the average of the repetitions to create the learned gesture.
        gesture_average = self.average_gesture(frame_arr)

        # Writing Gesture to the gesture file.
        gesture = LearnedGesture(gest_name, gesture_average, gest_arg_arr, 0,
                                 0, frame_arr)
        self.writer_reader.write_gesture(gesture)

        # Update the current known gestures.
        self.update_gestures()

    # Collects the data of a gesture to be compared to known gestures.
    def perform_gesture(self):
        done = False

        if len(self.known_gestures) == 0:
            return
        i = 0
        frames = []
        while not done:
            r = wiiuse.poll(self.wiimotes, self.num_motes)
            if r != 0:

                if wiiuse.is_held(self.first_wm[0], wiiuse.button['B']):
                    roll = self.first_wm[0].orient.roll
                    pitch = self.first_wm[0].orient.pitch
                    acc_x = self.first_wm[0].gforce.x
                    acc_y = self.first_wm[0].gforce.y
                    acc_z = self.first_wm[0].gforce.z

                    frame = (roll, pitch, acc_x, acc_y, acc_z)

                    # Only add every nth frame to the list.
                    if i % self.frame_freq == 0:
                        frames.append(frame)

                if wiiuse.is_released(self.first_wm[0], wiiuse.button['B']):
                    done = True
                i += 1

        gesture = PerformedGesture(frames)
        return gesture

    def update_gestures(self):
        self.known_gestures = self.writer_reader.get_learned_gestures()
        # self.full_gestures = self.writer_reader.get_full_gestures()

    def get_gesture_names(self):
        names = []
        for g in self.known_gestures:
            names.append(g.get_name())

        return names
Exemplo n.º 8
0
REPETITION_LIMIT = 5
STANDARD_SLEEP_TIME = 0.1

gestures_file = 'gestures.txt'
stats_file = 'gesture_stats.txt'
if len(sys.argv) > 2:
    if sys.argv[1] == 't':
        testing = True
    else:
        gestures_file = str(sys.argv[1])
        stats_file = str(sys.argv[2])

full_gestures_file = 'full_' + gestures_file

writer_reader = GestureWriterReader(
    gestures_file
)  # Initializes the writer-reader to the either general use or testing.
stat = GeStat(stats_file, gestures_file)
full_writer_reader = GestureWriterReader(
    full_gestures_file)  # writer-reader for full gestures.
wiimotes = wiiuse.init(num_motes)
first_wm = wiimotes[0]
# Handles button press events of the wiimote.
button_handler = ButtonHandler(wiimotes, first_wm, num_motes)
# Object to perform gesture comparison.
gesture_matcher = GestureMatcher(writer_reader)
# Object that creates gesture objects.
gesture_creator = GestureCreator(gestures_file, wiimotes, first_wm, num_motes,
                                 FRAME_FREQ)

Exemplo n.º 9
0
 def __init__(self, stat_file, gesture_file):
     self.stat_wr = StatWR(stat_file)
     self.gesture_writer_reader = GestureWriterReader(gesture_file)
     self.total_attempts = self.stat_wr.get_attempts()
     self.total_successes = self.stat_wr.get_successes()
Exemplo n.º 10
0
class GeStat:

    def __init__(self, stat_file, gesture_file):
        self.stat_wr = StatWR(stat_file)
        self.gesture_writer_reader = GestureWriterReader(gesture_file)
        self.total_attempts = self.stat_wr.get_attempts()
        self.total_successes = self.stat_wr.get_successes()

    # Updates a gesture's statistics according to whether or not
    # a gesture was performed successfully.
    def confirm(self, gesture, was_successful):

        # Increment the gesture attempts, and the total attempts
        gesture.incr_attempts()
        self.incr_total_attempts()

        # If the gesture was confirmed to be successful, increment the gesture successes
        # and the total successes.
        if was_successful:
            gesture.incr_successes()
            self.incr_total_successes()

        self.gesture_writer_reader.update_gesture(gesture)

    # Returns the success rate of a gesture.
    # If the gesture has never been recognized, returns a success rate of 0.
    @staticmethod
    def get_gesture_success_rate(gesture):

        if gesture.get_attempts() != 0:
            success_rate = float(gesture.get_successes())/float(gesture.get_attempts())
            return success_rate
        else:
            # Gesture has never been recognized.
            return 0

    # Convenience method. Returns failure rate of a Gesture.
    # i.e., the rate of times this gesture was mistakenly performed.
    def get_failure_rate(self, gesture):
        return 1-self.get_gesture_success_rate(gesture)

    # Returns the total success rate of BlueMote. (total successes over total attempts).
    def get_total_success_rate(self):
        if self.total_attempts != 0:
            return float(self.total_successes) / float(self.total_attempts)
        else:
            return 0

    # Increments successes, and updates the stats file.
    def incr_total_successes(self):
        self.total_successes += 1
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Increments attempts. Also updates the stats file.
    def incr_total_attempts(self):
        self.total_attempts += 1
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Sets total successes to the given number.  Updates stats file.
    def set_total_successes(self, num_successes):
        self.total_successes = num_successes
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Sets total attempts to the given number.  Updates stats file.
    def set_total_attempts(self, num_attempts):
        self.total_attempts = num_attempts
        self.stat_wr.update_stats(self.total_attempts, self.total_successes)

    # Resets the total statistics to 0 attempts and 0 successes.
    def reset_total_stats(self):
        self.stat_wr.reset_stats()
        self.set_total_attempts(0)
        self.set_total_successes(0)

    # Resets all gesture statistics to 0 attempts and 0 successes
    def reset_gesture_stats(self):
        known_gestures = self.gesture_writer_reader.get_learned_gestures()
        for g in known_gestures:
            g.reset_stats()

        self.gesture_writer_reader.overwrite_gestures(known_gestures)

    # Resets all gesture statistics and the total statistics.
    def reset_all_stats(self):
        self.reset_total_stats()
        self.reset_gesture_stats()

    # Prints the statistics of each gesture and the total stats.
    def print_stats(self):
        known_gestures = self.gesture_writer_reader.get_learned_gestures()
        print "\n".ljust(15+18+12+12, '*')
        print "Statistics:"
        print "\n\n" + "Number of known gestures: " + str(len(known_gestures))
        print "\n\n   Name".ljust(15) + "Success rate".ljust(18) + "Successes".ljust(12) + "Attempts".ljust(12) \
              + "\n"
        for g in known_gestures:
            print "   " + g.get_name().ljust(15) + str(self.get_gesture_success_rate(g)).ljust(18) + \
            str(g.get_successes()).ljust(12) + str(g.get_attempts()).ljust(12)

        print "\n   Total Success Rate".ljust(25) + "Total Successes".ljust(20) + "Total Attempts".ljust(20)
        print "   " + str(self.get_total_success_rate()).ljust(25) + str(self.total_successes).ljust(20) \
        + str(self.total_attempts).ljust(20) + "\n\n"