Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
pranav_kandpal
Participant
In this blog , I will cover the topic of transferring images over IoT services of Neo environment  and storing them in SAP Cloud Platform and look at the holistic picture around the mystical topic of Images .

Brief Background : 

While working on a recent Demo for my organization i came across the topic of transferring images as Blobs and using them for further processing . Our specific use case was tweaking a Machine learning algorithm which could process the image and return the response as a labelled image which we then transferred to the SCP Cloud for displaying . Of course there were other multiple alternatives but our aim was to leverage the features offered by SAP Cloud platform .

Sending images via IoT is not really something which i could find in the blogs so i decided to write something on this topic so maybe this feature could be leveraged as per the business use cases by others .

Simplifying Images 

Images are nothing but a rectangular grid of Pixels . Each Pixel consists of 1 byte for Black and White images and  3 bytes for a colored Image (Typically known as the RGB ). An image's resolution is the total number of pixels, e.g., 1600x2000 = 3.2 Megapixels, which corresponds to 3.2 Megabytes inside your computer for 8-bit B&W images or 9.6 Megabytes for 24-bit (3 bytes/pixel) color images.

In short they are a combination of these pixels which are stored as an array of byte in the File systems . When you try to read this array of pixel , of course it doesn't seem pleasing to our eyes and to be honest it doesn't make sense in the first go .

Well the next question comes , how this wonderful array is generated ? The format of the array depends on the underlying format of the image (PNG,JPEG etc.) . I can go on and on about this and this would be a more theory driven blog . So let me not digress from the technical aspects and bring the discussion back to the technical implementation .

I will now try to focus on breaking down images using Python , of course a similar logic can be applied to achieve the desired result in a different programming language .

Technical steps 

The main steps would involve executing a script which is capable of decoding the image into a relevant format accepted by SCP . Lets cover this first !

Blob stands for Binary Large Objects which generally supports formats with higher size and binary format . SAP Hana or SCP in this instance has data format which supports blob data types . I will touch the part of IoT transfer later where we cover the data type to consider for the MMS services .

Now lets begin with a small piece of Python code which will convert the Image to a format which can be sent to the SCP via a Post call .

with open("Path of your Image file with the name of the file ","rb") as imgFile:
f_img = imgFile.read()
k_img = binascii.hexlify(f_img)
data_img = bytes.decode(k_img)

The above mentioned 4 steps would read the image and convert them in a Hexadecimal  format and later decode this Hex format into a string .

The IoT services supports transfer as an array of string  and hence we need to decode the hexadecimal string to a Unicode ASCII Standard string (most preferably)  , therefore the last statement in the above code is used .

The next task would be to push this image to the SCP . Now let me deviate from the python script and focus on the IoT Services . We would need a Message Type in the IoT Services which will accept the Blob Format . We can create a Message type with data type as binary as mentioned below in the screenshot



This message type would accept a data type of Binary array .

Once the message type is created , we can easily link it to a device type and device to allow the push of data using the IoT Services with the help of  a simple code which will  call the Post method as below (in python using requests library) :

 

conn = http.client.HTTPSConnection("Your ID of the API which supports the HTTPS call")
payload = "{\r\n \"mode\": \"sync\",\r\n \"messageType\": \"Message type ID from SCP  ",\r\n \"messages\": [\r\n {\r\n\"B_Array\":\""+data_img+"\"\r\n }\r\n ]\r\n} "
headers = {
'content-type': "application/json",
'authorization': "Bearer Your OAUTH Code",
'charset': "utf-8",
'cache-control': "no-cache",
'postman-token': "Any token id generated , can be avoided as well 😉 "
}
conn.request("POST", "/com.sap.iotservices.mms/v1/api/http/data/ID of the Device , payload, headers)
res = conn.getresponse()
data_response = res.read()
print(data_response.decode("utf-8"))

 

The libraries which are required for the script would be as below :

import requests
from io import BytesIO
import base64
import binascii

This information is sufficient for the coding aspect of the script . The next action would be executing this script with correct information . After the successful execution of the script you will receive a  response of success for post request (201) and you can go into the cockpit to check the data which was pushed .

I will provide an example and try to push an image .  I will use the below mentioned image for reference .



 

The next step would be executing the script and checking the binary array in SCP . After the script is executed successfully a below mentioned response will be printed in the Command prompt ( of course you can play along with the response as you want ) .

 



Now lets go into the cockpit to see the response saved in the SCP .

 

As viewed in SCP (IoT MMS Cockpit)

 



Ofcourse the byte array is huge ( equal to the size of the image) , so i have trimmed the image to only show how it looks roughly .

Known Issue :

There might be some images which are not excepted by the IoT services and mention an error stating the expected array is of type Base64 . I have even tried to convert the BArray to Base64 and even then it doesn't really work . The reason is still unknown and i am still in the process of figuring out a solution for this but this is a rare case . In our use-case we clicked images and pushed them to the SCP instance which worked with a success ration of almost 90% with the script . Maybe if someone going through this blog has a solution or suggestion to this issue , it would be more than welcome !



 

Even though this piece of code looks easy , i had to burn alot of my energy to get this through and working because to be honest breaking down an image into a format which is accepted by the IoT Services was a pain .  This is my first blog after a break of almost 3 years and i hope this series helps in simplifying few things as we all evolve together into the world of Cloud Foundry in SCP 😉 .

 

In the next blog i will cover how to retrieve this request and show it in a simple app made in UI5  !
2 Comments
Labels in this area