2. Decode images
In this part of the tutorial you decode the base64 encoded images coming from the webcam.
Create the base64 decoder service
Follow these steps to deploy the base64 decoder service:
Navigate to the
Code Samplesand locate the Python
You can use the filters on the left hand side to select
Starter transformationin the resulting filtered items.
base64 decoder serviceas the name for the project.
Select or enter
image-base64for the input.
Select or enter
image-rawfor the output.
Save as project.
Update the code
The code is now saved to your workspace and you can edit it to perform any actions you need it to.
Using the following steps, update the default code so it decodes the web cam images being received on the
image-base64 topic. Then publish the decoded images to the
import base64to the imports at the top of
on_dataframe_received_handlermethod by adding the following line to base64 decode the images.
This should go immediately before this line:
main.py should look like this
import quixstreams as qx import os import pandas as pd import base64 # Added import (1) client = qx.QuixStreamingClient() topic_consumer = client.get_topic_consumer(os.environ["input"], consumer_group = "empty-transformation") topic_producer = client.get_topic_producer(os.environ["output"]) def on_dataframe_received_handler(stream_consumer: qx.StreamConsumer, df: pd.DataFrame): # Transform data frame here in this method. You can filter data or add new features. # Pass modified data frame to output stream using stream producer. # Set the output stream id to the same as the input stream or change it, # if you grouped or merged data with different key. stream_producer = topic_producer.get_or_create_stream(stream_id = stream_consumer.stream_id) df['image'] = df["image"].apply(lambda x: base64.b64decode(x)) # Added code (2) stream_producer.timeseries.buffer.publish(df) # Handle event data from samples items that emit event data def on_event_data_received_handler(stream_consumer: qx.StreamConsumer, data: qx.EventData): print(data) # handle your event data here def on_stream_received_handler(stream_consumer: qx.StreamConsumer): # subscribe to new DataFrames being received # if you aren't familiar with DataFrames there are other callbacks available # refer to the docs here: https://docs.quix.io/client-library/subscribe.html stream_consumer.events.on_data_received = on_event_data_received_handler # register the event data callback stream_consumer.timeseries.on_dataframe_received = on_dataframe_received_handler # subscribe to new streams being received topic_consumer.on_stream_received = on_stream_received_handler print("Listening to streams. Press CTRL-C to exit.") # Handle termination signals and provide a graceful exit qx.App.run()
- Import base64 which will be used to decode the images
base64.b64decodeand store the resulting data in the dataframe
Now it's time to deploy this microservice.
Follow these steps:
Tag the code by clicking
add tagat the top of the code panel. Enter
v1.0for your tag.
Deploynear the top right hand corner of the screen.
v1.0from the verison tag drop down.
You will be redirected to the homepage and the code will be built and deployed and your microservice will be started.