Friday, December 9, 2016

Java - Coding - Java 8 new features - Currying with Java 8

Description
This was my testing sample to understand how currying can be done in Java 8. I used both lambda and function reference to demonstrate currying behavior which would help great deal to understand behind the scene caricatures. 

Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.function.*;
import static java.lang.System.out;

public class CurryApplication {
 public static void main(String[] args) {
  IntBinaryOperator simpleAddWithLambda = (a, b) -> a + b;
  out.println("Simple Add (2params)= " + simpleAddWithLambda.applyAsInt(10, 20));

  IntFunction<IntBinaryOperator> simpleMultipleWithFunctionalReference = new IntFunction<IntBinaryOperator>() {
   @Override
   public IntBinaryOperator apply(int value) {
    return new IntBinaryOperator() {
     @Override
     public int applyAsInt(int left, int right) {
      return value * left * right;
     }
    };
    
   }
  };
  out.println("Simple Multiple (3 params)= " + simpleMultipleWithFunctionalReference.apply(10).applyAsInt(20, 30));

  IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b;
  out.println("Curried Add (2 params)= " + curriedAdd.apply(4).applyAsInt(5));
  
  IntFunction<IntFunction<IntUnaryOperator>> curriedMultiple = a -> b -> c -> c*a*b;
  out.println("Curried multiple (3 params)= "+curriedMultiple.apply(3).apply(4).applyAsInt(5));
 }
}

Explanation:
Functional reference implementation has to have (n-1) level function interface reference in the type declaration - IntFunction>. Here IntFunction is Java 8's feature where provided type is the type of the result of the function. Curried invocation of multiple's implementation has combination of apply and appyAsInt is used in curried fashion. 

Resources:
- http://stackoverflow.com/questions/6134278/does-java-support-currying
- https://dzone.com/articles/whats-wrong-java-8-currying-vs

Monday, November 14, 2016

Raspberry Pi - OpenCV - Setup

Description:
This post is to cover OpenCV installation in raspberry pi and list down the steps in concise fashion and get things up and running so that anyone can focus on the project has in mind. Majority of the contents can from the links and I found those are workable and tested at my end. Check the Notes sections for any problem faced or explanation on any particular step.

Pre-requisites and version of components:
- Raspberry Pi 3 
- Operating System raspbian Jessie
- OpenCV 3.1.0
- Python 3 (3.4.2)


Instruction:
StepsDescription
Install dependencies
sudo apt-get install build-essential cmake pkg-configCMake to configure OpenCV build process
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-devimage I/O packages to load image files
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-devvideo I/O packages to load video from files and streams
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk2.0-devSub module highgui to display image on screen
sudo apt-get install libatlas-base-dev gfortranlibrary to optimize OpenCV
sudo apt-get install python3-devPython 3 header file to compile OpenCV
Download OpenCV 
> cd ~
> wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
> unzip opencv.zip
OpenCV source code
> wget -O opencv_contrib.zip  https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip

unzip opencv_contrib.zip
OpenCV contribute source code
Setup Python3
> wget https://bootstrap.pypa.io/get-pip.py
> sudo python get-pip.py
install python package manager (pip)
sudo pip install virtualenv virtualenvwrapperinstall virtualenv and virtualwrapper to keep the python dependencies separate
sudo rm -rf ~/.cache/pipclear pip cache 
WORKON_HOME=$HOME/.virtualenvs 
source /usr/local/bin/virtualenvwrapper.sh
write these two lines in ~/.profile 
source ~/.profilereload profile changes
mkvirtualenv cv -p python3make python virtual environment
> source ~/.profile
> workon cv
all works will be done in cv python virtual environment
pip install numpyPython package for numeric processing
Install OpenCV
> cd ~/opencv-3.1.0/
> mkdir build 
> cd build 
> cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib
3.1.0/modules \ -D BUILD_EXAMPLES=ON ..
Cell 3:2
make -j4compile OpenCV on four cores
> sudo make install
> sudo ldconfig
install OpenCV
> cd /usr/local/lib/python3.4/site-packages/
> sudo mv cv2.cpython-34m.so cv2.so
rename cv file to cv2
> cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
> ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so
create soft link to cv2.so
Test OpenCV installation 
> source ~/.profile
> workon cv
> python
>>> import cv2
>>> cv2.__version__
test OpenCV version (3.1.0)
Clean Up 
> rm -rf opencv-3.1.0 opencv_contrib-3.1.0test OpenCV version (3.1.0)


Notes:
1.  If NOOBS is used, the SD card is already expanded. In case SD card needs to be expanded, follow below steps - 
- Type command 'sudo raspi-config' in terminal
- Select option '1 - Expand Filesystem'
- Restart the system using 'sudo reboot' in terminal

2. OpenCV and OpenCV contribute version has to be same


Resources:
1. http://www.linuxcircle.com/2015/05/18/open-computer-vision-opencv3-with-python-3-on-raspberry-pi-2/
2. http://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/
3.