Dice Detection using OpenCV

I’m working on a small project that will eventually involve object detection and sorting using a robot arm. In terms of object I considered various items from small plastic bottles to figurines and eventually settled on dice given their size, weight and suitability for what is basically a $20 plastic robot arm. Easy, I figured, grab some training images and train a tiny-yolo model for detection and we should be good to go, right? Not really.

This is what we are trying to detect, below. We want to know the color of the dice, as well as the number of pips it sees, from 1 to 6. We want to detect this off video, ideally, too, and fast, in real-time.

Dice

Training a yolo model, well any CNN basically requires a fair number of training images. In my previous post I trained a yolov3 model to detect rats and that took 600 images, carefully labelled and trained and I’ll be the first to admit that labeling hundreds of images is not my idea of a good time. It worked well, and I even managed to retrain it on tiny-yolo to fit on a Raspberry Pi3 and was happy with the result. The FPS rate wasn’t great but it worked well enough. So I figured I’d give it a go first with 40 odd images of a white dice, divided into 6 classes to denote the number of pips. Several hours later I had a model which detected pretty much nothing. Zero. Maybe I messed up the parameters or whatnot, but it made me consider alternatives.

Searching around I found a number of promising examples using OpenCV which I tried, with mixed results. OpenCV is fast, doesn’t require 60MB plus trained models and you can nicely break the problem down into different parts. So I started from scratch and assembled a fairly good model to detect not only different color dice but the pip count on each as well. Keep in mind I have specific requirements including dice location, rotation, color and distance from dice to camera being within a specific, fixed range. So don’t expect this to work for your casino tables right off the bat.

Key to making this work, and also the most painful part, is choosing an HSV color mask to extract the dice from the background. Now I assume that most of the time, and very much for what I need to do, you will have an idea what the background is going to be, say flat black, a green gaming board or whatever. You also have an idea of the distance between camera and dice too.

So the first step is to figure out the HSV color mask (lower and upper bounds of each) as shown below here on a white dice, within your own parameter constraints. It turns out green and red is easy, white is quite a pain to get right.

HSVConfig

You will notice in the screenshot above that I tuned the parameters using the trackbars to isolate the dice as much as possible. This won’t be 100%, there will be residual noise, but you want to be able to detect the pips, as circles, which you count using OpenCV’s HoughCircles method. We know what color we have if we detect pips within one of our 3 defined HSV color masks, basically. For bonus points you can detect the dice using contours too if you wish.

Having done the above for all three dice we get some very good results as shown below:

Red Dice:

Red3

Green Dice:

Green6

White Dice:

White3

The full Python code is included below. You’ll need to tune the HSV masks and the parameters for the HoughCircles method (minRadius, maxRadius) depending on your own requirements.

from imutils.video import VideoStream
import numpy as np
import cv2 as cv2
import imutils

# dice color in HSV
# measure these while on a typical expected background
greenLower = (43, 83, 103)
greenUpper = (99, 115, 182)
redLower = (137,26,149)
redUpper = (202,59,208)
whiteLower = (0,0,0) 
whiteUpper = (191, 160, 150) 

font = cv2.FONT_HERSHEY_SIMPLEX
topLeftCornerOfText = (10,30)
fontScale = 1
fontColor = (0,0,0)
lineType = 2

vs = VideoStream(src=1).start() #1=external USB cam

while True:
	frame = vs.read()
	if frame is None:
		continue

	frame = imutils.resize(frame, width=600)
	blurred = cv2.GaussianBlur(frame, (11, 11), 0)
	hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
	
	#try red?
	mask = cv2.inRange(hsv, redLower, redUpper)
	mask = cv2.bitwise_not(mask) # invert
	circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 20, param1=30, param2=15, minRadius=6, maxRadius=30)

	if circles is not None:
		circles = np.round(circles[0, :]).astype("int")
		if ((len(circles) > 0) and (len(circles) <=6)): # no point guessing
			cv2.putText(mask,"RED: " + str(len(circles)), topLeftCornerOfText, font, fontScale,fontColor,lineType)
	else:
		# try green?
		mask = cv2.inRange(hsv, greenLower, greenUpper)
		mask = cv2.bitwise_not(mask) # invert
		circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 20, param1=30, param2=15, minRadius=6, maxRadius=30)
		if circles is not None:
			output = mask.copy()
			circles = np.round(circles[0, :]).astype("int")
			if ((len(circles) > 0) and (len(circles) <=6)):
				cv2.putText(mask,"GREEN: " + str(len(circles)), topLeftCornerOfText, font, fontScale, fontColor, lineType)
		else:
			# try white
			mask = cv2.inRange(hsv, whiteLower, whiteUpper)
			mask = cv2.bitwise_not(mask) # for white, depending on background color, remark this out
			circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 20, param1=30, param2=15, minRadius=6, maxRadius=30)
			if circles is not None:
				output = mask.copy()
				circles = np.round(circles[0, :]).astype("int")
				if ((len(circles) > 0) and (len(circles) <=6)):
					cv2.putText(mask,"WHITE: " + str(len(circles)), topLeftCornerOfText, font, fontScale,fontColor,lineType)

	cv2.imshow("Preview", mask)
	key = cv2.waitKey(1) & 0xFF
	if key == ord("q"):
		break

vs.release()
cv2.destroyAllWindows()

 

Advertisements

From AI Model to Production in Azure

Problem Description (courtesy of DataDriven.com):

When a patient has a CT scan taken, a special device uses X-rays to take measurements from a variety of angles which are then computationally reconstructed into a 3D matrix of intensity values. Each layer of the matrix shows one very thin “slice” of the patient’s body.

This data is saved in an industry-standard format known as DICOM, which saves the image matrix in a set binary format and then wraps this data with a huge variety of metadata tags.

Some of these fields (e.g. hardware manufacturer, device serial number, voltage) are usually correct because they are automatically read from hardware and software settings.

The problem is that many important fields must be added manually by the technician and are therefore subject to human error factors like confusion, fatigue, loss of situational awareness, and simple typos.

A doctor scrutinising image data will usually be able to detect incorrect metadata, but in an era when more and more diagnoses are being carried out by computers it is becoming increasingly important that patient record data is as accurate as possible.

This is where Artificial Intelligence comes in. We want to improve the error checking for one single but incredibly important value: a field known as Image Orientation (Patient) which indicates the 3D orientation of the patient’s body in the image.

For this challenge we’re given 20,000 CT scan images, sized 64×64 pixels and labelled correctly for training. The basic premise is given an image, the AI model needs to predict the correct orientation as explained graphically below. The red arrow shows the location of the spine, which our AI model needs to find to figure out the image orientation.

Capstone

We’ll use Tensorflow and Keras to build and train an AI model in Python and validate against another 20,000 unlabelled images. The pipeline I used had three parts to it, but the core is shown in Python below and achieved 99.98% accuracy on the validation set. The second and third parts (not shown) pushed this to 100%, landing me a #6 ranking on the leader board. A preview of the 20,000 sample training images is shown below.

Sample

Our model in Python:

(x_train, x_test, y_train, y_test) = train_test_split(data, labels, test_size=0.15, random_state=42)

# construct our model
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=InputShape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(Classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])

checkpoint = ModelCheckpoint("model.h5", monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]

# start training
model.fit(x_train, y_train, batch_size=BatchSize, epochs=Epochs, verbose=1, validation_data=(x_test, y_test), callbacks=callbacks_list)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# save the model and multi-label binarizer to disk
model.save('capstone.model')
f = open('capstone.pickle', "wb")
f.write(pickle.dumps(mlb))
f.close()

 

I split the sample images into four folders according to their labels and I used ZERO, ONE, TWO and THREE as the class labels. So, given a test image the model will do a prediction and return one of those class labels to assign.

First things first, we’ll construct our model and start the training. On my dual-K80 GPU server this took about an hour. The model is saved at various stages, and once we are happy with the accuracy we’ll save the resulting model and pickle file (capstone.model & capstone.pickle in the code)

To deploy this as an API in Azure we’ll create a new web app with default Azure settings. Once deployed, we’ll add the Python 3.6 extension. Switch to the console mode and use pip to install any additional libraries we need, including Flask, OpenCV, Tensorflow and Keras. Modify the web.config to look like the one shown below. Note that our Python server script will be named run_keras_server.py

<configuration>
  <appSettings>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_HANDLER" value="run_keras_server.app"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
  </appSettings>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python364x64\python.exe|D:\home\Python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

 

Our Python run_keras_server.py script:

import numpy as np
from keras.preprocessing.image import img_to_array
from keras.applications import imagenet_utils
from keras.models import load_model
import cv2
import flask
import io
import pickle

app = flask.Flask(__name__)

model = load_model("capstone.model")
mlb = pickle.loads(open('capstone.pickle', "rb").read())

def _grab_image(stream=None):
	if stream is not None:
		data = stream.read()
		image = np.asarray(bytearray(data), dtype="uint8")
		image = cv2.imdecode(image, cv2.IMREAD_COLOR)
	return image
	
@app.route("/predict", methods=["POST"])
def predict():
    
    data = {"success": False, "label":"None"}

    if flask.request.method == "POST":
        if flask.request.files.get('image'):
            image = _grab_image(stream=flask.request.files["image"])
            image = image.astype("float") / 255.0
            image = img_to_array(image)
            image = np.expand_dims(image, axis=0)
            proba = model.predict(image)[0]
            idxs = np.argsort(proba)[::-1][:2]
            label = mlb.classes_[idxs[0]]
            
            if label == "ZERO":
                label = "Spine at bottom, patient facing up."
            if label == "ONE":
                label = "Spine at right, patient facing left."
            if label == "TWO":
                label = "Spine at top, patient facing down."
            if label == "THREE":
                label = "Spine at left, patient facing right."
            
            data["label"] = label
            data["success"] = True

    return flask.jsonify(data)

if __name__ == "__main__":
    app.run()

 

Using your FTP tool of choice, upload the run_keras_server.py script, along with capstone.model and capstone.pickle, into the D:\home\site\wwwroot folder. Restart the web app from within Azure.

We can test our API using Postman, or the C# script shown below, which takes a sample image and performs a prediction.

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace CallPythonAPI
{
    class Program
    {
        private static readonly HttpClient client = new HttpClient();

        static void Main(string[] args)
        {
            string responsePayload = Upload().GetAwaiter().GetResult();
            Console.WriteLine(responsePayload);
        }

        private static async Task<string> Upload()
        {
            var request = new HttpRequestMessage(HttpMethod.Post, "http://mywebappdemo.azurewebsites.net/predict");
            var content = new MultipartFormDataContent();
            byte[] byteArray = System.IO.File.ReadAllBytes("20.png");
            content.Add(new ByteArrayContent(byteArray), "image", "20.png");
            request.Content = content;
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
    }
}

 

Our sample image looks like this:

20

Running the prediction on this image yields the following result:

Prediction

That’s it. We can incorporate the API call into a web site, desktop client app or even a Raspberry PI device, since all the heavy lifting is done on the server-side.

Forensic Analysis with Python & Benford’s Law

Early in my career I specialised in Computer Security and more specifically Data Security. On one particular engagement I was confronted with a system that had virtually no audit log capability and very limited access control (mainframe based), and the suspicion was that staff was being paid off to alter transactional data.

The tools I had at my disposal was Microsoft Access, a basic CSV transaction log and a copy of Borland Delphi and I focussed on analysing and detecting changes in processing volume of data operators as an indication of suspicious activity, with some good success. Looking back, I wish I knew about Benford’s Law, as that would have certainly made my life much easier. Now 20 years later I work extensively in global payroll within the Microsoft Dynamics 365 ERP market, and while the threat of fraud remains, the tools and processing capability have advanced and improved dramatically.

From Wikipedia: “Benford’s law, also called Newcomb-Benford’s law, law of anomalous numbers, and first-digit law, is an observation about the frequency distribution of leading digits in many real-life sets of numerical data. The law states that in many naturally occurring collections of numbers, the leading significant digit is likely to be small. For example, in sets that obey the law, the number 1 appears as the most significant digit about 30% of the time, while 9 appears as the most significant digit less than 5% of the time. If the digits were distributed uniformly, they would each occur about 11.1% of the time. Benford’s law also makes predictions about the distribution of second digits, third digits, digit combinations, and so on.”

Payroll data as with any ERP financial data can consist of thousands or hundreds of thousands of transactions per pay run. Consider a typical worker with 10 to 15 different payments (or allowances) across a workforce of 5000 workers. This often generates 75,000 or more transactions per pay run and auditing of this volume, which can then be run weekly, fortnightly or monthly (thus 75,000 x 4 per month) presents a significant workload problem. Spot-checking becomes unfeasible unless you could reduce your focus to transactions that may require further scrutiny.

Consider a policy requiring approval of expenses that exceed $300. As long as you submit expenses totalling no more than $290 odd you might be able to sneak this through every so often, and while this is no heist, this amount can still add up over time. Anti-Money Laundering systems often utilize hundreds of rules, one typically detects money transfers exceeding a cut-off of $10,000 before raising a flag requiring bank approval. If you travel internationally often enough, you’ll see that $10,000 amount on arrival and departure cards all the time.

Let’s take a few thousand rows of allowance data, which includes salary and miscellaneous allowances and sanitize it by removing identifying columns, leaving only the amount column.

Our test data is shown below.

DataNotFake

I’ll be using a Python library available here that implements Benford’s Law by testing our null hypothesis and displaying a graph showing the digit distribution. A screenshot of the script is shown below, running in Visual Studio Code on Ubuntu Linux.

CodeView

I’ve modified the script and ran it against our clean, non-modified data and the resulting digit distribution is shown below.

NotFake

We can see a fairly good expected distribution curve with slight elevation of digit ‘6’ and ‘5’ being a bit low, but still within a fairly normal distribution. You need to understand the data fairly well to explain any deviations such as this. Here it could be that all employees receive a single allowance fixed at $60, producing the elevation. We are experimenting here after all, don’t assume you can load a bunch of numbers from a spreadsheet and this script will become your magic fraud detection silver bullet.

Let’s manually modify our test data by replacing some allowances with random numbers. An extract is shown below and notice the numerous 4xx digit amounts now occurring (my manually modified amount).

DataFaked

Running our script again produces the plot below, clearly indicating an elevation of digit ‘4’ occurring when the natural expectation of occurrence was much less. Other figures are also off as a consequence, especially ‘7’.

Fakes

With this in hand, we can now isolate these occurrences in our data and perform a deeper inspection and validation of these transactions, the associated workers and approver of the workflow, if that was required. Spot-checking, but across a more narrow area of focus.

For further reading I recommend the work done by Mark Nigrini on the subject.

Near-perfect YOLO3 Object Detection from scratch

I recently completed the Microsoft Professional Program in Artificial Intelligence and have been really impressed by some of the many computer vision examples I’ve seen. It’s a great course and if you are interested in AI I highly recommend it, along with the fantastic blog and training offered by Adrian Rosebrock at pyimagesearch.com.

There are a number of key technologies and platforms that will continuously come up in AI as you learn – Tensorflow, CNTK, OpenCV and of course Keras. Once you start exploring computer vision and specifically Convoluted Neural Networks you are bound to run into numerous examples of real-time object detection from video, whether it’s a car, person, dog or street-sign, and most of these examples will use a pre-built model, laboriously created to detect dozens or even thousands of classes of objects out of the box, and ready for you to use in your own models with little to no effort required.

That’s all great, but what if you wanted to detect something that is not included in the pre-built model? The solution lies in building and training your own from scratch, which is what I did for this post.

I’ve found YOLO3 to be really fantastic, and since I’m a Windows user my focus was on being able to build and train a model without having to struggle with code or tutorials designed for Linux. I found a pretty good set of scripts on GitHub and started off by getting it all running locally and training their example detector which detects raccoons.

Sometimes I use a laptop with Intel HD5000 GPU and PlaidML sitting between Keras and Tensorflow. This works well in most cases but for training a YOLO3 model you’ll need a better setup, and I used an Azure Windows 2016 Server VM I deployed and loaded it with Python 3.6, Tensorflow and Keras.

The VM comes with 112GB of RAM and dual Nvidia K80 GPU’s. It’s not cheap to operate so I do all my prep work locally, making sure the model starts training without obvious errors and then copy that all over to the VM for the training run.

For this post I decided that while raccoons are cool, rats would be more interesting. Rats are fast, come in all shapes, sizes and colours, and can unfortunately cause problems when not kept as pets. They nest, chew through electrical wiring, and cause havoc in agriculture and food manufacturing. They are also used for neuroscience research with the classic example being a rat running a maze.

Because of the speed they move and ways they can contort their bodies it should, in theory, be pretty hard to detect and classify using a CNN. Let’s give it a try.

I started off by collecting 200 assorted images of rats and mice using both Google and Bing, then did the annotation using LabelImg as shown below.

LabelImg

This presents the first major decision we need to make. Do we include the tail in the annotation or not? So, we need to take a step back and think carefully what it is we are trying to achieve.

  • We want to detect rats (and mice), and detecting their bodies or heads is good enough
  • Sometimes all you see is a tail, no body, and yet it’s still a rat!
  • Including the tail also introduces the visual environment around the tail, which could throw our training

Consider for a moment if our task was to build a model that detects both rats and earthworms. Suddenly a rat tail can (and likely will) be detected as an earthworm, or the other way around since they are both similar in shape and colour. I don’t really have an answer here, and I’ve opted to ignore tails completely, except for maybe a stump or an inch of the tail, no more. Let’s see how that works out. We don’t have a lot of training images so our options are limited.

I modified the config.json file as shown below to include our single class (rodent) and generated the anchors as recommended and changed that in the config file. I am not using the YOLO3 pre-trained weights file as I want to train from scratch completely. (Tip: I did a run with pre-trained weights as a test and the results were disappointing)

{
    "model" : {
        "min_input_size":       128,
        "max_input_size":       872,
        "anchors":              [76,100, 94,201, 139,285, 188,127, 222,339, 234,225, 317,186, 323,281, 331,382],
        "labels":               ["rodent"]
    },

    "train": {
        "train_image_folder":   "C:/Users/xalvm/Documents/Projects/keras-yolo3/data/rodent_dataset/images/",
        "train_annot_folder":   "C:/Users/xalvm/Documents/Projects/keras-yolo3/data/rodent_dataset/anns/",      
        "cache_name":           "rodent_train.pkl",
        "train_times":          10,             
        "pretrained_weights":   "",             
        "batch_size":           4,             
        "learning_rate":        1e-4,           
        "nb_epochs":             30,             
        "warmup_epochs":        3,              
        "ignore_thresh":        0.5,
        "gpus":                 "0,1",
        "grid_scales":          [1,1,1],
        "obj_scale":            5,
        "noobj_scale":          1,
        "xywh_scale":           1,
        "class_scale":          1,
        "tensorboard_dir":      "logs",
        "saved_weights_name":   "rodent.h5",
        "debug":                false            
    },

    "valid": {
        "valid_image_folder":   "",
        "valid_annot_folder":   "",
        "cache_name":           "",
        "valid_times":          1
    }
}

 

A typical training run in-progress is shown below, and I stopped the training at around 27 epochs since there was no loss reduction after epoch 24.

Training

Using a sample video off YouTube I ran predict.py and viewed the results frame by frame, noticing some good results and a fair amount of missed predictions. The best way to improve prediction is with more training data, so back we go to Google and Bing for more images, and we also grab some frames from random rat videos for more annotation.

My resulting set now contains 560 annotated training images which the script will split into a train/test set for me. With more training images comes longer training runs, and the next run took 20 hours before I stopped it at Epoch 30. This time the results were a lot more impressive.

There were still some failures, so let’s look at those first.

0127

Here are three consecutive frames where the first we have a hit, the second nearly identical frame was missed, while the third again got a hit. This is quite bizarre, as our predictor does a frame by frame prediction. It’s not seeing the video clip as a whole, it literally detects frame by frame and yet in the middle frame we failed.

0601

Again we see three frames where the first was missed, and we would assume the low quality of the frame is to blame. However, notice the following sequence:

0273

Here we barely have the silhouette of a head appearing and yet we get a 98% probability on what is a small, very fuzzy image.

1730

The final sequence above is quite impressive though, a good hit on what is no more than a ball of white fur. If you watch the full clip you will see a few more misses that should have been obvious, and then some pretty incredible hits.

All in all really impressive results, and we only had 560 training images.

Watch the clip here: (I removed 10 seconds from the clip to protect privacy)

YOLO3 Results

Easy Dynamics 365/AX Blockchain Integration

This post continues to explore Blockchain integration into Microsoft Dynamics 365 for Finance and Operations (AX). I’ve seen examples where developers did integration using the MetaMask Chrome extension, however I want something that looks and feels like pure AX.

For this post I will be using Xalentis Fusion which provides seamless Blockchain integration, whereby Blockchain I refer to Ethereum. I don’t see much use for Bitcoin, and apart from hundreds of other altcoins available, I see more enterprise-level movement towards Ethereum or its variants, including JP Morgans’ Quorum or Microsoft “Project Bletchley”.

Xalentis Fusion works by detecting new transactions in an Ethereum Blockchain and allows filtering to take place across those. Once a filter detects your specific requirements, it can fire off any number of associated rules, written in simple script. Fusion also includes a growing API allowing REST-based integration with the outside world, which is what we will be using.

Fusion provides a Transact API method allowing transactions to be made via a REST call. We can do this ourselves easily as well, but since we’ll be using Fusion for more than transacting (later on in this post) I figured I’ll just stick with it.

We’ll keep it very basic, and create a simple form accepting a number of parameters that we will use to perform a transaction. Our form design is shown below.

BeforeTransact

We’ve added fields for Node, Sender, Recipient, Sender’s account Password, and the amount of Wei to send. Depending on the Ethereum network you are connecting to, adjust the Node value accordingly, or simply hardcode it and remove the field. We are using the Fusion Test Net so that is shown. I’ve created two addresses in the Test Net, also shown in the form design, and loaded the first with some Ether. Perhaps customers want to trade in Dollars, so you can add code to convert Dollars entered into Wei or whatever token is in use. We’ll stick with Wei for now.

Let’s submit this transaction.

AfterTransact

As you can see, the transaction has been submitted to the Blockchain and an Infolog displayed showing success. The X++ code is shown below, including the form class and a utility class that performs our REST calls to Fusion. I’ve added a TransactionRequest class as the POST action we are performing requires JSON being passed, and wrapped the class members with the DataContract attributes to enable easy serialization. This particular POST call accepts the full JSON as part of the POST URL, wrapped as Base64, and that is done in the utility class. The body is required, so we set the content-length to 0.

[DataContractAttribute]
class TransactRequestClass
{
    str addressFrom;
    str addressTo;
    str node;
    str password;
    str wei;

    [DataMemberAttribute]
    public str AddressFrom(str _addressFrom = addressFrom)
    {
        addressFrom = _addressFrom;
        return addressFrom;
    }

    [DataMemberAttribute]
    public str AddressTo(str _addressTo = addressTo)
    {
        addressTo = _addressTo;
        return addressTo;
    }

    [DataMemberAttribute]
    public str Node(str _node = node)
    {
        node = _node;
        return node;
    }

    [DataMemberAttribute]
    public str Password(str _password = password)
    {
        password = _password;
        return password;
    }

    [DataMemberAttribute]
    public str Wei(str _wei = wei)
    {
        wei = _wei;
        return wei;
    }
}

class FusionUtilityClass
{
    public static str Transact(str addressFrom, str addressTo, str password, str node, str wei)
    {
        System.Net.WebClient webClient;
        System.Text.UTF8Encoding encoder;
        System.Text.UnicodeEncoding decoder;
        System.IO.Stream s;
        System.IO.StreamReader sr;
        System.Net.HttpWebRequest myRequest;
       
        try
        {
            TransactRequestClass request = new TransactRequestClass();
            request.AddressFrom(addressFrom);
            request.AddressTo(addressTo);
            request.Node(node);
            request.Password(password);
            request.Wei(wei);

            encoder = new System.Text.UTF8Encoding();
            str json = FormJsonSerializer::serializeClass(request);
            System.Byte[] encodedBytes = encoder.GetBytes(json);
            str encoded64 = System.Convert::ToBase64String(encodedBytes);
 
            str url = "http://fusionapi.azurewebsites.net/api/transact?bodyJson=" + encoded64;
            myRequest = System.Net.WebRequest::Create(url);
            myRequest.Method = "POST";
            myRequest.Timeout = 30000;
            myRequest.ContentLength = 0;

            System.Net.WebHeaderCollection headers = myRequest.Headers;
            headers.Add("API_KEY", "your fusion api key");

            s = myRequest.GetResponse().GetResponseStream();
            sr = new System.IO.StreamReader(s);
            str txnHash = sr.ReadToEnd();
            s.Close();
            sr.Close();
            return txnHash;
        }
        catch (Exception::Error)
        {
        }
        return "";
    }
}

class XalentisTestFormClass
{
    
    [FormControlEventHandler(formControlStr(XalentisTestForm, FormButtonControl1), FormControlEventType::Clicked)]
    public static void FormButtonControl1_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormStringControl nodeControl = sender.formRun().control(sender.formRun().controlId("FormStringControl1"));
        FormStringControl addressSenderControl = sender.formRun().control(sender.formRun().controlId("FormStringControl2"));
        FormStringControl addressRecipientControl = sender.formRun().control(sender.formRun().controlId("FormStringControl3"));
        FormStringControl passwordControl = sender.formRun().control(sender.formRun().controlId("FormStringControl4"));
        FormStringControl weiControl = sender.formRun().control(sender.formRun().controlId("FormStringControl5"));

        str addressFrom = addressSenderControl.Text();
        str node = nodeControl.Text();
        str addressTo = addressRecipientControl.Text();
        str password = passwordControl.Text();
        str wei = weiControl.Text();

        str txnHash = FusionUtilityClass::Transact(addressFrom, addressTo, Password, node, wei);
        //todo: store txnHash for history purposes.

        info("Transaction Posted");
    }
}

That works pretty well, but users don’t understand Blockchain addresses, and it would be painful to maintain that somewhere in notepad or Excel to copy and paste each time a transaction is made. Luckily Fusion provides an Account Mapping facility, which is a customer-specific key/value table mapping Blockchain addresses to friendly names, or account numbers the rest of us can readily understand.

So instead of entering address for Sender and Recipient, let’s modify our form as shown below. We can use drop-downs to pull up a list of known accounts, or use an API call to Fusion to return a full list of mapped accounts which we can then allow users to select. I’ll keep it simple with a text field. Here we’ve entered two known friendly account names we can read and verify. These could come from your chart of accounts as well, whatever works best in your scenario. As long as the display text maps to an address in Fusion, it can be resolved.

BeforeTransact2

I’ve modified our form class and utility class to add two additional API calls to Fusion to resolve the friendly names to Ethereum addresses as shown in the code below. We simply make a GET call to Fusion passing across the friendly name, and Fusion will perform the lookup, returning the proper Ethereum address we need to use when performing the transaction. The updated code is shown below.

class XalentisTestFormClass
{
    
    [FormControlEventHandler(formControlStr(XalentisTestForm, FormButtonControl1), FormControlEventType::Clicked)]
    public static void FormButtonControl1_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormStringControl addressSenderControl = sender.formRun().control(sender.formRun().controlId("FormStringControl2"));
        FormStringControl addressRecipientControl = sender.formRun().control(sender.formRun().controlId("FormStringControl3"));
        FormStringControl passwordControl = sender.formRun().control(sender.formRun().controlId("FormStringControl4"));
        FormStringControl weiControl = sender.formRun().control(sender.formRun().controlId("FormStringControl5"));

        str addressFrom = addressSenderControl.Text();
        str addressTo = addressRecipientControl.Text();
        str password = passwordControl.Text();
        str wei = weiControl.Text();

        str txnHash = FusionUtilityClass::Transact(addressFrom, addressTo, Password, wei);
        //todo: store txnHash for history purposes.

        info("Transaction Posted");
    }
}

class FusionUtilityClass
{
    public static str Transact(str accountFrom, str accountTo, str password, str wei)
    {
        System.Net.WebClient webClient;
        System.Text.UTF8Encoding encoder;
        System.Text.UnicodeEncoding decoder;
        System.IO.Stream s;
        System.IO.StreamReader sr;
        System.Net.HttpWebRequest myRequest;
       
        try
        {
            str addressFrom;
            str addressTo;

            str url = "http://fusionapi.azurewebsites.net/api/address/" + strReplace(accountFrom, " ","%20");
            myRequest = System.Net.WebRequest::Create(url);
            myRequest.Method = "GET";
            myRequest.Timeout = 30000;
            System.Net.WebHeaderCollection headers = myRequest.Headers;
            headers.Add("API_KEY", your fusion api key);
            s = myRequest.GetResponse().GetResponseStream();
            sr = new System.IO.StreamReader(s);
            addressFrom = sr.ReadToEnd();
            s.Close();
            sr.Close();

            url = "http://fusionapi.azurewebsites.net/api/address/" + strReplace(accountTo, " ","%20");
            myRequest = System.Net.WebRequest::Create(url);
            myRequest.Method = "GET";
            myRequest.Timeout = 30000;
            headers = myRequest.Headers;
            headers.Add("API_KEY", "your fusion api key");
            s = myRequest.GetResponse().GetResponseStream();
            sr = new System.IO.StreamReader(s);
            addressTo = sr.ReadToEnd();
            s.Close();
            sr.Close();

            TransactRequestClass request = new TransactRequestClass();
            request.AddressFrom(strReplace(addressFrom,"\"",""));
            request.AddressTo(strReplace(addressTo,"\"",""));
            request.Node("http://xaleth4kq.eastus.cloudapp.azure.com:8545"); // hardcoded now
            request.Password(password);
            request.Wei(wei);
            encoder = new System.Text.UTF8Encoding();
            str json = FormJsonSerializer::serializeClass(request);
            System.Byte[] encodedBytes = encoder.GetBytes(json);
            str encoded64 = System.Convert::ToBase64String(encodedBytes);
            url = "http://fusionapi.azurewebsites.net/api/transact?bodyJson=" + encoded64;
            myRequest = System.Net.WebRequest::Create(url);
            myRequest.Method = "POST";
            myRequest.Timeout = 30000;
            myRequest.ContentLength = 0;
            headers = myRequest.Headers;
            headers.Add("API_KEY", "your fusion api key");
            s = myRequest.GetResponse().GetResponseStream();
            sr = new System.IO.StreamReader(s);
            str txnHash = sr.ReadToEnd();
            s.Close();
            sr.Close();
            return txnHash;
        }
        catch (Exception::Error)
        {
        }
        return "";
    }
}

We’ll submit this transaction, and as shown we’ve got success.

AfterTransact2

I hope this was helpful. One final item to note is using Wei, which is a BigInteger. I’ve used strings to remove the need for dealing with BigInteger types in X++.

 

GPS, IoT, Blockchain Integration to ERP

I’ve read a number of articles discussing how Blockchain could have a significant impact on Trade & Logistics, especially item tracking. Granted, Blockchain is not a requirement for shipment tracking, but it does deliver a number of benefits through being a shared, secure ledger that, depending on the network, could provide an automatic openness almost immediately. That is a vast improvement over building a custom customer and partner portal to query legacy backend systems.

Of course, there remains the problem of now having to integrate Blockchain into your legacy ERP system, a whole different level of headache. So, in this post I’m going to do a simple POC to simulate how easy, or hard, it would be to build an item tracking service using Ethereum Blockchain, add to that GPS tracking with temperature and humidity monitoring, and get that to your ERP system, in this case Microsoft Dynamics 365. I want to achieve that without modifying the ERP system in any way, by using Microsoft Flow, a PowerApp and Microsoft Common Data Service. The idea is that end users, customers or partners can use the PowerApp to monitor shipments and climate conditions in real-time. Supply-chain visibility every step of the way, basically.

To start, I built a simple IoT monitoring device around the Adafruit Huzzah. I’ll be using WiFi here, making a wild assumption that WiFi is available wherever this device goes. In the real world, GPRS or Loran might be more suitable, but I don’t have that available in my toolkit just yet and besides, this is an experiment only. I’ve added a low-cost GPS, DHT11 temperature and humidity sensor, and an LCD screen to show me what is happening without requiring connecting to my laptop via the serial interface. The basic IoT device is shown below, with GPS and DHT-11 working and transmitting data.

Circuit

The C code for the IoT device is shown below. I do a POST to my Ethereum network of choice with hardcoded addresses, and embed the GPS coordinates and DHT11 state into the data portion of the Ethereum transaction. Addressing and data is entirely up to you; perhaps instead of hardcoding, this can all be read off an SD card.

#include <DHT.h>
#include "TinyGPS++.h"
#include <SoftwareSerial.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"

#define SSID "WiFiSSID" 
#define PASS "mypassword" 
#define OLED_RESET LED_BUILTIN
#define DHTPIN 12
#define DHTTYPE DHT11

TinyGPSPlus gps;
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(OLED_RESET);
SoftwareSerial mySerial(13, 15);

const char *gpsStream =
  "$GPRMC,045103.000,A,3014.1984,N,09749.2872,W,0.67,161.46,030913,,,A*7C\r\n"
  "$GPGGA,045104.000,3014.1985,N,09749.2873,W,1,09,1.2,211.6,M,-22.5,M,,0000*62\r\n"
  "$GPRMC,045200.000,A,3014.3820,N,09748.9514,W,36.88,65.02,030913,,,A*77\r\n"
  "$GPGGA,045201.000,3014.3864,N,09748.9411,W,1,10,1.2,200.8,M,-22.5,M,,0000*6C\r\n"
  "$GPRMC,045251.000,A,3014.4275,N,09749.0626,W,0.51,217.94,030913,,,A*7D\r\n"
  "$GPGGA,045252.000,3014.4273,N,09749.0628,W,1,09,1.3,206.9,M,-22.5,M,,0000*6F\r\n";

void setup() {
  Serial.begin(9600);
  dht.begin();
  display.setCursor(0,0);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println("Connecting");
  display.println("to");
  display.println("WiFi...");
  display.display();

  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  } 
  display.clearDisplay();
  display.display();
  display.setCursor(0,0);
  display.println("EtherGPS");
  display.println("www.xalentis.com");
  display.display();
  mySerial.begin(38400); // GPS
  delay(5000); // warm up GPS 
  display.clearDisplay();
  display.display();
  display.setCursor(0,0);
  display.println("Scanning...");
  display.display();
}

void loop() 
{
  while (*gpsStream)
    if (gps.encode(*gpsStream++))
      updateInfo();
}

void updateInfo()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (gps.location.isValid())
  {
    display.clearDisplay();
    display.display();
    display.setCursor(0,0);
    display.println("Temp:" + String(t));
    display.println("Hum:" + String(h));
    display.println("Lat:" + String(gps.location.lat()));
    display.println("Lon:" + String(gps.location.lng()));
    display.display();

    String data = "0x";
    String deviceSerial = "3132333435"; // 12345 in HEX
    data = data + deviceSerial + "2c"; // comma
    
    String temp = String(t);
    String hum = String(h);
    String lat = String(gps.location.lat());
    String lon = String(gps.location.lng());
    byte buffer[255]={0};
    
    //temp
    temp.getBytes(buffer, 255, 0);
    for (int i=0;i<=temp.length()-1;i++)
    {
      data = data + String((int)buffer[i], HEX);
    }
    data = data + deviceSerial + "2c"; // comma

    //hum
    hum.getBytes(buffer, 255, 0);
    for (int i=0;i<=hum.length()-1;i++)
    {
      data = data + String((int)buffer[i], HEX);
    }
    data = data + deviceSerial + "2c"; // comma

    //latitude
    lat.getBytes(buffer, 255, 0);
    for (int i=0;i<=lat.length()-1;i++)
    {
      data = data + String((int)buffer[i], HEX);
    }
    data = data + deviceSerial + "2c"; // comma

    //longitude
    lon.getBytes(buffer, 255, 0);
    for (int i=0;i<=lon.length()-1;i++)
    {
      data = data + String((int)buffer[i], HEX);
    }

    // build up our Ethereum transaction
    StaticJsonBuffer<1000> JSONbufferTwo;  
    JsonObject& uploadJSON = JSONbufferTwo.createObject(); 
    uploadJSON["jsonrpc"] = "2.0";
    uploadJSON["method"] = "personal_sendTransaction";      
    JsonArray&  uploadQueryParams = uploadJSON.createNestedArray("params");
    JsonObject& callTxParams = JSONbufferTwo.createObject();
    callTxParams["from"] = "0x27f6f763ae5c52721db57c4423c298a78de1f22a";
    callTxParams["to"] = "0xcaade3aa018d57d808fceb16824c47dfd206484c";
    callTxParams["value"] = "0x6FC23AC00"; //hex value 30 Gwei 
    callTxParams["gas"] = "0x30D40"; //hex value for 200000 -high gas limit for good measure          
    callTxParams["gasPrice"] = "0x6FC23AC00"; //hex value 30 Gwei gasprice 21gwei is typical
    callTxParams["data"] = data; // device,tem,hum,lat,long
    uploadQueryParams.add(callTxParams);
    uploadQueryParams.add("myetherpassword");
    uploadJSON["id"] = 1;
    String uploadString;
    uploadJSON.printTo(uploadString);
    callGeth(uploadString); // send for mining
  }
}

String callGeth(String inputJSON) 
{
  HTTPClient http;
  http.begin("http://13.72.73.21:8545/");
  http.addHeader("Content-Type", "application/json");
  int httpCode = http.POST(inputJSON);
  String JSONResult = http.getString(); // contains Txn
  http.end();
  return JSONResult;
}

At this point the transactions are flowing to the Blockchain network and that is great, but we need to be able to monitor the Blockchain for transactions we are interested in, so we can pull that off the Blockchain and into an ERP system, right?

The easiest way to do that is to use Xalentis Fusion. Sign up for a trial account at www.xalentis.com or grab it via Microsoft AppSource. Once signed-up, and logged-in, you’ll end up at the main dashboard as shown below.

xalgps1

Follow the Getting Started tutorial which takes about 10 minutes to create a pair of accounts and top them up with credit as required. The Ethereum network being used is a canned version of Microsoft’s Project Bletchley, so it’s not on the main or test Ethereum networks and can be used without spending any real Ether. You can deploy your own network and use that within the Fusion platform as well, by creating transaction filters pointing to your own deployed RPC node. Make sure your RPC node is visibly outside your firewall, obviously.

The following image shows us having created a transaction filter to monitor the default RPC node at http://xaleth4kq.eastus.cloudapp.azure.com, for any transactions made from the address 0x27f6f763ae5c52721db57c4423c298a78de1f22a. Filters can be created to match any transaction, from any address, or even containing a specific string value in the data portion. This is useful when the address(es) constantly change, while a specific identifier is passed within the data portion, perhaps a Device ID, Company ID or Serial Number of sorts – anything static.

xalgps2

Filters execute rules containing a simple compiled script, and this is where actions are performed on matching transactions. The script below has been added as a rule for our filter.

xalgps3

The rule simply extracts whatever is in the transaction data field, parses that and constructs a JSON packet. This packet will be passed to a Microsoft Flow we will be creating.

We’ll need a place to store our data. Using Flow, we could push directly into Dynamics 365, but since we don’t want to directly modify our ERP by adding a new table, I’ve chosen to use Microsoft Common Data Service as a temporary store instead. The image below shows the new Entity we’ve created with fields for Device, Temperature, Humidity, Longitude and Latitude.

GPS_CDS

Using Microsoft Flow, we’ll first use the Request action to accept an incoming POST request (from our Rule). Next, we’ll take the body of the POST request, parse it, and store the fields into our new CDS Entity. The Flow design is shown below.

GPS_Flow

Use the generated URL from the Flow to update the Rule – the final line calling Flow requires that URL.

Run the Flow, and then power up the IoT device to start submitting GPS and climate information into the Blockchain network. As transactions are mined into new blocks, Fusion will detect the transaction matching our Filter, and execute the associated Rule. The Rule in turn will parse the transaction data field, parse the content, construct it as JSON, and call our Flow with that body content. When the Flow executes, the JSON will be parsed and the data elements inserted as a new record into the CDS, as shown below.

GPS_DataCDS

We can use the data now stored in the CDS to create a PowerApp that displays that information on a Google Map. The PowerApp shown is fairly basic, but with enough time, patience and data this can be turned into something much more interactive, and it is real-time, a vast improvement over building a customer tracking portal from scratch, getting updates only when items are scanned with a barcode or RFID reader.

GPS_Map

Apart from our Rule script, we’ve used virtually no coding, and we’ve not modified our production ERP system in any way. As a bonus, we also have a mobile app that customers and partners can use!

RFID + IoT + Ethereum Blockchain

This is a very quick post, mostly code-only, showing how to read RFID tags on say an assembly line, process those using a WiFi IoT device (Adafruit Huzzah), extract the product serial number from the RFID tag and send that as part of a transaction to an Ethereum Blockchain.

I am using a MiFare RFID card reader, but you would want to use a long-range reader that supports low-cost sticker tags. POSTing the transaction to the blockchain takes a second or two, so don’t expect to scan 100 products/second flying past the reader and manage to submit those as transactions.

URL’s and codes are hard-baked, so consider how you want to post the transaction. I use static Ethereum addresses and stick the product ID in the data portion, you might want to read that from the RFID tags as well.

Once your product ID is in the blockchain, you might want to move that to your ERP system, or even a cool PowerApp or something. Unless you want to code X++ and mess around with your production systems, I suggest using Xalentis Fusion instead, to enable code-free integration between Ethereum and Microsoft Dynamics 365 for Finance and Operations. It also supports SMS, Email messaging, Service Bus messaging, Flow, PowerApps and Common Data Service.

Enjoy.

#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
 
#define RST_PIN         15
#define SS_PIN          2
#define SSID            "mywifiSSID" 
#define PASS            "password" 
 
MFRC522 mfrc522(SS_PIN, RST_PIN);
 
void setup() {
  Serial1.begin(115200);
  while(!Serial1){}
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
}
 
void loop()
{
  // Using MiFare card reader here, for production use sticker tags and high-speed reader instead
  MFRC522::MIFARE_Key key; // default to FFFFFFFFFFFF
  key.keyByte[0] = 0xFF;
  key.keyByte[1] = 0xFF;
  key.keyByte[2] = 0xFF;
  key.keyByte[3] = 0xFF;
  key.keyByte[4] = 0xFF;
  key.keyByte[5] = 0xFF;
 
  // Loop until RFID tag is presented 
  if (!mfrc522.PICC_IsNewCardPresent()) return;
  if (!mfrc522.PICC_ReadCardSerial()) return;

  byte readbuffer1[18];
  byte readbuffer2[18];
  byte block;
  MFRC522::StatusCode status;
  byte len;
  byte sizeread = sizeof(readbuffer1);
  block = 0;
  
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    return;
  }
  else
  {
    for (int i = 0; i < 18; i++)
    {
      readbuffer1[i] = 0x00;
      readbuffer2[i] = 0x00;
    }
    // read product ID from RFID tag
    status = mfrc522.MIFARE_Read(1, readbuffer1, &sizeread);
    if (status != MFRC522::STATUS_OK)
    {
      return;
    }
    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();
  }
 
  // convert product ID from RFID tag to hex string
  String data = "0x";
  for (int j=0; j<18;j++)
  {
    if (readbuffer1[j]=='\0') break;
    data = data + String(readbuffer1[j], HEX);
  }

  // build up our Ethereum transaction
  StaticJsonBuffer<1000> JSONbufferTwo;  
  JsonObject& uploadJSON = JSONbufferTwo.createObject(); 
  uploadJSON["jsonrpc"] = "2.0";
  uploadJSON["method"] = "personal_sendTransaction";      
  JsonArray&  uploadQueryParams = uploadJSON.createNestedArray("params");
  JsonObject& callTxParams = JSONbufferTwo.createObject();
  callTxParams["from"] = "0x27f6f763ae5c52721db57c4423c298a78de1f22a";
  callTxParams["to"] = "0xcaade3aa018d57d808fceb16824c47dfd206484c";
  callTxParams["value"] = "0x6FC23AC00"; //hex value 30 Gwei 
  callTxParams["gas"] = "0x30D40"; //hex value for 200000 -high gas limit for good measure          
  callTxParams["gasPrice"] = "0x6FC23AC00"; //hex value 30 Gwei gasprice 21gwei is typical
  callTxParams["data"] = data;
  uploadQueryParams.add(callTxParams);
  uploadQueryParams.add("bigsecretaccountpassword");
  uploadJSON["id"] = 1;
  String uploadString;
  uploadJSON.printTo(uploadString);
  callGeth(uploadString); // send for mining
}

String callGeth(String inputJSON) // thanks to https://github.com/gusgorman402
{
  HTTPClient http;
  http.begin("http://your RPC address here:8545/");
  http.addHeader("Content-Type", "application/json");
  int httpCode = http.POST(inputJSON);
  String JSONResult = http.getString(); // contains Txn
  http.end();
  return JSONResult;
}