Tutorial on setup OpenCV on iOS

This tutorial is to setup OpenCV on iOS. To include opencv2.framework to iOS8 Objective C Project.

By following this tutorial: http://docs.opencv.org/doc/tutorials/ios/hello/hello.html#opencvioshelloworld

1) to download prebuilt opencv2.framework, the fine working version is 2.4.9.

2) to create new XCode Project, target iOS8.0 if you are under XCode 6.0.1 as this version not yet support iOS8.1

3) link opencv2.framework to XCode Project. Go to project build phase, and link following frameworks:

Screen Shot 2015-01-20 at 12.42.28 am

4) Create <NameOfProject>-Prefix.pch, with following snippets of code add between ifdef / endif block.

#ifdef __cplusplus
#import <opencv2/opencv.hpp>

5) in ViewController.h, add the following to header
#import <opencv2/highgui/cap_ios.h>
#include <opencv2/opencv.hpp>

Note, the most important line is #include <opencv2/opencv.hpp>, this is important to make the stuffs works. I spent few hours and found this solve my problems, which unable to recognize cvtColor function, OK, will introduce in next steps.

6) To do some testing, by adding a video camera, reading 30 frames per second, process the images by inverting the color…

7) Rename ViewController.m to ViewController.mm as we are going to put C PlusPlus Code. Extension mm allows coexist of Objective C and C PlusPlus.

8) In ViewController.h, add following line
@property (nonatomic, retain) CvVideoCamera* videoCamera;

9) In ViewController.mm, we initialize videoCamera and starts it.

self.videoCamera = [[CvVideoCamera alloc] initWithParentView:_ImageView];
self.videoCamera.delegate = self;
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
self.videoCamera.defaultFPS = 30;
[self.videoCamera start];

10) In ViewController.h, add CvVideoCameraDelegate

Like this

@interface ViewController : UIViewController

11) In ViewController.mm, add following snippets of code before @end.

#pragma mark – Protocol CvVideoCameraDelegate

#ifdef __cplusplus
– (void)processImage:(Mat&)image;
// Do some OpenCV stuff with the image
Mat image_copy;
cvtColor(image, image_copy, CV_BGRA2BGR);
bitwise_not(image_copy, image_copy);
cvtColor(image_copy, image, CV_BGR2BGRA);

12) Run the project. Cheers.

Photo 20-1-15 12 51 57 am

Install CMake on Mac

CMake, the cross-platform, open-source build system. CMake is a family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice.

To install CMake on Mac, you can download the precompiled binaries here. Under platforms Mac OSX 10.6 or later, I chose cmake-3.1.0-Darwin64.dmg at the time of writing.

Once downloaded, run open cmake-3.1.0-Darvin64.dmg on terminal.

A finder view shown. Drag cmake-3.1.0-Darvin64.dmg to Applications shortcut, it will copy the binary to Applications folder.

Open the applications by sudo.

Do sudo /Applications/CMake.app/Contents/MacOS/CMake

Click on menu Tools, Install for Command line use.

You shall see the result, then you are done.

Screen Shot 2015-01-17 at 7.25.34 pm

Freeswitch Outbounds call provider settings

FreeSWITCH is a scalable open source cross-platform telephony platform designed to route and interconnect popular communication protocols using audio, video, text or any other form of media.

To setup Freeswitch Outbounds call to landline or cell phone, must go through a PSTN or SIP gateway. Didlogic provides DID and SIP gateway.

This tutorial is going to configure SIP gateway on Didlogic, and configure the necessary settings in Freeswitch, in order to make outbounds call to landline or cellphone.

The step 1. Register an account on Didlogic and navigate to SIP Tab (https://didlogic.com/sipaccounts).

Create a SIP Account you will get SIP Username, Password, and you may set a caller ID from a list of DID you registered.

DIDLogic SIP Profiles


The step 2. You may navigate to Didlogic ‘s provided Freeswitch Configuration page (https://didlogic.com/support/setup-guides/freeswitch-config).

What we want is Freeswitch General Config

<gateway name="didlogic">
<param name="username" value="12354"/>
<param name="realm" value="sip.didlogic.net"/>
<param name="password" value="your_SIP_password"/>
<param name="register" value="true"/>
<param name="context" value="public"/>

Replace it with your SIP account username, and password created in Didlogic SIP Accounts Page.

The step 3. Go to your Freeswitch installation directory, for example, installed in a Linux box the location will be: /usr/local/freeswitch/conf/sip_profiles/external

Vi didlogic.xml, and paste the contents above, and :wq! to save the file.

If your Freeswitch is open and running, go to /usr/local/freeswitch/bin/fs_cli, then type reloadxml, it will reload the xml configuration.

Now, you can make a test call.

The step 4. You may go to /usr/local/freeswitch/bin/fs_cli, type:

originate sofia/gateway/didlogic/65xxxxxxxx ''

Then your phone will get the call. You will see the caller id is the same you defined at Didlogic SIP Profiles page.

For advanced Caller ID settings in Freeswitch, I not tried, you need have a verified business custoemr in Didlogic, then you can follow this tutorial https://didlogic.com/support/setup-guides/freeswitch-own-cli, then you may be able to configure your custom CLI.

Linode Account Balance Enquiry API

Linode is a cloud hosting platform provides varieties of Linux based OS (ubuntu, cent-os, and more). It does provide an API to dynamically provision Linode cloud, like add new node, upgrade/downgrade plan, and account API. It does provide Prepaid service by credit card. In case you purchase $20 plan, you can pay in advance the amount stored in your account, it will be deducted every first week of month.

I owns a Linode. Normally I paid one or two months fee in advance. I have to create my own reminder or randomly remember, Oh, just need to pay Linode again. I had to log in to Linode Member Dashboard to check my account balance every time.

After 9 months of Linode experience, I began finding its API and found this API very useful, its Account Balance Enquiry API.

This simple API allows me to query my current account balance, and then compare with the threshold I define, if it is lower than the threshold, then I can receive iPhone Push Notification.

I set a Cron Job to execute the following PHP script which then send the notification. The details of setting up Push Notification for iPhone is not in this topic.

< ?php

$threshold = 20;

$api_key = '<your api key here>';
$result = json_decode(file_get_contents('https://api.linode.com/?api_key='.$api_key.'&api_action=account.info'), TRUE);

if($result['ACTION'] == 'account.info') {
	$balance = floatval($result['DATA']['BALANCE']);
	$balance = abs($balance);
	$message = 'Linode Account Balance is '.$balance;
	if($balance < $threshold) { 		$message .= ', lower than threshold '. $threshold; 	} 	sendAPN($message); } echo '1'; ?>

Generating Wave Files from Mac OS X Say Commands

Mac OS X provides say command in its Terminal Utility to perform Text-To-Speech.

Just type: say “Hello World”, it will speak out.

To output this sound into a Wave file, you can provide file name, it will infer the file extension on file name, and you provide the data format, just type:

say -o hi.wav –data-format=LEF32@8000 “Hello World”


Inversion of Control (IoC)

Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.

Saw IoC many times in many technical articles especially with the topics of Object-oriented design or software engineering development. However, I have no fully understand what IoC is talking about at the first pace. Therefore, I had carried out a research about this.

To understand this, let take a scenario.

Consider we have a framework, and application that runs on the framework.

In traditional aspects, application calls required libraries or API to carry out their tasks. The central control is held by central piece of code in application. In Inversion of Control, framework  actively calls application to carry out tasks. It often using inheritance to realize IoC. Parent class actively calls the method from child class. Framework lets relevant parent classes work together to define the process of the tasks. At the meantime, parent class leave interface, or in Java Term, abstract method, to be implemented by child classes.

The advantage of Inversion of Control is that there is decoupling of the execution of a certain task from implementation. Secondly, it enables less hassles in system replacement.

Further reading: