sábado, 9 de outubro de 2021

Computer Vision with JavaFX and DJL

Computer Vision is not a new topic in Computer Science, but in the recent years it got a boost due the use of Neural Networks for classification, object detection, instance segmentation and pose prediction.

We can make use of these algorithms in Java using a Deep Learning library, such as DL4J as we already did in this blog with handwritten digits recognition, and detecting objects in a JavaFX application.

However, deep learning is popular mostly among Python developers (one reason is because Python easily wraps on top of native libraries, it will be easier in Java after Project Panama), so we have much more pre-trained models for Tensorflow and Pytorch libraries. It is possible to import them, but it requires a bit more of work then simply reusing a pre-trained model.

Fortunately there is a new library called Deep Java Library which offers a good set of pre-trained models. It makes use of Jupyter, which makes easier to try the library APIs. Another DJL feature is that it is made to wrap an existing library, so it works on top of Keras, Tensorflow, MXNet and other libraries.

In this post we will test some of the DJL Computer Vision models from a JavaFX application. Let's start first capturing webcam from JavaFX then use this input to a pre-trained model


Capturing Web Cam


The input data for the neural network we capture a webcam image. The project that worked without any issue with JavaFX on my Fedora 34 is capture-webcam. I used the JavaFX sample code and it just worked. See my workspace captured from the webcam:


Using Pre-trained Neural Networks

I started with a maven project that only used the webcam-capture. Then I added DJL maven dependencies and Eclipse allowed me to import the classes from DJL. Later I had also the ML library engine to run my models, this is how my final pom.xml looks like:


Back to the code, what I wanted was to grab the image from the webcam, input into a pre-trained model, get the result and print in JavaFX instead printing the webcam image. To allow users to test the pre-trained models we added a combo box to the user interface. 


To abstract the model we created a abstract class called MLModel. This class wraps the model call, so we can focus on printing the image and the UI will not know about any specific model, making it easy to add and remove models. The class MLModel grabs the predictions from the ML algorithm and draw on the image accordingly to the result type:

In the UI we have an array of implementations, which are selected when the combo box changes. We could use other ways to select the model, like Java Service Provider, but for our code we decided to keep it simple.



Finally it was time to implement the algorithms itself. First we created MLModel implementation for Object detection. It returns a class of type DetectedObject and we re able to build the pre-trained model as we want. We could select the engine, select the data used to train the model and other parameters using the class ai.djl.repository.zoo.Criteria. In our case we selected from the Engine Tensorflow a model that was trained using the mobilenet_v2 dataset. A video is on my twitter.





For Pose Prediction we had to internally run two models: the first to extract Person from the input image and the other to do the PoseEstimation itself. We also had to calculate the points of the pose relative to the input image. See a video for this model




Finally we also have Instance Segmentation, but it was so, so slow that I will not discuss it here. You are free to run the application and test it.



Conclusion

Java is a good alternative for building Machine Learning applications! DJL and its great model zoo makes it easier to reuse ms trained with other libraries. Next steps would be use other family of trained models, such as NLP and create more useful applications, like augmented reality!

Full code on Github.


quinta-feira, 10 de junho de 2021

Database Query Server using Quarkus



We are updating our application to Quarkus and I have this requirement of supporting datasouces that users of our application can create.

This is already supported in Widlfly, but does not seem to be supported on Quarkus. Before implementing this in our system we created a PoC which we share in this post.


The problem

Agroal is the part of Quarkus responsible for DataSources and pool connections. It is easy to configure datasources in Quarkus, but some properties only works during development time, after the JAR for distribution is built then you can't change some properties.

The solution is programatically create datasources so the application is flexible in a way that we can add datasets using system properties. We also must make sure that all supported data base drivers are in the application so users just needs to setup properties and no other action is required.

All drivers dependencies

Datasource from property

We can create datasources using a map of properties. The properties keys are defined in class AgroalPropertiesReader. When creating a properties reader we can specify a prefix which will be used to read the properties from the source.


To build the prefix we need all datasources mapped by the user followed by the properties setup for each mapped datasource. We can simply support the same properties as AgroalPropertiesReader. Here's a sample configuration


From Java we use Microprofile config to collect the datasource properties to a map, set the correct prefix and then build the properties that will be used to create a datasource. This is done right after the application is started so any error in configuration prevents the service to run.




Running Queries


Now that we map all datasources we simply have to expose it via REST to run queries using pure JDBC code. The trick is to transform any query result to a suitable data structure that can be translated to JSON, for this we use a MAP of LIST (the best data structure ever), where the key is the column name and the list are the rows for that specific column:



Now we can start our application and send requests to execute queries using HTTP requests:


curl --silent --data 'select id, variableId, value from VariableInstanceLog' http://localhost:8080/datasource/ds1/query



Finally you can run the built JAR using system properties and it will connect to any database:

java -Ddatasources=ds1,ds2 \

-Ddatasource.ds1.jdbcUrl=jdbc:mariadb://localhost:3306/jbpmdb \

-Ddatasource.ds1.providerClassName=org.mariadb.jdbc.Driver \

-Ddatasource.ds1.maxSize=10 \

-Ddatasource.ds1.principal=jbpm \

-Ddatasource.ds1.credential=jbpm \

-Ddatasource.ds2.jdbcUrl=jdbc:mariadb://localhost:3306/test \

-Ddatasource.ds2.providerClassName=org.mariadb.jdbc.Driver \

-Ddatasource.ds2.maxSize=10 \

-Ddatasource.ds2.principal=repasse \

-Ddatasource.ds2.credential=repasse \

-jar target/configurable-datasource-1.0.0-SNAPSHOT-runner.jar


Conclusion

In this post we show how we can use Agroal and quarkus to create an application that can run queries on any of the supported databases. The code is in my github

This application has room for a lot of improvements:


  • Create a UI
  • Better return error messages from REST endpoint
  • Add tests
  • Extend for other commands (INSERT, UPDATE and so on)
  • Support new data sources creation after the application is running (This could be useful for creating a generic Database Client)


Feel free to send PRs if you implement any of these improvements!




sábado, 14 de novembro de 2020

Creating Fat JARs for JavaFX Applications

A FAT JAR is a type of Java application distribution where a single JAR contains all other dependencies, so no additional file is required to run the JAR besides the Java Virtual Machine.

For any Java maven based application, creating a FAR JAR could be solved by using Maven Shade Plugin. However, creating FAT Jars using JavaFX may be a challenge because JavaFX uses modules.

Fortunately this subject was intensely discussed in the WEB, and a good explanation and a solution was provided by Jose Pereda in this StackOverflow response.

In this post I want to briefly share the steps to make a FAT JAR and post an example on my github so I can point others to check the example.


How to create a FAT JAR for a JavaFX application?

1- Create a main class that will run your application. This class must have the main method and call your actual application static launch method;

2- Add the Shade Plugin to your project. For those using Gradle notice that Jose Pereda also provided an answer about it in Stack Overflow;

3- In the shade plugin configuration make sure you are setting the Main class to be the one created in step 1.


That's basically all you need. If i is not clear you can check my sample project in github. The three files, App.java, Main.java and pom.xml can be checked below.


quinta-feira, 17 de setembro de 2020

Using jbang for ETL Scripts

As a Java programmer I prefer to use good old Java to extract/transform/load data to make it usable in tools I work for data visualization. However, it is a burden having to use maven just to write a single Java file. Hopefully there's a new tool called jbang that makes it easier to do scripting in Java.



More than simple scripts

What is impressive about jbang is that you are not restricted to standard Java APIs in fact you can use any library you want coming from Maven! What you will miss is the burden of having to deal with pom.xml and maven builds. Dependencies are simply declared in the Java file itself using a "DEPS" comment, for example:

//DEPS org.slf4j:slf4j-nop:1.7.25


Installation

To install jbang you just have to check the best way for you in downloads page.  In my case I have sdkman, so it was just a matter of running:


$ sdk install jbang


Hello World

The simplest hello world we can thing can run with jbang without any additional configuration, just install jbang and you should be able to run java files. So let's say I have a hello.java just like this


I can run it using: jbang hello.java. With an additional comment at the begging of the file I can make the file work as an executable file. Add the comment as the file first line (linux environments) and make the file executable:

///usr/bin/env jbang "$0" "$@" ; exit $? 

chmod +x hello.java

Then you should be able to run using ./hello.java. This comment can be added to existing files, but if you are starting a new script with jbang just use: $ jbang init hello.java


Real world script with external dependencies

In real world you will need external dependencies. I had a Java file I use to process some CSV files, it is in a maven project and this version can be seen here. To run it with jbang here' what I did:

  • Went to the Java file and added these comments to the beginning:

//usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS org.apache.commons:commons-csv:1.8

//JAVA 11


And that should be it... But for my project I did two additional steps:

  • Got rid of Maven stuff
  • Removed package declaration
My ETL folder is clear now and I can use Java and have contributions from others because they only need to have jbang installed to run my Java file. 

Development is easier, you will not lose content assist or IDE goodies with jbang. For example, I can edit a Java file in vscode by just using: code `jbang edit myfile.java`

Learning more


This is not all, specially with JavaFX, where all the setup is done by jbang! If you want to learn more I recommend you to check jbang examples and  this talk from  jbang father Max Andersen: