Example #1
0
def classify_images(type):
    X_test, y_test = load_mnist('Data/', kind='t10k')
    if type == "google":
        message_broker = MessageBroker("google")

        # Listening to print out classes
        consumer = message_broker.generate_consumer(type='google', topic_name='classes', gcallback=callback)

        # Sending images
        publisher = message_broker.generate_producer(type='google')
        for img in X_test:
            print("Sending an image.")
            message = img.tobytes()
            message_broker.publish_message(type='google', producer_instance=publisher, topic_name='images', value=message)
            time.sleep(10)

        try:
           consumer.result()
        except KeyboardInterrupt:
           consumer.cancel()

    if type == 'kafka':
        # Sending images
        message_broker = MessageBroker(type='kafka')
        for img in X_test:
            print("Sending an image.")
            producer = message_broker.generate_producer(type="kafka")
            message_broker.publish_message(producer_instance=producer, topic_name="images", key='parsed',
                                           value=img.tobytes(), type='kafka')
            time.sleep(10)
Example #2
0
def kafka_testtopic():
    #Tests run on a test topic for simple communication testing of the adapter

    #Consume
    running = True
    message_broker = MessageBroker(type='kafka')
    consumer = message_broker.generate_consumer(type='kafka', topic_name='test', auto_offset_reset='earliest',
                                               bootstrap_servers='localhost:9092', api_version=10)
    try:
        consumer.subscribe("test")
        while running:
            msg = consumer.poll(timeout_ms=10000000)
            if msg is None: continue

            for val in iter(msg.values()):
                print(val[0].value)
    finally:
        consumer.close()
        message_broker.publish_message(producer_instance=publisher,
                                       topic_name="classes",
                                       key='parsed',
                                       value=tosend,
                                       type='kafka')


if __name__ == '__main__':
    #specify arks for kafka or google
    # Listening to images to classify
    type = sys.argv[1]
    message_broker = MessageBroker(type=type)

    if type == "google":
        future = message_broker.generate_consumer(type='google',
                                                  topic_name='images',
                                                  gcallback=classify_google)
        try:
            future.result()
        except KeyboardInterrupt:
            future.cancel()

    if type == "kafka":
        running = True
        consumer = message_broker.generate_consumer(
            type='kafka',
            topic_name='images',
            auto_offset_reset='earliest',
            bootstrap_servers='localhost:9092',
            api_version=10)
        try: