Beispiel #1
0
def main():
    if len(argv) < 4:
        exit(1)

    model_file = argv[1]
    recording_files = argv[2:]

    feature_extractor = PiNet()

    stdout.write("Loading")
    xs = []
    ys = []
    class_count = {}

    for i, filename in enumerate(recording_files):
        stdout.write(' %s' % filename)
        stdout.flush()
        with open(filename, 'rb') as f:
            x = load(f)
            features = [feature_extractor.features(f) for f in x]
            label = np.zeros((len(recording_files), ))
            label[i] = 1.
            xs += features
            ys += [label] * len(x)
            class_count[i] = len(x)
    stdout.write('\n')

    classifier = make_classifier(xs[0].shape, len(recording_files))

    classifier.fit([np.array(xs)], [np.array(ys)], epochs=20, shuffle=True)

    classifier.save(model_file)
Beispiel #2
0
def main():
    if len(argv) < 4:
        print("""Usage: train.py MODEL RECORDING_FILES...
Saves a MODEL after training a classifier for 10 iterations to distinguish between
two or more RECORDING_FILES (see record.py to create these).""")
        exit(1)

    model_file = argv[1]
    recording_files = argv[2:]

    print('Loading tensorflow feature extractor...')
    feature_extractor = PiNet()

    # Load the data from the recording files and hope we don't run out of RAM
    # TensorFlow and Keras support streaming data from files if you want to use more data
    stdout.write("Loading")
    xs = []
    ys = []
    class_count = {}

    # We loop through the recording files and preprocess each frame with the NN feature extractor
    # These features are what our classifier will use as inputs (xs)
    # It will try to predict which file id they correspond to (ys)
    for i, filename in enumerate(recording_files):
        stdout.write(' %s' % filename)
        stdout.flush()
        with open(filename, 'rb') as f:
            x = load(f, encoding='iso-8859-1')
            #x = pickle._Unpickler(f).load()
            features = [feature_extractor.features(f) for f in x]
            label = np.zeros((len(recording_files), ))
            label[
                i] = 1.  # Make a label with a 1 in the column for the file for this frame
            xs += features  # Add the features for the frames loaded from this file
            ys += [label] * len(x)  # Add a label for each frame in the file
            class_count[i] = len(x)
    stdout.write('\n')

    print("Creating a network to classify %s" % ', '.join(recording_files))
    classifier = make_classifier(xs[0].shape, len(recording_files))

    print("Training the network to map high-level features to %d categories" %
          len(recording_files))
    classifier.fit([np.array(xs)], [np.array(ys)], epochs=20, shuffle=True)

    print("Now we save this model so we can deploy it whenever we want")
    classifier.save(model_file)

    print("All done, model saved in %s" % model_file)
Beispiel #3
0
def main(clientsocket, servercommand):
    model_file = 'records/model.h5'
    recording_files = 'records/req', 'records/rne'

    clientsocket.send(servercommand + ': Loading...\n')
    print('Loading tensorflow feature extractor...')
    feature_extractor = PiNet()

    # Load the data from the recording files and hope we don't run out of RAM
    # TensorFlow and Keras support streaming data from files if you want to use more data
    stdout.write("Loading")
    xs = []
    ys = []
    class_count = {}

    # We loop through the recording files and preprocess each frame with the NN feature extractor
    # These features are what our classifier will use as inputs (xs)
    # It will try to predict which file id they correspond to (ys)
    clientsocket.send(servercommand + ': Loading records...\n')
    for i, filename in enumerate(recording_files):
        stdout.write(' %s' % filename)
        stdout.flush()
        with open(filename, 'rb') as f:
            x = load(f)
            features = [feature_extractor.features(f) for f in x]
            label = np.zeros((len(recording_files),))
            label[i] = 1.          # Make a label with a 1 in the column for the file for this frame
            xs += features         # Add the features for the frames loaded from this file
            ys += [label] * len(x) # Add a label for each frame in the file
            class_count[i] = len(x)
    stdout.write('\n')

    clientsocket.send(servercommand + ': Training...\n')
    print("Creating a network to classify %s" % ', '.join(recording_files))
    classifier = make_classifier(xs[0].shape, len(recording_files))

    print("Training the network to map high-level features to %d categories" % len(recording_files))
    classifier.fit([np.array(xs)], [np.array(ys)], epochs=20, shuffle=True)

    clientsocket.send(servercommand + ': Saving the model...\n')
    print("Now we save this model so we can deploy it whenever we want")
    classifier.save(model_file)

    clientsocket.send(servercommand + '-ACK: Model saved\n')
    print("All done, model saved in %s" % model_file)
Beispiel #4
0
def main():
    if len(argv) != 2 or argv[1] == '--help':
        print("""Usage: run.py MODEL
Use MODEL to classify camera frames and play sounds when class 0 is recognised.""")
        exit(1)

    model_file = argv[1]

    # We use the same MobileNet as during recording to turn images into features
    print('Loading feature extractor')
    extractor = PiNet()

    # Here we load our trained classifier that turns features into categories
    print('Loading classifier')
    classifier = keras.models.load_model(model_file)

    # Initialize the camera and sound systems
    camera = Camera(training_mode=False)
    random_sound = RandomSound()


    # Create a preview window so we can see if we are in frame or not
    if SHOW_UI:
        pygame.display.init()
        pygame.display.set_caption('Loading')
        screen = pygame.display.set_mode((512, 512))

    # Smooth the predictions to avoid interruptions to audio
    smoothed = np.ones(classifier.output_shape[1:])
    smoothed /= len(smoothed)

    print('Now running!')
    while True:
        raw_frame = camera.next_frame()

        # Use MobileNet to get the features for this frame
        z = extractor.features(raw_frame)

        # With these features we can predict a 'normal' / 'yeah' class (0 or 1)
        # Keras expects an array of inputs and produces an array of outputs
        classes = classifier.predict(np.array([z]))[0]

        # smooth the outputs - this adds latency but reduces interruptions
        smoothed = smoothed * SMOOTH_FACTOR + classes * (1.0 - SMOOTH_FACTOR)
        selected = np.argmax(smoothed) # The selected class is the one with highest probability

        # Show the class probabilities and selected class
        summary = 'Class %d [%s]' % (selected, ' '.join('%02.0f%%' % (99 * p) for p in smoothed))
        stderr.write('\r' + summary)

        # Perform actions for selected class. In this case, play a sound from the sounds/ dir
        if selected == 0:
            random_sound.play_from('sounds/')
        else:
            random_sound.stop()

        # Show the image in a preview window so you can tell if you are in frame
        if SHOW_UI:
            pygame.display.set_caption(summary)
            surface = pygame.surfarray.make_surface(raw_frame)
            screen.blit(pygame.transform.smoothscale(surface, (512, 512)), (0, 0))
            pygame.display.flip()

            for evt in pygame.event.get():
                if evt.type == pygame.QUIT:
                    pygame.quit()
                    break
Beispiel #5
0
def main():
    if len(argv) != 2 or argv[1] == '--help':
        exit(1)

    model_file = argv[1]

    extractor = PiNet()

    classifier = keras.models.load_model(model_file)

    camera = Camera(training_mode=False)

    if SHOW_UI:
        pygame.display.init()
        pygame.display.set_caption('Loading')
        screen = pygame.display.set_mode((512, 512))

    smoothed = np.ones(classifier.output_shape[1:])
    smoothed /= len(smoothed)

    isLampOn = False
    last_selected = None
    inRoom = False

    print('Now running!')
    while True:
        raw_frame = camera.next_frame()

        z = extractor.features(raw_frame)

        classes = classifier.predict(np.array([z]))[0]

        smoothed = smoothed * SMOOTH_FACTOR + classes * (1.0 - SMOOTH_FACTOR)
        selected = np.argmax(smoothed)

        summary = 'Class %d [%s]' % (selected, ' '.join('%02.0f%%' % (99 * p)
                                                        for p in smoothed))
        stderr.write('\r' + summary)

        ###        #python train.py models/model1.h5  records/lights records/random records/start_music records/stop_music records/greeting records/empty
        try:
            if selected == 0 and last_selected != 0:
                last_selected = 0
                if isLampOn:
                    requests.post(url_switch_off)
                    isLampOn = False

                else:
                    requests.post(url_switch_on)
                    isLampOn = True

            elif selected == 1 and last_selected != 1:
                last_selected = 1
                pass
            elif selected == 2 and last_selected != 2:
                last_selected = 2
                musicplay.play_song()
            elif selected == 3 and last_selected != 3:
                last_selected = 3
                musicplay.stop_music()
            elif selected == 4 and last_selected != 4:
                last_selected = 4
                if inRoom == False:
                    musicplay.play_sound_GE()
                    inRoom = True
            elif selected == 5 and last_selected != 5:
                last_selected = 5
                requests.post(url_switch_off)
                inRoom = False
        except Exception:
            print("UNABLE TO SEND COMMAND TO SONOFF\n")

        if SHOW_UI:
            pygame.display.set_caption(summary)
            surface = pygame.surfarray.make_surface(raw_frame)
            screen.blit(pygame.transform.smoothscale(surface, (512, 512)),
                        (0, 0))
            pygame.display.flip()

            for evt in pygame.event.get():
                if evt.type == pygame.QUIT:
                    pygame.quit()
                    break
Beispiel #6
0
def main():
    # ---- camera init
    uh.set_layout(uh.PHAT)
    uh.brightness(1)
    #------

    model_file = argv[1]
    countdown = int(argv[2])


    # We use the same MobileNet as during recording to turn images into features
    print('Loading feature extractor')
    extractor = PiNet()


    # Here we load our trained classifier that turns features into categories
    print('Loading classifier')
    classifier = keras.models.load_model(model_file)

    
    # Initialize the camera and sound systems
    camera = Camera(training_mode=False)
    #random_sound = RandomSound()

    # Create a preview window so we can see if we are in frame or not
   

    # Smooth the predictions to avoid interruptions to audio
    smoothed = np.ones(classifier.output_shape[1:])
    smoothed /= len(smoothed)
    
    # init logger
    logger = logging.getLogger('bios-fish')
    hdlr = logging.FileHandler('/home/pi/bios_fish/log.txt')
    formatter = logging.Formatter('%(asctime)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)

    print('Now running!')
    while countdown > 0 :
        raw_frame = camera.next_frame()

        # Use MobileNet to get the features for this frame
        z = extractor.features(raw_frame)

        # With these features we can predict a 'normal' / 'yeah' class (0 or 1)
        # Keras expects an array of inputs and produces an array of outputs
        classes = classifier.predict(np.array([z]))[0]

        # smooth the outputs - this adds latency but reduces interruptions
        smoothed = smoothed * 0.8 + classes * (1.0 - 0.8)
        selected = np.argmax(smoothed) # The selected class is the one with highest probability
        
        calculateRed=math.floor(smoothed[0] * 255)
        calculatedGreen=math.floor(smoothed[1] * 255) + math.floor(smoothed[0] * 50)
        calculatedBlue=math.floor(smoothed[2] * 255) + math.floor(smoothed[0] * 50)
        calculatedWhite=math.floor(smoothed[3] * 255)
        if (calculateRed + calculatedWhite > 255):
            calculateRed = 255
        else:
            calculateRed = calculateRed + calculatedWhite
            
        if (calculatedGreen + calculatedWhite > 255):
            calculatedGreen = 255
        else:
            calculatedGreen = calculatedGreen + calculatedWhite
            
        if (calculatedBlue + calculatedWhite > 255):
            calculatedBlue = 255
        else:
            calculatedBlue = calculatedBlue + calculatedWhite
            
        uh.set_all(calculateRed, calculatedGreen, calculatedBlue)
        uh.show()

        # Show the class probabilities and selected class
        summary = 'Class %d [%s]' % (selected, ' '.join('%02.0f%%' % (99 * p) for p in smoothed))
        stderr.write('\r' + summary)
        logger.info(summary)

        # Perform actions for selected class. In this case, play a sound from the sounds/ dir
#        if selected == 0:
#            random_sound.play_from('sounds/')
#        else:
#            random_sound.stop()

        # Show the image in a preview window so you can tell if you are in frame
        time.sleep(0.2)
        countdown = countdown - 1