2016年5月1日 星期日

Install OpenCV3 and Octave on Ubuntu 14.04 and with Anaconda3

 

Platform

Step 1: platform => Ubuntu 14.04 and the update first!

Step 2:  Install lots of stuffs:   

參考: http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/

Open up a terminal and update the apt-get  package manager followed by upgrading any pre-installed packages:

 Now we need to install our developer tools:

The pkg-config  is likely already installed, but be sure to include it just in case. We’ll be usinggit  to pull down the OpenCV repositories from GitHub. The  cmake  package is used to configure our build.

OpenCV needs to be able to load various image file formats from disk, including JPEG, PNG, TIFF, etc. In order to load these image formats from disk, we’ll need our image I/O packages:


At this point, we have the ability to load a given image off of disk. But how do we display the actual image to our screen? The answer is the GTK development library, which the highgui  module of OpenCV depends on to guild Graphical User Interfaces (GUIs):


We can load images using OpenCV, but what about processing video streams and accessing individual frames? We’ve got that covered here:

注意 libav 取代了 ffmpeg for video decoding.


Install libraries that are used to optimize various routines inside of OpenCV:

以下 python 的 installation 改成 anaconda3. 

 

Step 3:  Install anaconda3  and (not needed anaconda2)

 

Step 4: Git opencv 3.1.0 and opencv-contrib 3.1.0, and cmake opencv

Our environment is now all setup — we can proceed to change to our home directory, pull down OpenCV from GitHub, and checkout the 3.1.0  version:

we also need the opencv_contrib repo as well. Without this repository, we won’t have access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) that were available in the OpenCV 2.4.X version. We’ll also be missing out on some of the newer OpenCV 3.1 features like text detection in natural images:

Again, make sure that you checkout the same version for opencv_contrib  that you did foropencv  above, otherwise you could run into compilation errors.

 

Time to setup the build:  Need to solve the path problem of anaconda3

以下例子 NOT WORKING!!!!

以下例子 OK!!!

可參考 https://scivision.co/anaconda-python-opencv3/

cmake -DBUILD_TIFF=ON 
-DCMAKE_BUILD_TYPE=RELEASE
-DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") ## ?? dangerous -> fix later with /etc/ld.so.conf.d/opencv.conf!!!
-DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules
-DBUILD_opencv_java=OFF 
-DWITH_CUDA=OFF 
-DENABLE_AVX=ON 
-DWITH_OPENGL=ON 
-DWITH_OPENCL=ON 
-DWITH_IPP=ON 
-DWITH_TBB=ON 
-DWITH_EIGEN=ON 
-DWITH_V4L=ON 
-DBUILD_TESTS=OFF 
-DBUILD_PERF_TESTS=OFF 
-DPYTHON3_EXECUTABLE=$(which python3) 
-DPYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") 
-DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

 

cmake ok,  then

$ make -j4

But make -j4 failed!!!!   No hdf5.h!!!

(path problem -DPYTHON3_INCLUDE_PATH=~/anaconda3/include/python3.5m)

 

The problem was that PYTHON3_INCLUDE_DIR was set to ~/anaconda3/includewhile Anaconda's Python.h is actually inside ~/anaconda3/include/python3.5m. Setting PYTHON3_INCLUDE_DIR=anaconda3/include/python3.5m/ fixes this issue as it now sees Python.h

However, this led to another problem where some .h files such as hdf5.h and H5public.h were no longer visible to OpenCV as these were in the main anaconda3/include/ directory, not in the subfolder.

The solution for this was to copy all the .h files from the include/ directory into the include/python3.5m directory (copying the contents of both into another directory and setting that as the path would also work) so that all the .h files are in the same folder visible to the compiler. OpenCV now builds!

Solutions:  copy ~/anaconda3/include/* to ~/anaconda3/include/python3.5m/*  so that  

 

then

$ sudo make install

 

$ sudo ldconfig  (NOT enough)

--> all lib reside in ~/anaconda3/lib   --> but cannot see when do ldconfig -p!!

--> need to fix it!!!! 

add a file called opencv.conf in

/etc/ld.so.conf.d/

There is only one line in content:

/home/alu/anaconda3/lib  (match with the cmake)

注意 ldconfig 是動態 library linker, 可以取代 LD_LIBRARY_PATH (靜態設定).

 

redo 

$sudo ldconfig

and check 

$ldconfig -p | grep opencv  ==> to make sure opencv lib is included!!

或是用

$ldconfig -v | less 來檢查是否有 opencv 的 lib.   

 

 

 

It seems work now!!! with the video!!!

 

How to test

1.  run python ->  

>>> import cv2

>>> cv2.__version__

'3.1.0'

 

2. run ~/opencv/samples/python/demo.py

check 每一個 test case.  特別是第一個和第三個和 video 相關者。

 

 

How to compile the cpp samples code in ~/opencv/samples/cpp?

(1) First need to fix the lib problem;  use pkgconfig to solve the problem

When compile, do 

$ g++ -o npr_demo npr_demo.cpp `pkg-config opencv --cflags --libs`

then

$ ./npr.demo

 

(2) Second encounter ippicv lib missing problem;  direct edit opencv.pc to remove ippicv

or copy the ~/opencv/..../libippicv.a to the ~/anaconda3/lib   ==>  I use this method  

 

目前比較大的問題是 /usr/lib/x86_64-linux-gnu 和 /home/alu/anaconda3/lib 的 lib 衝突。

大部份都相同。一個問題是 sequence 是先呼叫 /home/alu/anaconda3/lib.  因為在

/etc/ld.so.conf.d/opencv 的順序比 /etc/ld.so/conf.d/x86_64-linux-gnu 先。

在執行 emacs 或是 octave 時都會發生 /home/alu/anaconda3/lib 中的 lib 呼叫到找不到的版本。

 

暫時解是 /etc/ld.so.conf.d/opencv --> 改為 zopencv!!!

再重新執行 $sudo ldconfig

最後再 check $sudo ldconfig -v | less 確認順序。 

 

 

2016年4月28日 星期四

Computer Vision OpenCV 筆記

 

Computer vision (CV) 已存在多年。之前主要由 computer graphic researcher 主導。主要以多種不同的算法和 feature extraction (特徵擷取) 為主。

近年由於 machine learning 和 deep learning (深度學習) 快速發展。逐漸變為 learning base 的 computer vision.

例如前端用 feature extraction (SIFT, HOG) + learning classifier (SVM, softmax, adaboost).  或甚至 end-to-end deep learning (DNN, CNN).

 

CV 相關的 specification 其實不多。主要是 OpenCV, 目前已經到 3.1.0 (2016/4), 包含算法, feature extraction, 和 machine learning classifiers.  

底層應該是 C and C++; interface 多了 python, java, matlab, ruby, etc. 

OpenCV(Open Source Computer Vision Library)是一個集合數百種演算法的跨平台電腦視覺庫。只要是針對影像做辨識、操作、分析等,不管是要做人臉辨識、物件辨識、車牌辨識、人形偵測、物件追蹤、影像壓縮、影像加密等各種影像處理,OpenCV確實是一個很好的輔助工具,目前已經可以在Windows、Linux、iOS及Android平台上使用,支援的程式語言有C/C++、Java、Python、Matlab、Ruby及C#等,已經是大部分影像處理使用者必備的涵式庫了,以下會介紹OpenCV的主要功能、使用方式及範例程式碼。

Main

  • core. 基本資料結構的緊湊模組,包含操作影像像素及1.0與2.0版本格式交換
  • imgproc. 影像處理模組,包含線性與非線性的影像濾波器及幾何影像的轉換(影像縮放翻轉、色彩空間轉換和直方圖操作等)
  • imgcodecs. 影像格式編碼、解碼及影像存取
  • videoio. 攝影機操作
  • highgui. 提供簡易的視窗介面及鍵盤與滑鼠操作
  • video. 物件追蹤、背景相減及移動方向預估等
  • calib3d. 多視角幾何演算法,影像校正、3D物件重建及姿態估測等
  • features2d. 特徵擷取及特徵描述
  • objdetect. 物件偵測,如人臉偵測、眼睛偵測、嘴巴偵測、鼻子偵測及身形偵測等
  • ml. 機器學習演算法,如KNN、貝式機率分類器、SVM等
  • flann. 實現FLANN演算法
  • photo. 影像去雜訊、HDR
  • stitching. 影像合成相關技術
  • cudaarithm. 使用CUDA資料結構操作
  • cudabgsegm. 使用CUDA的背景相減MOG
  • cudacodec. 使用CUDA的影片存取讀取
  • cudafeatures2d. 使用CUDA實現部分特徵擷取技術 …
紅色部份是最常用 modules: core, imgproc, highgui.
藍色部份是 GPU 才會用到

Extra

  • aruco. ArUco Marker Detection
  • bgsegm. Improved Background-Foreground Segmentation Methods
  • bioinspired. Biologically inspired vision models and derivated tools
  • ccalib. Custom Calibration Pattern for 3D reconstruction
  • cvv. GUI for Interactive Visual Debugging of Computer Vision Programs
  • datasets. Framework for working with different datasets
  • dnn. Deep Neural Network module
  • dpm. Deformable Part-based Models
  • face. Face Recognition
  • fuzzy. Image processing based on fuzzy mathematics ...

 

 

目前 OpenVX 1.0 的功能,包括了:

 

  • 核心資料結構(Core data structures)
    • 影像以及影像金字塔(Images and Image Pyramids)
    • Processing Graphs, Kernels, Parameters
  • 影像處理(Image Processing)
    • 算數、邏輯、統計運算(Arithmetic, Logical, and statistical operations)
    • 多通道色彩和色彩深度的抽取與轉換(Multichannel Color and BitDepth Extraction and Conversion)
    • 2D Filtering and Morphological operations
    • 影像縮放與變形(Image Resizing and Warping)
  • 核心視覺計算(Core Computer Vision)
    • Pyramid computation
    • Integral Image computation
  • 特徵擷取與追蹤(Feature Extraction and Tracking)
    • Histogram Computation and Equalization
    • Canny Edge Detection
    • Harris and FAST Corner detection
    • Sparse Optical Flow


除了 OpenCV 之外,還有幾個相關的 spec.  一個是 OpenVX, 這是 Khronos (製訂 OpenCL and OpenGL 的 organization) 在 2014/10 發佈 OpenVX 1.0 spec.

也是 focus on computer vision.  不過似乎未包含 deep learning

 

另外還常提到的是 OpenCL.  

OpenCL 是由 Khronos Group 針對異質性計算裝置(heterogeneous device)進行平行化運算所設計的標準 API 以及程式語言。所謂的「異質性計算裝置」,是指在同一個電腦系統中,有兩種以上架構差異很大的計算裝置,例如一般的 CPU 以及顯示晶片,或是像 CELL 的 PPE 以及 SPE。目前,最為常見的就是所謂的 GPGPU 應用,也就是利用一般的顯示晶片(即 GPU)進行 3D 繪圖以外的計算工作。

OpenCL 也就是異質平台,特別是 GPGPU 的 API and computing language.  並非為 computer vision 的 API.  一般是作為底層。 OpenCV 可以架在 OpenCL 之上。 

2016年4月25日 星期一

OpenCV Install

先說結論:

Step 1: Use VMware to install Ubuntu 14.04.4 (TLS) version.

Step 2: Install anaconda2  (OpenCV2.x and 3.1.x 似乎和 python 2.7 相容,如果 python 3.x 會遇到相容問題)

Step 3: Use conda install -c https://conda.binstar.org/menpo opencv=3.1.0 

如果要重新 compile and cmake opencv.  我尚未成功。

--> another problem is ffmpeg doesn't seem to work using conda install

-> so all video using VideoCapture not working!!!

-> it seems that need to include opencv_contribute

 

-------------------------------------------------------

 

1. Need to install anaconda2; reinstall from contiumm

2. Need to run face detection

 

sudo apt-get install libgtk2.0-dev

sudo apt-get install pkg-config

 

More complete installation guide of opencv, please follow:

 

http://ccw1986.blogspot.tw/2013/09/learningopencv.html

OpenCV Tutorial

 

http://ccw1986.blogspot.tw/2016/03/install-python-and-opencv-31-on-ubuntu.html

Installation of OpenCV on Ubuntu TLS14.04

 

It works on c++ version, but failed on the anaconda python.

 

Update latest packages and installed

$ sudo apt-get update
$ sudo apt-get upgrade

apt-get update - 更新最新的套件資訊 apt-get upgrade - 更新套件

安裝基本開發工具

$ sudo apt-get install build-essential cmake pkg-config git
  • build-essential - 基本編譯器
  • git - 分散式的版本控制系統
  • cmake - 跨平台自動化建構系統
  • pkg-config - 查詢套件include與library的位置

安裝影像I/O開發工具,例:JPEG, PNG, TIFF, etc.

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

安裝影音I/O開發工具,讀影影片、接收串流、影音解碼...

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

安裝GTK開發工具,OpenCV highgui module顯示視窗時需要

$ sudo apt-get install libgtk2.0-dev
  • libgtk2.0-dev - (Gimp ToolKit)跨平台的圖形介面開發套件
$ sudo apt-get install libtbb-dev
  • libtbb-dev - (Intel® Threading Building Blocks )平行計算引擎

安裝OpenCV優化的開發工具

$ sudo apt-get install libatlas-base-dev gfortran

以下 install python 部份 ignore.  因為要改用 anaconda2

http://dr-kayai.hatenablog.com/entry/2015/03/30/134856

 

 

So I remove the anaconda2 directory and re-build the opencv!!

 

NOT WORKING!!!!! for python ==> import cv2 has problem.

 

最後找到直接可用 anaconda install opencv3 方法

http://danwin.com/2014/12/compile-opencv-2-4-10-ubuntu-14-04-14-10/

不過以上是 opencv2, 必須修改

 

  1. conda install binstar 
  2. 似乎 binstar 改為 anaconda-client  -> conda install anaconda-client 做一次
  3. conda install -c https://conda.binstar.org/menpo opencv=3.1.0  (可以直接到 url 找最新的 opencv)
It works!!!!
不過 opencv3 和 opencv2 有些地方不同, example face detection
opencv2 --> cv2.cv.CV_HAAR_SCALE_IMAGE
opencv3 --> cv2.CASCADE_SCALE_IMAGE

 

 

 

2016年3月19日 星期六

New Idea From Andrew Ng

 

Overfit is a big problem --> assuming too many parameter space and high variance learning (not high bias)

Solution A:  constraints: e.g. regularization, sparsity (L1-regularization), ???

 

Solution B: Use a lot of data (10x or 100x),  how?  

artificial synthesis of data,  adaboost, random forest ?

 

Are they (more data and regularization) equivalent or behavior similarly?

Raspberry Pi 3 記錄

 

繼上次買 Raspberry Pi 1, 這次又買了 RP 3.  

RP3 最大的好處: (1) 4 核且 1.2GHz, 是 CA53 ARM 64-bit processors; (2) 板上有 WiFi + BT (from Broadcom, too?).  其它和之前的 RP 應差異不大。

 

Step 1:  (MicroSD card)

I bought 32G Kingston microSD card (UHS ..   U3) 號稱 80MB/s write and 90M/s read speed.  不過我用 RP3 測試只有 20MB/s, 應該是 PR3 performance limitation.

Download NOOBS zip file, decompress to microSD card.   用此 boot 自動會 install Raspbian OS (derivative of Linux Debian).

 

Step 2: (installation Raspbian 4.1.18-v7+, based on Debian Jessie version); armv7l 32-bit instruction set!!!  

1. 設定 keyboard layout from UK to US.

2. 設定 WiFi AP

3. Update Raspbain to the latest update

$ sudo apt-get update  (update apt-get inself)

$ sudo apt-get upgrade  (upgrade all packages)

 

Step 3: (Install 中文顯示和輸入)

請參考葉難的 blog.

先更新一下套件清單。
$ sudo apt-get update

安裝中文字型,文泉驛微米黑、文泉驛正黑、文泉驛點陣宋體。
$ sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

然後將locale設定成繁體中文。
$ sudo dpkg-reconfigure locales

繁體中文的部份,我選了三個zh_TW BIG5、zh_TW.EUC_TW EUC-TW、zh_TW.UTF-8 UTF-8。

 

然後將zh_TW.UTF-8設定為預設的locale。

然後要安裝中文輸入法。
$ sudo apt-get install scim scim-tables-zh scim-chewing

其中scim-chewing是注音的輸入法,若是大陸用的拼音輸入法,請安裝scim-pinyin。

重開機後,以Ctrl-Space即可切換輸入法,輸入中文。

 

Step 3: (Remote Desktop)   Raspberry Pi 的 default username: pi   passwd: raspberry --> change to aluxxx4

可以用 VNC or microsoft remote desktop.  因為 mac 也可以在 app store download microsoft remote desktop.  因此先 install xrdp.

$ sudo apt-get install xrdp --> mac 端必須 download Microsoft remote desktop.  見 xxx blog.

--> mac 可以直接用 microsoft remote desktop, 不用 download any software!!

不過連 ping 都不 work, 上網卻 ok.   $ ping www.google.com --> icml open socket: operation not permitted

-->  $ sudo chmod u+s /bin/ping  --> ping ok and ssh ok and remote desktop ok!!!

ip addr:  192.168.1.245

 

Install VNC server 就先 delay. 

 

Install miniconda python (2016/?)

不知何時 install miniconda python.

 

Install Camera Module (2016/04/16)

主要想做一些 computer vision 有關的應用,從淘寶買了 Element 14 官方版的 camera module (RMB$95 + RMB$20 shipping).

Install camera module 可以參考葉難的 article.

硬體裝好後,軟體的設定及測試如下。 

$ sudo apt-get update 
$ sudo apt-get upgrade
然後執行raspi-config:
$ sudo raspi-config

進入裡頭的「camera」項目,啟用相機模組,再重開機。
 

接下來,便能使用一些準備好的程式與指令拍攝靜態照片與錄製動態影片。
以指令raspistill可拍照,如下
$ raspistill -o image.jpg -t 1000

-o指定輸出檔名,-t指定預覽時間(單位為千分之一秒),你會在螢幕上看到預覽畫面。如果你發現相片左右或上下顛倒,可加入參數-hf(horizontal flip)與-vf(vertical flip)調整。

這是我的手與我房間的天花板牆壁。
 


另外可利用指令raspivid進行錄影,如下:

$ raspivid  -o video.h264 -t 20000

-o指定輸出檔名,為H.264格式,-t指定錄製時間(單位為千分之一秒)。

可以用 omxplayer video.h264 來顯示 video. 

omxplayer 會利用 BRCM SoC 內的 BRCM GPU to play H.264 影片。

如果用 VLC player, 軟解無法正確顯示 video!!!

可利用ffmpeg轉成其他格式,例如:

$ ffmpeg -r 30 -i video.h264 -vcodec copy outputfile.mkv

或是

$ avconv -r 30 -i video.h264  outputfile.avi
 

 

Install Bluetooth (2016/04/16)

Follow the article in element 14.

sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade

sudo dpi-update --->  update the firmware

 

>> then reboot 

sudo apt-get install pi-bluetooth >> show it's already installed in the new firmware

sudo apt-get install blueman

>> then reboot and the bluetooth icon shows

 

>> then reboot and the bluetooth icon disappear!!

 

 

Install Camera Software Codec and OpenCV

$ sudo apt-get install -y picamera

$ sudo apt-get install -y vlc

$ sudo apt-get install -y gpac mplayer

$ sudo apt-get install -y mencoder  (# obsolete!!)

$ sudo apt-get install -y python-opencv

 

 

2016年3月16日 星期三

Jupyter Notebook with Octave Kernel

在學 machine learning 時常會用到兩種 languages.  一類是 matlab or octave (e.g. Andrew Ng).

另一類是 python (e.g. sckit-learn, etc., 林軒田; UC Washington ML classes).   少數用 R and maybe other languages.

 

Matlab/Octave 的好處是簡單清楚。處理 vector or matrix or plot 乾淨利落。缺點是 large scale ML 的速度太慢。另外 matlab/octave 沒有很好的 notebook UI.

Python 則相反。即使在用 ipython 的 vector, matrix, plot, 仍然處處有 python 的影子。但好處是速度快, scalable.  最重要的是 ipython notebook 是非常簡單好用的 math or engineering notebook UI.

是否能結合兩者?  Yes, Jupyter Noteook.   Jupyter notebook 是 ipython notebook 的下一代。把 UI 和 kernel 分開。 UI 就是 ipython notebook interface. 

Kernel 則可以是 python, octave, matlab, R, etc.

 

為了要 install Jupyster, 首先 install python. 

 

Step 1: Use Vmware to install Ubuntu 14.04.4 (TLS) version.  

Detailed refer to previous article.  

為了方便再 install Dropbox (ubuntu version) for .bashrc, and install kompare, kdiff3, emacs

Step 2: Install Anaconda python (3).   

-> Recommend to use Anaconda python 3.5 from the Jupyter website.

Download PYTHON 3.5 64-bit version (Anaconda3-2.5.0-Linux-x86_64.sh)

=>  bash ...

 

(1) Do "conda install jupyter  -> update jupyter related files

(2) Do "pip install bash_kernel"  -> No need for Octave kernel

 

Step 3: install octave:   sudo apt-get install octave

(3) Do "pip install octave_kernel" 

(4) Do "python -m octave_kernel.install"

 

--> Done.

 

You can either do on the web:

jupyter notebook -> choose new octave notebook

or 

jupyter qtconsole --kernel octave

jupyter console --kernel octave

 

It works now!

2016年3月15日 星期二

Anaconda Python

一般在做用 python 時,最常用到的三個 packages.

1. pip:  取代 easy_install.  是 python 最常用的 package manager

2. virtualenv: environment manager, 主要是自由切換 python2.x and python3.x.  不是很好用,也不常用。不過為了相容性,還是會 install.

3. Numerical packages (numpy, scipy, matplotlib, etc.), 或是用 Enthough, 或是 python(x,y).  

 

一個好消息是只要 install anaconda python, 可以集三者為一。不用再 install above!

 

TaskConda package and environment manager commandPip package manager commandVirtualenv environment manager command
Install a package conda install $PACKAGE_NAME pip install $PACKAGE_NAME X
Update a package conda update --name$ENVIRONMENT_NAME $PACKAGE_NAME pip install --upgrade $PACKAGE_NAME X
Update package manager conda update conda Linux/OSX: pip install -U pip Win: python -mpip install -U pip X
Uninstall a package conda remove --name$ENVIRONMENT_NAME $PACKAGE_NAME pip uninstall $PACKAGE_NAME X
Create an environment conda create --name$ENVIRONMENT_NAME python X cd $ENV_BASE_DIR; virtualenv$ENVIRONMENT_NAME
Activate an environment source activate $ENVIRONMENT_NAME X source$ENV_BASE_DIR/$ENVIRONMENT_NAME/bin/activate
Deactivate an environment source deactivate X deactivate
Search available packages conda search $SEARCH_TERM pip search $SEARCH_TERM X
Install package from specific source conda install --channel $URL$PACKAGE_NAME pip install --index-url $URL $PACKAGE_NAME X
List installed packages conda list --name $ENVIRONMENT_NAME pip list X
Create requirements file conda list --export pip freeze X
List all environments conda info --envs X Install virtualenv wrapper, then lsvirtualenv
Install other package manager conda install pip pip install conda X
Install Python conda install python=x.x X X
Update Python conda update python * X X
  • conda update python updates to the most recent in the series, so Python 2 to latest 2.x, Python 3 to latest 3.x, and so on.

追蹤者