Python Fundamentals for Robotics

I have mentioned before that C++ and Python are the most popular languages in Robotics. C++ is the best language if you want great performance. For example, if you want to write a robot vision algorithm that needs to run in real time, use C++. But if you are looking to quickly prototype something and don’t want the headaches of having errors fire all over your program because you forgot a semicolon, you should use Python. While Python uses more computing resources than C++, it is much simpler and far more intuitive.

In this tutorial, we will learn the fundamentals of Python for programming robots. Getting your head around these fundamentals will help you immensely when you learn ROS.

Table of Contents

You Will Need

In order to complete this tutorial, you will need:

Directions

How to Install Python in Ubuntu Linux

To install Python, open a new terminal window and type:

sudo apt-get install python python3

Find out what the default version of Python is on your system by typing:

python
1-python-versions

The default is versions 2.7.15 and higher.

2-default-python-version

C++ is a compiled language. You write source code, compile it into object/machine code, and then run that code. Python skips the compilation step, and instead runs (i.e. interprets) the source code directly without any compilation step. To find out where the Python interpreter is located, type this command.

which python
3-which-python

You can also type the following command to get the location of the interpreter as well as the documentation and other Python-associated files.

whereis python
4-whereis-python

Return to Table of Contents

Write Your First Program in Python

For our first program in Python, we will write a program that prints “Hello World” to the screen. Open up a terminal window and type:

python

Now type:

print(“Hello World”)
5-hello-world-terminal

What I showed you above is how to write a program directly inside the Python interpreter. You can keep writing different Python commands as you please. You write a line of code, and the interpreter executes that code, one line at a time.

The more common way to write Python programs is to create Python files (called scripts) containing lots of lines of code. You then run those files using the Python interpreter. Python files end with the .py extension. Let’s write a Python script now.

Open a terminal window and type gedit to open up the Linux text editor. Type the following program, and save it as hello_world.py. I am following the Google Python Style Guide.

6-hello-world-program

The program above might look a little intimidating at first. In the first line I told the program where to find the Python interpreter (#! /usr/bin/env python). That line is known as Shebang.

Then I wrote some comments to tell what the program does and what command to type in the terminal window to run the code (python hello_world.py).

Then I defined a method named main. This is where we implement our code.

The final block of code prints the comments and runs the main method.

Now, let’s exit the terminal.

Open a terminal window and move to the directory where hello_world.py is located.

Run the program.

python hello_world.py
7-run-hello-world

Another way is to run a program is to make a Python script executable, similar to what we do in C++. Open a new terminal window, move to where your hello_world.py program is located and type:

chmod +x hello_world.py
./hello_world.py
8-run-chmod

To rerun the program, you can press the up arrow on your keyboard to find the command again. Then press Enter.

9-run-chmod-again

Return to Table of Contents

Python Crash Course

Now that you know how to create Python programs and run them, check out the tutorial below to get a good foundation in the basics of Python. Do each tutorial in the “Learn the Basics” section. The tutorials are designed to be interactive. They cover the Python fundamentals that you will encounter repeatedly as you use this language to program robots. Proceed through each tutorial carefully so that you understand what is going on. 

Once you have done the basics section of the tutorial above, you will have a firm foundation in Python to begin the fun stuff…programming robots!

Return to Table of Contents

C++ Fundamentals for Robotics

In this post, we will learn the fundamentals of C++, the most popular language (along with Python) for programming robots. Getting your head around these fundamentals will help you immensely when you learn ROS. The only prerequisite knowledge is that you have some basic programming experience in any language.

Table of Contents

You Will Need

In order to complete this tutorial, you will need:

Directions

How to Install the GCC/G++ Compilers

C++ is a compiled language. What that means is that when you want to run a program that you write in C++, your machine needs to have a special program that translates the C++ that you write into language that the computer understands and can execute.

The built-in compiler for C/C++ (i.e. C language and C++ language) is called GCC/G++. Let’s see if the compiler is installed on your machine.

Within Ubuntu Linux, open up a new terminal window.

1-new-terminal-window

Type the following command to see if you have the C compiler (named GCC):

gcc
2-gcc-not-installed

GCC is not installed. Before installing it, let’s update the package list using this command:

sudo apt-get update

Now use this command to install a bunch of packages, including GCC, G++, and GNU Make:

sudo apt install build-essential

You might see some sort of error about things being locked if you try the following command. If you do, kill all processes that are using the APT package management tool using this command:

sudo killall apt apt-get

Remove the lock files:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*

Reconfigure the packages and update:

sudo dpkg --configure -a
sudo apt update

Now use this command to install numerous packages, including GCC, G++, and GNU Make:

sudo apt install build-essential
Press Y to continue.

Wait while the whole thing downloads.

Now, install the manual pages about using GNU/Linux for development (note: it might already be installed):

sudo apt-get install manpages-dev

Check to see if both GCC and G++ are installed.

whereis gcc
whereis g++
3-whereis-gcc-gpp

Check what version you have.

gcc --version
g++ --version
4-version-checking

Return to Table of Contents

How to Install the C/C++ Debugger

In this section, we will install the C/C++ debugger. It is called GNU Debugger (GDB) and enables us to detect problems or bugs in the code that we write.

In the terminal window, type the following command:

sudo apt-get install gdb

You will be asked to type your password, and then click Enter.

Type the following command to verify that it is installed:

gdb
5-install-gdb

Type this command to quit.

q

Exit the terminal.

exit

Return to Table of Contents

Write Your First Program in C++

On the Ubuntu Linux desktop, click the 9 dots in the bottom left of the screen and search for the text editor named gedit. Double-click on it.

6-search-for-gedit
7-search-for-gedit
8-double-click-gedit

Write the following code and save it as hello_world.cpp.

9-hello-world

This page on GeeksforGeeks explains what each line of code does.

You can close the text editor now by clicking on the x on the upper right corner of the screen.

Click the file cabinet on the left side of the screen. You should see your hello_world.cpp file. Drag and drop it on your Desktop.

1-drag-to-desktop

How to Compile and Run Your Program

We now need to compile the hello_world.cpp program so that your computer will be able to read it.

Since the file is located on the desktop, open a terminal window, and type the following command:

cd Desktop

Type the following command to see if it is there:

dir
10-check-desktop

Compile hello_world.cpp by typing the following command:

g++ hello_world.cpp

If you see an error, check your program to see if it is exactly like I wrote it.

If you don’t see an error, type the dir command to see if the a.out file is there, then run the program by typing the following command:

./a.out
11-run-first-program

Congratulations! You have created and run your first C++ program. 

In this example, the name of the executable file was a.out. If we wanted to use another name, we could do that too.

g++ hello_world.cpp -o hello

Type dir to see the new file.

12-hello-custom-name

Type the following command to run it.

./hello
13-hello-custom-name

Return to Table of Contents

How to Debug Code Written in C++

In this section we’ll learn how to debug code written in C++. Debug means to remove errors from a program.

Let’s create a file called product.cpp. We will multiply two numbers together and display the product.

Search for the gedit text editor in Ubuntu Linux. Open the application.

Write the program.

14-product-cpp

Compile the program using the following command. We have to add that -g option so that the code is built with debugging functionality.

g++ -g product.cpp -o product

Execute the code using the following command:

./product
15-product-output

Now, debug the code using the following command:

gdb product

Create a breakpoint at line 5. A breakpoint tells the debugger to run the code but stop at line 5.

b 5
16-breakpoint-line-5

Now, run the program and stop at the breakpoint.

r
17-run-it

Execute the next line of code.

n

Print the value of the first_number variable.

p first_number
18-print-first-number

Run the rest of the program.

r

Type:

n
19-type-r-then-n

Return to Table of Contents

Classes and Objects in C++

The main difference between C and C++ is that C++ enables the programmer to define what are known as classes. A class has its own data and functions (i.e. behavior, methods, etc..). A class often represents something that we might find in the real world.

For example, consider the Car class. There are many different types of cars one can buy, but all cars have certain things in common. The data for the car class might include the following:

  • Number of wheels
  • Make
  • Model
  • Year
  • Current speed

Functions for the car class might include the following:

  • Speed up
  • Slow down

Classes are blueprints (i.e. templates) for what are called objects. Each time we want to create a new car in a program, for example, instead of defining that car’s data and methods from scratch, we can use the Car class as a template to define the new car’s data (number of wheels, make, model, year, etc.), and we can use the car’s functions to change its data (e.g. speed up and slow down will change the current speed).

Geeks for Geeks has a good, brief  tutorial on creating classes and objects in C++. I recommend you go through it now.

After going to that tutorial above, I recommend you go through the “Basics of C++” tutorial at Geeks for Geeks to make sure you have practice with the tasks in C++ that you will do again and again. Go slowly so you understand what you are doing and why you are doing it. No need to hurry.

Now quickly skim the following tutorials. No need to do a deep dive into any of these tutorials. I just want you to have a high-level understanding of what you can do in C++. You can come back to these tutorials when you need them during an actual project:

Return to Table of Contents

How to Create a C++ Project

In many large C++ projects, you will have to compile multiple programs, each containing hundreds or even thousands of lines of code. In addition to being compiled, the programs might need to be linked together. In this section, I will show you how to do this using a tool known as a Linux makefile.

Let’s develop a program again that multiplies two numbers together. This time, we will create a class called Product. The product class has only one method (or function) called calculate. The calculate method takes as input two numbers, and it outputs the product of those two numbers.

Best practice when creating classes in C++ is to split the class into two files: one file to declare the Product class (i.e. tell the compiler that a class exists called product) and one file to implement the Product class (i.e. to implement the Product class’s data and methods). We then need a third file that creates an object of the Product class and performs the multiplication. This last file is called main.cpp:

  1. Product.h: This is a header file for the Product class. We declare the Product class here (both data and methods) rather than in the Product.cpp file. 
    • Header files help make your projects more organized and speed up compile time. 
  2. Product.cpp: We already declared the data and methods of the product class in the Product.h file, but we now need to implement them. Product.cpp is the program where we implement the data and methods of the Product class.
  3. main.cpp: This is the main code of the project that is going to get built into an executable file that your machine understands. Here is where we create an object of the Product class and perform the multiplication. 

In case you are confused at the difference between declaring something and implementing something, take a look at the example below of my Product.h file:

20-product-h

Above we have only declared the Product class. We have not yet implemented it. Let’s do that now in the Product.cpp file:

21-product-cpp

Now that we have implemented the Product class, we need to create one more file, the main.cpp file. This file is where we perform the actual multiplication of two numbers. Make sure your code looks exactly like what I have below.

22-main-cpp

Now that we have our class declaration, class implementation, and main program, we are ready to compile the code and execute it. We can do all that in two lines in the Linux terminal. Move to the directory where your files are located. Mine are located on my desktop, so I open up a Linux terminal window and type:

cd Desktop

Type the dir command to get a list of the files in your desktop.

And then compile the code using the following command:

g++ Product.cpp main.cpp -o main
23-compile

Type dir

24-executable-main

Notice that you have an executable file called main. This is the main program that includes all that code you developed above in a nice neat package that is ready to be executed. Let’s execute it now by typing:

./main
25-output-33

If you got “Output = 33”, congratulations! If you did not get that answer or go an error, go back to the code and make sure it is written exactly as I have written above.

Now remove that executable file:

rm main

rm above means “remove.”

Return to Table of Contents

How to Create a Linux Makefile

In the example above, each time we make changes to any of the three source files, we have to create a new executable file by typing the “g++ Product.cpp main.cpp -o main” command. Now imagine if we had a project that had 100 source files. Having to type out the command to compile 100 source files each time we made a tiny change to just one of the source files would get annoying really fast!

Fortunately, Linux has some solutions for this. One of the solutions is called a Linux Makefile. A makefile is a text file (or small program) that contains the commands that you would ordinarily need to type out manually to link, compile, and build the executable for all your source code. A makefile is run in Linux using the make command. When you run the make command on a makefile, all your source code files are linked, compiled, and built into an executable file (like we did in the previous section).

Let’s suppose that you have 100 source files, and you have made a tiny change to one of those source files. One really cool thing about running the make command is that it knows which files have changed since the last time you built the executable. make will only rebuild files that changed since the previous version. So rather than rebuilding all 100 files each time you need to link, compile, and build your project, make will ensure that only one file is rebuilt, and everything else remains unchanged. You can imagine how much time make will save you.

The official manual for creating Makefiles is here at GNU.org in case you ever need a detailed reference to refer to. I’ll show you below how to create and use a makefile for the source code we developed in the last section.

Open up a new terminal window. Move to your desktop, and create a new directory called product_project using the mkdir (i.e. make directory) command.

Move to that directory using the cd product_project command.

26-cd-product-project

Type gedit to open up the text editor.

Create the following makefile exactly as I have written below. Save it as makefile. The syntax is complicated, but don’t worry about trying to memorize this. Just refer back to this tutorial when you need to create a makefile in the future. You can also consult the makefile manual.

27-makefile
28-save-as-makefile

After you have saved the makefile, open a new terminal window, move to the Desktop, and move your three programs (Product.cpp, Product.h and main.cpp) to this product_project directory using the mv (i.e. move) command. We need to move all cpp and h files.

mv ~/Desktop/*.cpp ~/Desktop/product_project/
mv ~/Desktop/*.h ~/Desktop/product_project/
29-move-files

Now we need to execute the makefile.

make
30-make

You will now have three new files in your directory: executable file named main and two object files named main.o and Product.o.

Now that the program is built, we can run it.

./main
31-run-main

Return to Table of Contents

How to Create a CMake File

An alternative to using a Linux makefile to build your C++ project is to use a software tool known as CMake. CMake is one of the most popular tools for building C++ projects because it is relatively user-friendly (compared to the Linux makefile process I described in the previous section). Here is a link to the CMake documentation.

Let’s see how to build a C++ project using CMake.

First, go to the desktop and create a product_project_2 folder. Move (or copy and paste) your three source code files (Product.h, Product.cpp, and main.cpp) to that new folder. 

32-new-folder

Install CMake.

sudo apt-get install cmake

Make sure you are in the product_project_2 folder. Then create the following text file and save it as CMakeLists.txt. This file builds the executable file called main from main.cpp and Product.cpp.

33-cmake-file

Close the text editor to return to the terminal window.

Create a new folder.

mkdir build

Move to the build folder.

Type the following command:

cmake ..
34-run-cmake

Now you need to build the project. Type:

make
35-build-target

Run the code:

./main

If everything worked properly, you should see Output = 33

36-run-the-code

Congratulations! We have covered a lot of ground. You have come a long way from the beginning of this tutorial and now have a solid foundation in C++, the most popular language for building robots (along with Python, which I will cover in a future post).

Return to Table of Contents

How to Install Ubuntu and VirtualBox on a Windows PC

In this project, we will get started on our Robot Operating System (ROS) programming journey by installing Ubuntu. Ubuntu is a popular distribution (i.e. flavor) of the Linux operating system and is fully supported by ROS, the most popular framework for writing robotics software. If you have a Windows PC (I have Windows 10), I recommend you install a VirtualBox first and then install Ubuntu in the Virtual Box. I’ll show you how to do all that below.

The process for installing Ubuntu has a lot of steps, so hold on tight, don’t give up if something goes wrong, and go slowly so that you get your installation setup properly. Let’s get started!

Requirements

Here are the project requirements:

  • Install Ubuntu
  • Install Virtual Box
  • Install Ubuntu on VirtualBox
  • Learn Important Linux Terminal Commands

You Will Need

The following components are used in this project. You will need:

Directions

Download the Ubuntu Image

Check Ubuntu Releases to find the latest version of Ubuntu that has long term support (LTS). As of this writing, the latest release is Ubuntu 18.04.3 LTS (Bionic Beaver).

1-ubuntu-releasesJPG

Click on the latest release of Ubuntu, and download the 64-bit PC (AMD64) desktop image (.iso file).

Before installing Ubuntu, you need to install Virtual Box. Virtual Box extends the capabilities of your host computer (i.e. your laptop or desktop PC) by enabling you to install and run an operating system in a new environment on top of your current operating system (Windows 10 in my case). The environment the new operating system will run in is known as a virtual machine (or guest).

Install VirtualBox

Let’s download VirtualBox. Go to VirtualBox downloads.

Select the platform package for Windows hosts to download the executable (.exe) file.

2-virtualbox-downloadJPG

Detailed installation instructions for all operating systems (Windows, Mac OS, Linux, and Solaris) can be found in the instruction manual. Let’s go through the steps.

Double-click on the executable file.

3-double-click-executable-virtual-boxJPG

Click Next to begin the installation process.

Click Next to install the VirtualBox in the default location.

4-click-nextJPG

Click Next to choose the default features.

5-default-featuresJPG

You will see a warning about network interfaces. Ignore it, and click Yes to proceed.

6-warning-network-interfacesJPG

You are now ready to install VirtualBox. Click Install to proceed.

7-click-installJPG

Click Yes to allow the software to make changes to your device.

Click Install again.

8-click-install-againJPG

Click Finish to run VirtualBox.

9-click-finishJPG

You can optionally delete the original executable file for VirtualBox (the one with the .exe extension). You don’t need it anymore.

Create a Virtual Machine

Now that VirtualBox is installed on your computer, we need to now create a new virtual machine.

Click the New button in the toolbar.

10-start-virtual-boxJPG

Type in a descriptive name for your operating system. You can stick with the default machine folder. The machine folder is where your virtual machines will be stored.

Also, select the operating system that you want to later install (Linux in this case).

12-virtualbox-name-operating-systemJPG

Click Next to proceed.

The default memory size for me is 1024 MB. That is not enough. Raise it to 6470 MB, and then click Next to proceed.

13-select-recommended-ramJPG

Make sure “Create a virtual hard disk now” is selected, and click Create.

14-create-virtual-hard-diskJPG

Select “VirtualBox Disk Image (VDI)”, and click Next.

Choose a Fixed size virtual hard disk so that you have better performance, and click next.

15-fixed-sizeJPG

You can stick with the default hard disk space (10GB as of the time of this writing) or go with something like 50 GB. I went with 50 GB. I also prefer to save my hard disk on my D drive (which has more space than my C drive). Then click Create.

16-default-hard-disk-sizeJPG
17-creating-virtual-diskJPG

Double-click on the left panel where it says “Ubuntu 18.04.” A startup window will appear.

18-new-virtual-machineJPG

Click the Folder icon next to Empty and select the Ubuntu image you downloaded earlier in this tutorial. It is a .iso file. You can make sure that your .iso file is somewhere in your C drive (doesn’t have to be on your Desktop). Then click Start to proceed.

19-startup-windowJPG

You might get an error that looks like this.

20-errorJPG

Click Close VM.

Enable Virtualization Technology on Your Computer

The error above arises because virtualization technology is disabled on your computer by default. We need to enable it. Let’s do that now.

Go down to the search area on your computer in the bottom left of your screen, and searched for “Advanced Startup”.

21-searchJPG

Click “Change Advanced Startup Options.”

Click Restart Now.

Click Troubleshoot.

Click Advanced Options.

Click UEFI Firmware Settings.

Click Restart to change the UEFI Firmware Settings.

Click F10 BIOS Setup.

Press the right arrow to go to System Configuration.

Scroll down to Virtualization Technology.

Press Enter to select Enabled.

Press the down arrow and then Enter to select Enabled.

Press F10 to save and exit.

Press Enter on Yes to save the changes.

Your computer will reboot.

Double-click on the VirtualBox icon to start it.

Click on the left panel of the window to start the Ubuntu virtual machine. Or you can just click Start in the toolbar.

22-restart-virtual-boxJPG

You should see the Ubuntu window appear.

Install Ubuntu

Click on “Install Ubuntu” to install Ubuntu.

23-ubuntu-startJPG

Click “Continue” to save the keyboard layout. The default English one is fine.

Keep clicking Continue through all the prompts. The options you want selected as you go through the prompts are the following:

  • Download updates while installing Ubuntu
  • Erase disk and install Ubuntu

You will get to a point where you will need to set your time zone. It will be a big map of the world that should automatically detect your location.

Type in a computer name and pick a username and password. I select the “Log in automatically” option.

When installation is complete, click “Restart Now.”

When you reboot, you will go to the Ubuntu desktop.

25-welcome-to-ubuntuJPG

If at any time you want to exit Ubuntu, go to File → Close. You will be given the option to save your machine state so that you can pick up where you left off the next time you login to Ubuntu.

27-save-machine-stateJPG

Two additional notes….when you power up your VirtualBox, it is a good idea to go to Settings → Display and change your Video Memory to 128 MB. This will give you ample video memory.

32-video-memoryJPG

Also go to Settings → System → Processor, and adjust the number of CPUs to 4.

33-number-of-cpusJPG

If you at any point in the future run out of hard drive space for your virtual machine, you can move it to another drive (e.g. D drive) by following this article at Tech Republic.

Here are the complete settings on my Windows machine.

34-my-settingsJPG

Getting Used to Ubuntu

Similar to the C drive in Windows 10, Linux has a file system (everything branches from the / symbol). You can find it by clicking the icon of the file cabinet on the right side of the desktop. It is the third icon down in the image below.

28-file-cabinetJPG

Click “Other Locations.”

29-other-locationsJPG

Click “Computer.”

You will see all the files. For example, the path to the bin file is /bin 

30-ubuntu-file-managerJPG

You can find popular software applications for download in the Ubuntu Software module. It is the sixth icon in the left-hand panel. 

31-ubuntu-softwareJPG

Linux Terminal Commands With the Ubuntu Command-Line Interface

We could certainly navigate around Ubuntu using the graphical user interface, but we would miss out on being able to run advanced processes for ROS. This is where the Terminal application comes in handy. The Terminal application is the Ubuntu command-line interface and is similar to the Command Prompt on a Windows 10 system. It enables us to use Linux terminal commands.

To open the Terminal, click the bottom left where you see those nine white dots and search for “Terminal” at the top. 

35-nine-white-dotsJPG

Click on Terminal.

36-terminal-searchJPG
37-terminal-windowJPG

If you do a Google search for “common Linux terminal commands,” you should find some nice cheat sheets to use as a reference. Let’s try a few common commands below.

The following command retrieves a list of all the files and folders in the current directory.

ls
38-ls-commandJPG

If you want to change to the desktop, type:

cd Desktop

If you want to get the path to the current directory, type:

pwd

To go up one directory, type:

cd ~

To update the list of packages, type:

sudo apt-get update

The sudo keyword enables you to run a command as an administrator.

At this stage, it would be useful for you to install htop, an interactive system-monitor process-viewer and process-manager. To install it, type the following command:

sudo apt-get install htop

If at any point you want to remove it, you can type the following command:

sudo apt-get remove htop

To run, htop, you type:

htop

You can reboot the system using the following command:

sudo reboot

Finally, to shutdown the system, you type the following command:

sudo poweroff

How to Set Up Anaconda for Windows 10

In this post, I will show you how to set up Anaconda. Anaconda is a free, open-source distribution of Python (and R). The goal of Anaconda is to be a free “one-stop-shop” for all your Python data science and machine learning needs. It contains the key packages you need to build cool machine learning projects.

Requirements

Here are the requirements:

  • Set up Anaconda.
  • Set up Jupyter Notebook.
  • Install important libraries.
  • Learn basic Anaconda commands.

Directions

Install Anaconda

Go to the Anaconda website and click “Download.”

setting-up-anaconda-1

Choose the latest version of Python. In my case, that is Python 3.7. Click “Download” to download the Anaconda installer for your operating system type (i.e. Windows, macOS, or Linux). 

setting-up-anaconda-2

Follow the instructions to install the program:

setting-up-anaconda-3
setting-up-anaconda-4
setting-up-anaconda-5
setting-up-anaconda-6

Verify Anaconda is installed by searching for “Anaconda Navigator” on your computer.

Open Anaconda Navigator.

setting-up-anaconda-7

Follow the instructions here for creating a “Hello World” program. You can use Spyder, Jupyter Notebooks, or the Anaconda Prompt (terminal). If you use Jupyter Notebooks, you will need to open the notebooks in Firefox, Google Chrome or another web browser.

Check to make sure that you have IPython installed. Use the following command (in an Anaconda Prompt window) to check:

where ipython
setting-up-anaconda-8

Make sure that you have pip installed. Pip is the package management system for Python.

where pip
setting-up-anaconda-9

Make sure that you have conda installed. Conda is Anaconda’s package management system.

where conda
setting-up-anaconda-10

Install Some Libraries

Install OpenCV

To install OpenCV, use the following command in the Anaconda Prompt:

pip install opencv-contrib-python

Type the following command to get a list of packages and make sure opencv-contrib-python is installed.

conda list

Install dlib

Install cmake.

pip install cmake

Install the C++ library called dlib

pip install dlib
setting-up-anaconda-11

Type the following command and take a look at the list to see if dlib is successfully installed:

conda list

Install Tesseract

Go to Tesseract at UB Mannheim.

Download the Tesseract for your system.

Set it up by following the prompts.

setting-up-anaconda-12

Once Tesseract OCR is downloaded, find it on your system.

Copy the name of the file it is located in. In my case, that is:

C:\Program Files\Tesseract-OCR

Search for “Environment Variables” on your computer.

setting-up-anaconda-13

Under “System Variables,” click “Path,” and then click Edit.

Add the path: C:\Program Files\Tesseract-OCR

setting-up-anaconda-14

Click OK a few times to close all windows.

Open up the Anaconda Prompt.

Type this command to see if tesseract is installed on your system.

where tesseract

Now, apply the Python binding to the packages using the following commands:

pip install tesseract
pip install pytesseract

Install TensorFlow

Type the following command in the Anaconda Prompt:

pip install tensorflow

Install TensorFlow hub using this command:

pip install tensorflow-hub

Now install tflearn.

pip install tflearn

Now, install the Keras neural network library.

pip install keras

Install the Rest of the Libraries

Type the following commands to install the rest of the libraries:

pip install pillow
pip install SimpleITK

Learn Basic Anaconda Commands

Changing Directories

If you ever want to change directories to the D drive instead of the C drive, open Anaconda Prompt on your computer and type the following commands, in order

D:
cd D:\XXXX\XXXX\XXXX\XXXX

where D:\XXXX\XXXX\XXXX\XXXX is the file path.

Listing Directory Contents

Type the dir command to list the contents of a directory.

dir

Creating a Jupyter Notebook

Open Anaconda Prompt on your computer and type the following command:

jupyter notebook

Converting a Jupyter Notebook into Python Files

If you want to convert a Jupyter Notebook into Python files, change to that directory and type the following command:

jupyter nbconvert --to script *.ipynb

Congratulations if you made it this far! You have all the libraries installed that you need to do fundamental image processing and computer vision work in Python.

Difference Between Recursion and Iteration

If you are learning algorithms or data structures, you might have come across the terms recursion and iteration. Knowing the difference between the two can be confusing, so I will explain both terms using a real-world example.

Eating My Morning Bowl of Oatmeal

eating-oatmeal

Everyday before I go to work, I take a shower, get dressed, and then head over to the kitchen to fix myself a big bowl of oatmeal. Breakfast is my favorite meal of the day and the most important. Many days I feel like skipping breakfast so that I can go on with my day. However, on those rare occasions when I decide to skip breakfast, I have usually regretted it. I spend the morning hungry and cannot fully concentrate on the tasks at hand.

The problem of eating my morning bowl of oatmeal can be implemented both iteratively and recursively. Iteration is the act of repeating a process. So, in the case of oatmeal, an iterative way to solve the problem of eating my bowl of oatmeal would be to do the following:

  1. Take one bite of the oatmeal
  2. Repeat Step 1 until the bowl is empty.

Recursion, on the other hand, is when a method calls itself. It involves breaking a problem into smaller versions of the same problem until you reach a trivial stopping case.

Let’s consider the problem of eating a bowl of oatmeal. I can solve the problem of eating the entire bowl of oatmeal recursively by breaking it into two subproblems as follows:

  • Take one bite of oatmeal
  • Eat the rest of the bowl of oatmeal

Each step of this recursive procedure gets smaller and smaller until all I am left with is one more spoonful of oatmeal. Once I finish that last spoonful, I am done, and I can go on about my day.

Here is how I would implement the iterative and recursive procedures for eating a bowl of oatmeal in Java pseudocode:

Iterative Implementation

public static main void main(String[] args) {

    while (!bowl.empty()) {
        bowl.takeOneBite(); 
        // Keep taking bites over and over 
        // again until the bowl of oatmeal 
        // is empty
    }
}

Recursive Implementation

public static void eatOatmeal(Bowl bowl) {

    if (bowl.empty()) {  
    // This is our stopping case. 
    // We stop taking bites once 
    // the bowl is empty

        return;  
    }
    else {
        bowl.takeOneBite(); 
        eatOatmeal(bowl); // Recursive call
    }
}

What Is a Maximum Spanning Tree?

In this post, I will explain the concept of a maximum spanning tree.

What is a Spanning Tree?

Let’s say we have a graph G with three nodes, A, B, and C. Each node represents an attribute. For example, for a classification problem for breast cancer, A = clump size, B = blood pressure, C = body weight.

Graph G:

maximum-spanning-tree-1

A spanning tree is a subset of the graph G that includes all of the attributes with the minimum number of edges (that would have to be 2 because a tree with just one edge would only connect at most 2 attributes). In the graph above, there are three spanning trees. All spanning trees in this graph G must have the same number of attributes (3 in total) and edges (2 in total).

Spanning Tree 1:

Spanning Tree 2:

Spanning Tree 3:

What is a Maximum Spanning Graph?

OK, so we have our spanning trees. Now, imagine that each edge has a weight. This weight would be some number. Weighted graphs look like this:

The graph above could has three spanning trees, subsets of the graph G that include all of the attributes with the minimum number of edges.

Which one of those spanning graphs is the “maximum spanning graph?”…the one that, when you add up the weights of each edge of the spanning graph, delivers the greatest result. The answer to that is our maximum spanning tree.

Here is the maximum spanning tree:

Since the Attribute Designated as the Root Is Arbitrary, Is It Safe to Assume That This Choice Does Not Affect the Model Effectiveness?

Yes, it is safe to assume that. The graph doesn’t change, and Kruskal’s algorithm, the algorithm for finding the maximum spanning tree in a graph doesn’t care what the root is…it just wants to find the largest edge at each step that doesn’t produce a cycle.

The number of maximum spanning trees in a graph G remains constant. Whether you start at C, B, and E, doesn’t matter. The graph is what it is…unless of course you decide to add a new attribute…but then it would be a different graph with a whole other set of spanning trees.

How to Set Up Visual Studio Community 2019 for Windows 10

In this post, I’ll show you how to set up Visual Studio Community 2019 for Windows 10. This IDE will help us build C++ and Python programs. Our goal is to create a simple “Hello World” program in C++ and in Python.

Requirements

Here are the requirements:

  • Set up Visual Studio Community 2019 for Windows 10.
  • Create a simple “Hello World” program in C++ and in Python

Directions

Installation of Visual Studio Community 2019

Open a web browser.

Type “download visual studio community” into your favorite search engine.

visual-studio-community-setup-1

Click on the first result.

visual-studio-community-setup-2

Click to download Visual Studio Community.

Click the up arrow (^) and then click “Show in folder.”

visual-studio-community-setup-3

Right click on the file and click “Run as administrator.”

Click Yes.

Acknowledge the terms and conditions by clicking “Continue”.

visual-studio-community-setup-4

Wait for the Visual Studio Installer to do its thing.

visual-studio-community-setup-5

Select “Desktop Development with C++”. This is all you need to build C++ programs.

Select “Python development”. This is all you need to build Python programs.

visual-studio-community-setup-6

I also plan to develop for Raspberry Pi and do some data science, so I installed some extra workloads. This is optional if all you want to do is develop programs in C++ and Python:

  • Linux development with C++
  • Data science and analytical applications
visual-studio-community-setup-7

Choose the individual components that you want to install. It is OK to keep the default. Here is what else I selected:

  • Git for Windows
  • GitHub extension for Visual Studio
  • Class Designer (under Code Tools)
visual-studio-community-setup-8

You don’t need any of the .Net stuff.

Click “Install”. Go get a bite to eat or take a break. This will take a while.

visual-studio-community-setup-9

Once the install is complete, reboot your computer.

visual-studio-community-setup-10

Search for the program “Visual Studio 2019” on your computer, and then create a Desktop shortcut for it so that it is easier to find the next time around.

On the sign-in screen, sign in using your Microsoft account (or create one).

visual-studio-community-setup-11

Select “General”.

Creating the “Hello World!” Program in C++

Click “Create a new project”.

visual-studio-community-setup-12

Click “Empty Project” to start a new project.

visual-studio-community-setup-13

Configure your new project.

Right click on the project under the Solution Explorer. Go to Add -> New Item.

visual-studio-community-setup-14

Select C++ File (.cpp), and give the source code a name. Then click “Add”.

visual-studio-community-setup-15

Type in the code for your “Hello World!” program:

// This is a basic C++ program to display "Hello World!" 

// Header file for input output functions 
#include<iostream>  

using namespace std;

// main function: where the execution of program begins 
int main()
{
	// prints Hello World! to the console
	cout << "Hello World!";

	return 0;
}

Click the green button (Local Windows Debugger) to compile and run.

visual-studio-community-setup-16

You can also go to Build -> Build Solution. Then go to Debug -> Start Without Debugging.

That’s it! You should see the Hello World! message pop up.

visual-studio-community-setup-17

Running a Program in a Command Window

Solution Explorer (Right click the project name) -> Open Folder in File Explorer

Open the Debug folder.

visual-studio-community-setup-18

Select the hello_world.exe application.

visual-studio-community-setup-19

Hold down the Shift Key and right click to open a menu

Choose “Copy as path”.

Press Windows + R to open a command prompt window.

Type cmd.exe in the box.

visual-studio-community-setup-20

Click OK.

Right click to paste in the path.

visual-studio-community-setup-21

Press Enter to run.

visual-studio-community-setup-22

Creating the “Hello World!” Program in Python

Click “Create a new project”.

visual-studio-community-setup-23

Under “Language” select Python.

Select “Python Application”.

visual-studio-community-setup-24

Configure your new project. This time I checked “Place solution and project in the same directory”.

Click “Create”.

visual-studio-community-setup-25

Type in the code for your “Hello World!” program:

print("Hello World!")

Click the green button (“Attach”) to run.

That’s it! You should see the message pop up.

visual-studio-community-setup-26

To run via command line, go to the project folder and find the python file (hello_world.py in this case).

visual-studio-community-setup-27

Hold down the Shift Key and right click to open a menu.

Choose “Copy as path”.

Press Windows + R to open a command prompt window.

Right click to paste in the path.

visual-studio-community-setup-28

Press Enter to run. You might have to select the Python application you want to use to execute the file. You’ll only have to do this once.

The message should print to the terminal window. Congratulations! You are now ready to build C++ and Python programs!

visual-studio-community-setup-29

How to Add an External C++ Library to Your Project

Libraries in C++ are collections of code that someone else wrote. They prevent you from having to reinvent the wheel when you need to implement some desired functionality. You can think of libraries as a plugin or add-on that gives you more functionality.

For example, I wanted to write a program that is able to multiply two matrices together. Instead of writing the code from scratch, I searched the web for a linear algebra library in C++ that contained functionality for multiplying matrices. I found the library named “Eigen.” I added the library to my project in the CodeLite IDE, and I was ready to go.

Without further ado, here is the step-by-step process for adding an external C++ library to your project using the CodeLite IDE. Note that this process will be different if you are using another IDE for C++, but the two basic steps are the same:

  1. Add the path for the header files
  2. Add the path for the actual code (i.e. the library)

How to Add an External C++ Library to Your Project

Step 1: Go to the website of the library.
For example, for the linear algebra library, Eigen, you go to this page: Eigen Main Page

Step 2: Download the zip file that contains all the code.

Step 3: Unzip the zip file to your computer.

Step 4: Open CodeLite (i.e. your IDE)

Step 5: Open a new Project

Step 6: Right click on project name and click on Settings

Step 7: Click the Compiler tab and add the Include Paths:
e.g. the folder that contains the folder named ‘Eigen’…C:\XYZ\eigen-eigen-21301928\
This is where the compiler can find the .h (i.e. header) files

Step 8: Click Linker and add the Libraries Search Path
e.g. C:\XYZ\eigen-eigen-21301928\
The path above needs to be the location where the linker can find the libraries (usually suffixed with .a, .dll, .lib, .so)

  • Static Libraries are – XYZ.lib for Windows, UNIX/Linux/Max – libXYZ.a
  • Dynamic Libraries are – XYZ.dll for Windows, Unix/Linux/Mac – libXYZ.so

Step 9: Go to main.cc (i.e. your source code file…could also be main.cpp) and add the preprocesser directives at the top of the source file:

e.g.
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include

using namespace Eigen;

[Your code here]

Step 10: That’s it. You are ready to rock and roll!

How to Learn Git and Github for Windows Users

Most of the tutorials and books on Git and GitHub are overly complicated. The best book that I have found that covers all the fundamentals is A Practical Guide to Git and GitHub for Windows Users: From Beginner to Expert in Easy Step-By-Step Exercises by Roberto Vormittag. It explains everything step-by-step and covers the main use cases. It is easy to follow, and you can get through the entire book in just a day.