© Nicolas Modrzyk 2020
N. ModrzykReal-Time IoT Imaging with Deep Neural Networkshttps://doi.org/10.1007/978-1-4842-5722-7_1

1. Getting Started

Nicolas Modrzyk1 
(1)
Tokyo, Tokyo, Japan
 

One of the goals of this book is to get you ready to perform real-time IoT imaging quickly, avoiding a lengthy installation process. Being ready quickly doesn’t mean we are going to take any shortcuts, it means we will get the tooling part out of the way so we can focus on the creation process.

In this chapter, you’ll run your first example.

Visual Studio Code Primer

You can’t knock on opportunity’s door and not be ready.

—Bruno Mars

The playground setup introduced in this book is fairly standard for people who are used to writing code. It also has a little bit of a “new kid on the block” feeling—only the cool kids use it. The stack , or the environment, we will use consists of the following:
  • Visual Studio Code, a small but pluggable text editor

  • The Java Development Kit and its runtime, so we can write Java code and run it

  • A Java plugin to Visual Studio Code so that the editor understands the Java code and runs it

Basically, that’s all.

It does not really matter whether you’re using Windows, Mac, or Linux. All the different pieces of XX are made to run anywhere. To install Visual Studio Code, you need to head to https://code.visualstudio.com/.

Click the download button on the top-right side of the page. After you click the button, you’ll be presented with options for the different packages, one for each computer platform, as shown in Figure 1-1.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig1_HTML.jpg
Figure 1-1

Choosing your download

At the time of writing, the current version is 1.40, but newer versions would only be better.

Running the installer and opening Visual Studio Code for the first time gives you a screen similar to the one in Figure 1-2.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig2_HTML.jpg
Figure 1-2

Visual Studio Code

The second step is to install Java, if it is not already installed. You can either head to the OpenJDK web site (https://jdk.java.net/) and download a zip file or go to the Oracle web site and download a ready-to-use installer for your machine (see Figure 1-3 and Figure 1-4). Specifically, you can go to https://www.oracle.com/technetwork/java/javase/downloads/index.html.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig3_HTML.jpg
Figure 1-3

Oracle Java download page

../images/490964_1_En_1_Chapter/490964_1_En_1_Fig4_HTML.jpg
Figure 1-4

Java download link

Let the installer run to the end, and do not stop it even if it tries to open another installer while running. Java, being a development kit, does not come with a fancy application to check that it has been installed properly, so after the Java installation is finished, a quick way to check that things are in place is to open a terminal in Visual Studio Code and check the Java version.

You can press Ctrl+Shift+@ to open a terminal inside Visual Studio Code, or you can open it from the menu, as shown in Figure 1-5.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig5_HTML.jpg
Figure 1-5

Opening a terminal window from within Visual Studio Code

This will pop up a small tab, usually at the bottom of the editor, as shown in Figure 1-6.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig6_HTML.jpg
Figure 1-6

The Terminal tab

Then inside the terminal, type the following command:
java -version
Figure 1-7 shows the expected version output if you have installed the OpenJDK version.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig7_HTML.jpg
Figure 1-7

Java version

Note that the Java version itself is not important; any version between 8 and 14 is expected to work. Heck, let’s be optimistic—things should also work for the foreseeable future.

We are almost there with all the setup, so bear with me for a few more seconds. In Visual Studio Code, everything you write will be handled as simple text. Basically, if you do not tell a computer what to do with text, it will not do anything.

Let’s tell Visual Studio Code to understand our Java files by installing the Java plugin.

The left sidebar of the editor has a set of rather cute icons, and if you click the one at the bottom of Figure 1-8, you can access the Visual Studio Code Marketplace.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig8_HTML.jpg
Figure 1-8

Visual Studio Code Marketplace

From here, you can install plug-ins in the editor to extend it in many different ways. We are going to make heavy use of plugins in this book, and build a setup which you can reuse and enhance for other situations.

For now, we want to install Java support from within the editor, which is done by searching for the Java extension using the search bar (see Figure 1-9).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig9_HTML.jpg
Figure 1-9

In search of the perfect Java plugin

Selecting the plugin on the left gives an extensive description on a tab on the right, as shown in Figure 1-10.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig10_HTML.jpg
Figure 1-10

Description of the Java plugin

The Java extension is actually a collection of plugins, and there is a full entry and description available in the Visual Studio Code documentation, available here:
https://code.visualstudio.com/docs/java/extensions

These plugins will allow you to perform all the tasks that are useful when writing code, such as autocompletion, code inspection, debugging, and more.

At this stage, after reloading the editor, you are essentially done with the installation steps. The fun development environment is ready; it hasn’t cost you a penny, and almost no personal data has been leaked.

Running Your First Java Application

So, with the editor open, let’s create a file, write some Java code in it, and see how to run the code directly from within the editor.

Running code in Java usually implies a compilation step, where the Java text file is converted to a form that the computer, and here the Java runtime, can execute.

In our setup, all those steps are being handled in the background by Visual Studio Code.

Our first example will just output a greeting, as is usually the case with any well-behaved computer program.

First, let’s create a file named First.java and drop in some very basic Java code. The code will output some basic greeting text; this will also help us check that the setup is fully working.

Let’s write the content in Listing 1-1 in the Java file.
public class First {
    public static void main(String[] args) {
        System.out.println("Hello Java");
    }
}
Listing 1-1

Your First Java Program

After entering this code (or just opening the sample provided), you will notice a few dynamic refreshes happening in the editor itself. The editor will highlight the code for you and associate the different Java features to your typing, as shown in Figure 1-11.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig11_HTML.jpg
Figure 1-11

Editor magic

Autocompletion of the code is included for free and can be triggered by using the Tab key, as shown in Figure 1-12.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig12_HTML.jpg
Figure 1-12

Autocompletion

You can also access the Java documentation when available, as shown in Figure 1-13, by using the keyboard combination Ctrl+spacebar.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig13_HTML.jpg
Figure 1-13

Inline documentation

You have also probably already noticed the Run and Debug links at the top of the main Java method, as shown in Figure 1-14. You can click these links or trigger them with keyboard shortcuts. Press F5 for debugging and press Ctrl+F5 to run the code.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig14_HTML.jpg
Figure 1-14

Run and Debug links

Clicking the Run button will, as you would expect, trigger the execution of the code inside the editor and display some text in the Terminal tab we opened earlier (Figure 1-15).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig15_HTML.jpg
Figure 1-15

Running some Java code for real

Importing Core Java Packages

You will need to have external classes loaded eventually, and this can be done for you using the editor’s import function. This seems trivial at this stage, but when using OpenCV in Java, some of the packages are hard to figure out, especially when a lot of the code samples do not bother writing the imports for conciseness.

First, let’s write a piece of code that displays the current time. The easiest way to do this is to simply use a Date object , as shown in Listing 1-2.
public class First {
    public static void main(String[] args) {
        Date d = new Date();
        System.out.println(String.format("hello java. it is %s", d));
    }
}
Listing 1-2

Before Imports

In the print version of this book, everything looks OK, but in Visual Studio the editor rightfully highlights the parts of the code that cannot be compiled properly. In Figure 1-16, notice how the code cannot be understood by the compiler and is being underlined.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig16_HTML.jpg
Figure 1-16

Missing Date class import

To import the class, you can click the small light bulb next to the Date word that is underlined in Red (Figure 1-17).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig17_HTML.jpg
Figure 1-17

Importing using the light bulb

Alternatively, you can trigger the Visual Studio command menu by pressing Ctrl+Shift+P or Command+Shift+P and start typing Organize Imports in the bar (see Figure 1-18).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig18_HTML.jpg
Figure 1-18

Organizing imports

You can select the exact Java class that needs to be imported in the current file, which here is java.util.Date (see Figure 1-19).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig19_HTML.jpg
Figure 1-19

Selecting the proper class

Now that the code is fixed, it is ready to be executed again. Let’s click the same Run link or use the F5 keyboard shortcut to start the execution.

The execution output is once again shown in the terminal, as shown in Figure 1-20.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig20_HTML.jpg
Figure 1-20

Wow. That code was written and executed in November 2019

Debugging Lesson

This is your book, so you can skip this section on debugging and come back to it later if you want. I decided to add this section on debugging now, when we’re working with some simple code, because getting this part done up front allows you to master the OpenCV code better and go through each OpenCV computation step in a nice and easy-to-understand way.

If you decided to keep reading this part, then great! We are going to spend just a little bit of time looking at how to debug Java code from within Visual Studio Code.

As you have seen, executing the code was quite simple from within the editor. In Java and with this setup, we also get a free debugger. You even get free debuggers in browsers nowadays when running JavaScript code from web pages.

Why is a debugger that important? A debugger allows you to stop the execution at any point in the code and then locally inspect it and act from the position of the stopped code.

That means you can start executing the code but also ask the runtime to stop wherever you want. Also, you do not have to choose where to stop code execution before starting it. Let’s say you do video processing in real time, and at some stage, you would like to know why the code analyzing the camera feed is not finding objects as you would expect it to do. In that case, you can stop the code execution right in the frame capture loop and either dump the picture or rerun the analysis step-by-step so you can find out if the model is wrong or if the picture is still in an unexpected size or color mode.

Java itself does not have a really useful read-eval-print-loop (REPL). A REPL allows you to execute code line by line, and it works quite well with my favorite language, Clojure, and even with Kotlin and of course Python. Java does not make it easy to run and add things line by line, which can be almost forgiven now that debugging has been made so easy from Visual Studio Code.

Enough talking, let’s see how to use the debugger to perform basic debugging tasks:
  • Add a breakpoint

  • Execute the code step-by-step

  • Watch a variable

  • Change a variable value

Add a Breakpoint

A breakpoint is, as its name implies, a point in time to take a break. It is shown as a red dot in the editor while moving the mouse on the left side of the line numbers (Figure 1-21).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig21_HTML.jpg
Figure 1-21

Breakpoint

To create a breakpoint, simply click the line number on which you want to stop the execution. Note that the breakpoint will stop the code before any of the code on that line is executed, so adding a breakpoint on line 5 will not show the Date object when the execution stops, but adding a dot on line 6 will, as shown in Figure 1-21.

Execute the Code Step-by-Step

Clicking the Debug link will start the execution in Debug mode and will stop it at the first breakpoint, the one you just added on line 6, and the editor will display a slightly different layout, as shown in Figure 1-22.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig22_HTML.jpg
Figure 1-22

Taking a break

On the left side, you can see on the small Call Stack tab where the code execution has been stopped; here it’s on line 6 in the class First in the main method (Figure 1-23).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig23_HTML.jpg
Figure 1-23

Where am I?

You can also see the value of the variable d, which is a Date object, and you can also see the input parameters given to the program if there are any; here you see an empty array (Figure 1-24).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig24_HTML.jpg
Figure 1-24

Variables

You can of course play with the different arrows and show the extended content, recursively, and fields of each variable one by one, as shown in Figure 1-25.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig25_HTML.jpg
Figure 1-25

Expanded variables

In Figure 1-25 you can see for yourself that the Date object has a field named cdate, and you can see recursively all the fields of that field.

Resume Execution

To resume or finish the execution from the breakpoint, you have a few options that are available in the runtime bar located at the top of the editor while doing a debugging session (Figure 1-26).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig26_HTML.jpg
Figure 1-26

Execution commands

From left to right you can perform actions explained in Table 1-1.
Table 1-1

Debugging Actions

Icon

Action

Comments

../images/490964_1_En_1_Chapter/490964_1_En_1_Figa_HTML.jpg

Resume

This will tell the execution to go to either the end of the program or the next breakpoint without stopping.

../images/490964_1_En_1_Chapter/490964_1_En_1_Figb_HTML.jpg

Step Over

This executes all the code on the current line, goes to the next line of code, and then stops again.

../images/490964_1_En_1_Chapter/490964_1_En_1_Figc_HTML.jpg

Step Inside

This goes inside each block of execution, so in the previous example, on the first stop, the execution will go inside the format function and then stop.

../images/490964_1_En_1_Chapter/490964_1_En_1_Figd_HTML.jpg

Step outside

This goes to the outer part of the code, so if you were currently in the format function, then this goes back to the original main() function.

../images/490964_1_En_1_Chapter/490964_1_En_1_Fige_HTML.jpg

Restart

This stops all the debugging and restarts from scratch.

../images/490964_1_En_1_Chapter/490964_1_En_1_Figf_HTML.jpg

Stop

This stops the code execution.

../images/490964_1_En_1_Chapter/490964_1_En_1_Figg_HTML.jpg

Hot Code Replace

This updates the code in the runtime, with code from the editor. Real-time coding!

The action you will use most of the time is Step Over, which just executes the code line by line. This gives you a good overview of the variables that have been changed and the newly assigned variables and their values. See Figure 1-27.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig27_HTML.jpg
Figure 1-27

Next line

Going to the next line and then to the next one will eventually finish the program execution, ideally with the proper message printed in the terminal window.

Watch an Expression

You may have noticed the Watch tab on the left side of the editor and may already have wondered what it is for.

On the top-left Variables tab, you can see the direct values of objects and fields, but on the Watch tab you can add function calls on all the available objects, in what is called a watch expression. Watch expressions can be defined before the variable or the object is instantiated.

In object vision, and while doing direct rendering, you can use a watch expression to produce some side effects, create functions to, for example, turn the input picture from colored to black and white, or quickly create a contour-only version of an image. You can, of course, do all this in real time, but in the coming chapters we will be working with low processing power and memory, so briefly pausing the code execution and using a one-off call to a watch expression is definitely better to keep the extra memory and computation to a minimum.

In this first chapter, we will add simple computations on the watch expression—one with no side effect directly returning a value based on the input parameters and one with a side effect printing to a log file.

We’ll start from the code sample in Listing 1-3, which is simply looping over a value that is incremented in a while loop. We do not want to burn up the computer while doing this, so we’ll add a short sleep call inside the loop to give some time to the execution loop.
public class Second {
    public static void main(final String[] args) throws Exception {
        int i = 0;
        while (true) {
            i++;
            Thread.sleep(500);
        }
    }
}
Listing 1-3

The Incredible Loop Over an Integer

We’ll add the expression i+2 in the watch expression and start the debugging session without actually setting a breakpoint yet.

After waiting some time, let’s click to add a breakpoint on line 6 and see the editor stop as if we had set the breakpoint before starting the execution.

The Visual Studio Code layout will look like Figure 1-28.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig28_HTML.jpg
Figure 1-28

The incredible loop in Debug mode

By clicking and creating a breakpoint, you have just stopped the execution, and the debug layout appears. As you can see, the variable i already has a value of 7, and the watch expression of i+2 is showing the correct value of 9.

Nice.

Now for some exercises that you can try on your own. Try implementing two functions and use them inside two watch expressions.
  • One function will just do a basic computation on the variable i and display it.

  • Another function will not return anything but append a value to an external file.

This should take around 5–10 minutes of your time. Good luck! Once you’re finished, you can read and compare your code with Listing 1-4.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
public class SecondFinal {
    static int myfunction1(final int i) {
        return 3 + i + 2;
    }
    final static String filePath = "my.log";
    static void myfunction2(final int i) {
        try (Writer writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(String.format(">> %d ", i));
        } catch (Exception e) {
        }
    }
    public static void main(final String[] args) throws Exception {
        int i = 0;
        while (true) {
            i++;
            Thread.sleep(500);
        }
    }
}
Listing 1-4

Watched Expressions with Side Effects, and No Side Effect

The different watch expressions are showing as expected on the left side in the debugging layout of Visual Studio Code (Figure 1-29).
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig29_HTML.jpg
Figure 1-29

Watch expression

That wasn’t so hard, was it? Now the good news is that you can reuse those techniques when working with the imaging library OpenCV. For example, you can do the following:
  • Output the width and height, as well as the number of color channels of an image

  • Output the Hough lines of an image in an external file

  • Output the number of detected faces in an image

Remember, you can add watch expressions after starting the code execution in Debug mode, so add them any time you need them.

Change a Variable Value

One last thing to know about debugging in Visual Studio Code before being done is that you can change the value of a variable in real time, straight from the Variables tab.

Now that is some superpower!

You can do this by double-clicking the variable value you would like to change, as shown in Figure 1-30.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig30_HTML.jpg
Figure 1-30

Updating a variable value

Observe how the related watch expressions are being recomputed for you in Figure 1-31.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig31_HTML.jpg
Figure 1-31

You can see the related watch values being updated

Sweet. In the context of imaging and neural networks, you can directly update alpha, gamma, contrast, etc., directly and have the recomputed images or detection happen in real time.

Wrapping Things Up

We spent a bit of time learning about debugging, so picture how much can be done by reusing the techniques you have just seen in the context of imaging and object detection.

For example, Figure 1-32 shows how a well-designed debugging layout can be used as a full user interface to update values for a sepia conversion.
../images/490964_1_En_1_Chapter/490964_1_En_1_Fig32_HTML.jpg
Figure 1-32

Marcel the cat in sepia

In this layout, you can see the following:
  • The array of values used to recompute the sepia has been extracted in the main code as a double array.

  • While debugging, the rgb array and its values are available on the Variables tab and can be updated at will.

  • The sepia function here is used only as a debugging function, not in the main code.

  • That sepia function outputs a picture in a file named sepia.jpg.

  • A watch expression is added, making a simple call to the sepia function defined earlier.

  • The sepia.jpg picture is opened within Visual Studio Code, and any update of values in the rgb array will output a new picture in real time.

Now that you have an idea of how things can be worked on from within Visual Code Studio, let’s move on to some OpenCV fun.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.126.239