Tuesday, November 08, 2011

[Py] The Python Challenge -- Level 06

Here is a note of my solution to solve Level 06 of The Python Challenge.

The main library used is the zipfile module.

Monday, October 31, 2011

[Py] The Python Challenge -- Level 05

Here is a note of my solution to solve Level 05 of The Python Challenge.

The main library used is the pickle.

Sunday, October 30, 2011

[Py] The Python Challenge -- Level 04

Here is a note of my solution to solve Level 04 of The Python Challenge.

The main library used is the urllib.parse to parse the url and the urllib.request for retrieving the web page source.

Thursday, October 27, 2011

[Py] The Python Challenge -- Level 03

Here is a note of my solution to solve Level 03 of The Python Challenge.

The main library used is the urllib.request for retrieving the web page source and the re for regular expression operations.

[Py] The Python Challenge -- Level 02

Here is a note of my solution to solve Level 02 of The Python Challenge.

The main library used is the urllib.request. In Python 2.x, it is just urllib.

Wednesday, October 26, 2011

[Py] The Python Challenge -- Level 01

Here is a note of my solution to solve Level 01 of The Python Challenge.

Used functions: ord() and chr().

Thursday, October 20, 2011

pss: searching your source code in command line

When I was working on my Ubuntu with Vim to write or to view source code, I had no simple way to find certain functions scattering in the files by their name or key words.

Now I have one useful tool which is named pss written by Eli Bendersky. The tool is written in Python and that is attractive to me, because I can read the source code and maybe learn something from it. :-)

Download: https://bitbucket.org/eliben/pss/downloads

Okular: Annotations are stored in local directory

This is just a quick note.

When using Okular to view the PDF files, you can add various annotations in the documents. These annotations, however, are not embedded in the PDF files. Instead, they are stored in your local directory. In my Ubuntu PC, the path is

~/.kde/share/apps/okular/docdata

The annotations are stored in individual XML files for the corresponding annotated PDF files.


Thursday, September 15, 2011

Using Boost accumulators to calculate the variance

Boost seems a promising set of C++ library, but I found it's not easy for me to figure out its usage. After serveral trials, I finally got the variance calculated.

Here is the testing code I used (in VS2005):

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/variance.hpp>

int main(){
    using namespace boost::accumulators;
    accumulator_set< double, stats<tag::variance> > acc_variance;

    for (int i = 0; i < 10; i++){
        std::cout << i << ", ";
        acc_variance(i);
    }

    std::cout << std::endl << "Variance = " 
        << variance(acc_variance) << std::endl;

    return 0;
}

The output is:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
Variance = 8.25

When I tried to varify the result, I use MS Excel but got unexpected result. The variances given by Boost and Excel are different!

Did I make any mistakes? Oh, I didn't realise the variance calculated by Boost function was the population variance, and the VAR function of Excel gave the sample variance. To have population variance in Excel, you need VARP, not VAR.


---
Ref: Variance calcs in the stats library and in Excel

Using Git as a working logger

I am working on a project, which is a experimental program coded and maintained by myself.

It is redundant and seems stupid to copy the whole working folder whenever I want to change the code to another version. Especially when the folder contains not only source code but some other documents or videos of testing results.

``I need a version control tool.'' The idea came to my mind.

``But I've never used or been familiar with any.'' The truth is cruel.

Then I thought of Git, a term which I might saw several times when surfing webpages.

Because the project I am working on is written and compiled with Visual Studio 2005, I started to search how to use Git with VS2005 and found Git Extensions.

Wednesday, August 31, 2011

VLC outputs black screen while the audio works fine

My VLC video player gives strange behaviour these days. When playing video, there is only sound with black screen. I once though the video files were broken, but they can be viewed by using QuickTime. So, the problem must be in VLC.

I searched on the Web and didn't find exact issue I encountered. Then I found this post, but the solution didn't work for me and didn't make sense to me. However, the post remind me about preference settings, and I thought of there was a post I glanced mentioned about video card. Oh! I've changed my video card several weeks ago. It might be the cause! I looked into the ``Video Settings'' of preference menu, and unchecked the item named ``Accelerated video output (Overlay)''. It works! :-)


Monday, July 25, 2011

[Py] Example code of FAST corner detection

Edward Rosten developed the FAST corner detection algorithm and gives several versions of source code. Python code is one of them. I downloaded the Python source code but found there was no example to illustrate the basic usage. Fortunately, the Matlab code has one example m-file. Based on the m-file, I tried to write a Python version and it works.

Tuesday, May 31, 2011

Tile Figures with Montage (ImageMagick)

Suppose you have several figures for articles ,papers, blogs, or anything else. You want to combine these figures into a bigger frame, and they have gaps between each other. How? I had used Inkscape to import the figures, arrange them, and export the final result. No, don't do that again. It is not necessary to launch your image editors with GUI to accomplish this kind of work. I found the most convenient  way is to use some commands offered by the magic ImageMagick.

Good examples have been given in ``ImageMagick v6 Examples -- Montage, Arrays of Images,'' and the following is my own test with some notes.

Sunday, May 22, 2011

Tetris in Command Line

Ha ha! That's really funny. I never think about even sed can be used to write games such as Tetris!

I just wondered how to play Tetris in command line. Googled it and found several results with source code. There are three of them I've played:
  1. Tetris for the command line with Python Curses (in Python)
  2. Play Tetris at the command line (in sed)
  3. tt: Tetris for Terminals (in C)
To compile tt, you may need to install ncurses, or you will get the following errors:
utils.c:32:20: error: curses.h: No such file or directory
utils.c:33:18: error: term.h: No such file or directory

In Ubuntu, just install the ncurses-dev.

Thursday, May 05, 2011

[talk] Thanks to You

I attended a class about how to write English emails yesterday. At the end of the class, the lecturer asked us to write an email for exercise. Below is what I wrote. A personal thank to you who have contributed to the open source community.

Dear Mr. and Ms. Hackers,

I am writing to thank you for your great and amazing works
to the open source community.

With so many brilliant tools you've created and kindly released,
many end-users, including me, are allowed to do more useful
things with free [of] charge and, more important[ly], with the freedom
to choose software.

Furthermore, I've learnt about many interesting and useful skills from
the source code released by you.
That's great and I hope I could be capable of contributing back to the
community in the future.
Thank you all for your great works.

Sincerely yours,


THK

Thursday, April 28, 2011

Great tool for LaTeX users working with MS Word -- Bibtex4Word

I was writing conference papers which have only templates in MS Word, and was wondering how to cite the references and to produce the bibliography in Word as easy as that in LaTeX. Then I found Bibtex4Word.

Most MS Word users might type the bibliography list manually, and it is really a tedious work. Inconsistent formatting style might be a main problem of manually typing the bibliography. The format should be done automatically, and should be changed easily to other styles. Just all the natural things in LaTeX.

Bibtex4Word can do the work pretty nice. I have installed and tested with it. Other programs needed are:
  • JabRef: to produce and to manage the bib file of your references
  • MikeTeX: to provide BibTeX for Bibtex4Word so that it can output formatted bibliography list
No need to worry about the formatting is really great. :-)

Wednesday, April 20, 2011

[talk] Talk to myself...

  • Work hard when you are at work.
  • Don't promise too much, but once promised, don't think too much. Just do it.
  • No matter what is in front of you, calm down.
  • The only way to solve the problem is to solve it.
  • A problem is not only a problem, it's also a challenge, and could be a chance, an opportunity.
  • Knowledge, skill, and working hard.

Tuesday, March 08, 2011

[Py] Use ``import cv'' in Ubuntu

At the beginning of my OpenCV learning with Python, I noticed one thing which confused me but I didn't give it further attentions. Some examples import the OpenCV modules by using ``import cv'' and some use ``import opencv.'' The first one has never worked to me, so I've adopted ``import opencv'' until today.

The problems were not only about the import approach. Although I found I can import opencv.cv module, which looked similar to the so-called cv module, there had been other problems about the function names. I noticed that many functions given by the opencv.cv module have the ``cv'' prefix, while the same functions given directly by the cv module don't. For example, opencv.cv offers open.cv.cvSomeFunction() while in cv it is cv.SomeFunction(). Based on the observation, I suspected that my installation of OpenCV an/or Python bindings are not totally right.

Thursday, March 03, 2011

Trials of pycam example code (2)

I've downloaded pycam and tested some of the sample codes. There were some errors when I tried to run the examples, such as DrawAHat.py and eyeLocator.py [1]. The error messages were as follows.

Monday, February 28, 2011

[Py] Use cProfile to check the run time performance of a program

When I was trying to figure out the usage about get_fps of pygame.time.Clock, I read about the thread:

And I noticed that they were talking about ``cProfile,'' so I went to the document about it:

[Py] Using get_fps function of pygame

There are modules of pygame which provide time related functions. One of them is the get_fps which can be used to know the fps of executing program.

Using rst files -- the easy way

When viewing documents of mahatos, there are text files with rst as their extensions. They reminded me about the reStructureText which I met but got confused and gave it up. This time I tried to figure out how to use it and how to convert it into other formats, for example, the HTML. The following steps and links are my quick notes, just for you who also want to try the rst files very quickly on Ubuntu.

  1. Assume that you have Python on your system already.
  2. $ sudo apt-get install python-docutils (this is the module that gives the utilities of the conversion between rst and other formats)
  3. Download the source package of Docutils
  4. Untar the downloaded package and go to the tools directory, find the rst2html.py, and copy it to a folder contained the cheatsheet.txt (Please download it. It is a text file formatted in rst file, and we will use it for the following test.)
  5. Go to the folder contained the cheatsheet.txt file
  6. $ python rst2html.py cheatsheet.txt cheatsheet.html
  7. You now have the cheatsheet.txt converted to cheatsheet.html, and you can view the cheatsheet.html in your browser
To be brief, use the the tools given by the source package to convert the rst text files into other formats.

Before jump into installing the Dcoutils on your system, you may want to try and see the rendered result online: reST to HTML conversion. Just copy the content of cheatsheet.txt and paste it into the online converter, and click the Render button to view the result. That's pretty cool. :-)

    [Py] SURF tests with mahotas.surf

    I installed mahotas in my Python for the purpose of trying the SURF. There is a simple example on the mahotas document page, but some minor modifications are needed. Here is the modified example code:

    Sunday, February 27, 2011

    Installing Mahotas (image processing in Python)

    I've just learned about SIFT and SURF, and am trying to find some example to learn more.

    For SIFT, it is a patented methodology and seems not possible to be used as the open sourced libraries. For SURF, which has been claimed more robust and faster then SIFT, there is OpenSURF written in C++.

    Based on the information given on Wikipedia, I went for mahotas, which is for image processing in Python, and that's what I need.

    Saturday, February 26, 2011

    [Py] Python+OpenCV+pygame for capturing webcam frames on Ubuntu (10.04)

    Several weeks ago, I tried to find some sample code which can get the frames of the webcam on Ubuntu. Some examples were written in C/C++, some were in Python, and them are all based on OpenCV. I tested them and got some failed. Unfortunately, I have neither enough experience nor enough time to figure out where the problems are, so I started to search some more examples again.

    The first light came from Joseph Perla's blog. He gave an example which worked fine on my Ubuntu 10.04. I post the example with minor modifications (using ImageOps.mirror to flip the image horizontally) as the follows.

    Friday, February 25, 2011

    Stereo vision -- source code testing


    C. Zitnick and T. Kanade proposed an algorithm [1] to compute the depth (or disparity) maps for stereo version. The source code was released for non-commercial usage, and there is a modified version by K. Sokolova and B. Shilo. I suggest you go for the modified version which gives some practical and straightforward descriptions and examples. Also, the modified version offers a complete set to be compiled and executed. However, the source code needs some more minor modifications to be compiled using g++ in Ubuntu 10.04.

    Friday, February 18, 2011

    Bring good stuffs of Linux into Windows

    I love to work with Linux (Ubuntu), especially with its useful commands and tools when I need to write programs or analyse some kind of data. Sometimes, however, I have to cooperate with my colleagues, who are working with Windows and Visual Studio. Maybe I can do the coding works in Ubuntu, but I am not so good to solve all the problems related to hardware and drivers. We are always using cameras to capture images and process the streams in real-time. There are problems for me to run all the programs written by my colleagues in Ubuntu, and unfortunately I have no enough time as well as knowledge to tackle all the problems. Therefore, I have to go back to Windows for some program developments.

    But, the command line environment in Windows is not so convenient as in Ubuntu. Lacking the useful commands and tools might be the most inconvenient. Cygwin is one way to run Linux commands in Windows, but it can only work within the Path in which the Cygwin is installed. Luckily, we have GunWin which provides many of the useful tools for Windows. Just click the ``Setup'' links on the page of  packages, then you will get the tools installed and you can use them directly in the Windows cmd shell.

    For further introduction, please go for the following link:

    Moved from Firefox to Chromium

    I have moved from Firefox to Chromium for several weeks. Firefox is excellent in many aspects, especially its abundant and wonderful add-ons. However, in my own experience, it is not really fast. After some trials and findings, I decided to use Chromium as my default browser.

    I would like to share some experiences here. Some are the excellent features of Chromium, and of course some are not so good.

    Thursday, February 17, 2011

    codepad -- an interesting web service for code sharing

    Check the codepad out. I had no idea about pastebin until meet the codepad website. It's really useful when you want to share some code and output with someone via email, MSN, or other internet media.

    Here is my simple trial on the codepad: http://codepad.org/Oja9hsaI

    Saturday, February 05, 2011

    VirtuaWin -- Virtual desktop manager for Windows

    If you are used to working with virtual desktops or workspaces in Linux, you might feel uncomfortable when working under the Windows environment. Fortunately, VirtuaWin offers an wonderful solution. I have used VirtuaWin in my office PC for at least couples of month. Today I have to do some works in Windows in my laptop, recalled VirtuaWin, and I think this time, I should introduce this good applications to you who read this post. In addition to its excellent functions, VirtuaWin is licensed under GNU.

    After downloading VirtuaWin and installing it, you can check the content of Setup by right clicking on the icon of VirtauWin, which should reside in the bottom right toolbar of the desktop.

    Basic usage by default:
    Alt+Ctrl+Left/Right/Up/Down: to navigate around the virtual desktops
    Alt+Win+Left/Right/Up/Down: to move the working window to the next virtual desktop

    ---
    Note: Its name is VirtuaWin, not VirtualWin.

    Wednesday, January 26, 2011

    Trials of pycam example code

    I am trying to learn something about computer version, and Python is my first choice of the coding tool. After several hours of searching, I found pycam. There are several sample codes given by the pycam. What I have downloaded was the ``pycam-read-only'' folder in which are three subdirectories:

    • c-vs-py
    • openeyes
    • pycam

    To install the pycam, just find the ``setup.py'' in the subdirectory named ``pycam'' and run ``sudo python setup.py install.'' Furthermore, what you need to install might include the follows:

    • python-opencv
    • python-pygame
    • libcv-dev
    • libcvaux-dev
    • libhighgui-dev

    The lib*-dev are (my guess) for the cpp codes, which need to include the cv.h and highgui.h.

    So far, my testing results are:

    1. pycam-read-only/pycam/examples/: Okay, but the fps are low
    2. pycam-read-only/pycam/examples/opencv: Failed. There were errors I don't know how to resolve yet. Only the ``cam-histo.py'' can be executed, after remove the ``cv.CV_VERSION'' at line 88. (the CV_VERSION seems not in my OpenCV.)
    3. pycam-read-only/pycam/pycam/: Failed
    4. pycam-read-only/c-vs-py/c_examples: Okay
    5. pycam-read-only/c-vs-py/py_examples: Some codes showed errors. They are harris_opencv.py, objectDetect.py, and people_only.py.
    ---
    EDIT: New results are given in the new post:
    Trials of pycam example code (2)

      Tuesday, January 25, 2011

      Face detection-- code patch and tests with some drawings

      My colleague introduced me an OpenCV site in Japanese in which many sample codes are available. Most of the sample codes are written in C++, and some are converted to Python also. I copied one sample code, which is used for face detection.

      After testing, I found the sample code has two lines which have to be modified to run properly. The first one seems to be a typo. The variable named as ``storage1'' should be ``storage'' according to other lines of code. The 25th line with variable ``starage1'' should be deleted. Another modification is to change assign the path of Haar-cascade xml files [*] explicitly. The original code gives the xml file without the complete path, and the program cannot find the so-called ``haarcascade_frontalface_default.xml.''

      The patch file:
      @@ -22,9 +22,8 @@
                       sys.exit(-1)
               src_gray = cvCreateImage(cvSize(src_img.width, src_img.height), IPL_DEPTH_8U, 1)
       
      -        storage1 = cvCreateMemStorage(0)
               ## (2)ブーストされた分類器のカスケードを読み込む
      -        cascade_name = "haarcascade_frontalface_default.xml"
      +        cascade_name = "/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml"
               cascade = cvLoadHaarClassifierCascade(cascade_name, cvSize(1,1))
       
               ## (3)メモリを確保し,読み込んだ画像のグレースケール化,ヒストグラムの均一化を行う
      
      


      Here I tested the sample code with some of my drawings, which can be viewed on my another blog. The results are interesting (at least for me).









      ---
      [*] I have just begun to learn things about computer vision, so I might use some terms wrong.

      Thursday, January 13, 2011

      [Py] Spline function test

      To interpolate data points, Scipy has several modules for this work. For what I am interested in, there are two functions seem to meet my requirement:

      According to the explanations given by the above links, to use UnivariateSpline with s=1 is equivalent to use InterpolatedUnivariateSpline.

      Here is the results of a simple test:

      Code:

      from numpy import linspace,exp
      from numpy.random import randn
      from scipy.interpolate import UnivariateSpline, InterpolatedUnivariateSpline
      from pylab import plot, legend, show
      
      x = linspace(-3, 3, 100)
      y = exp(-x**2) + randn(100)/10
      
      s0 = UnivariateSpline(x, y, s=0)
      s1 = UnivariateSpline(x, y, s=1)
      s2 = InterpolatedUnivariateSpline(x, y)
      
      xs = linspace(-3, 3, 1000)
      ys0 = s0(xs)
      ys1 = s1(xs)
      ys2 = s2(xs)
      
      plot(xs,ys0,'g--',label='UnivariateSpline s=0',linewidth=4)
      plot(xs,ys1,'b',label='UnivariateSpline s=1')
      plot(xs,ys2,'k',label='InterpolatedUnivariateSpline')
      plot(x,y,'ro',label='original signal')
      legend()
      show()
      

      Wednesday, January 05, 2011

      Producing sinusoidal signals


      Sometimes I need a signal of different frequencies for testing, so I wrote a simple Python code to produce compound sinusoidal signal by adding several cosine waves of different frequencies, amplitudes, and phases.

      The code is listed as the follows. (download)
      from scipy import *
      from pylab import *
      
      data_size = 10 # number of sinusoidal waves
      
      A = rand(data_size)
      theta = rand(data_size)
      f = [i for i in range(1,data_size+1)]
      t = linspace(0,10,1024)
      
      y = [0.0 for i in range(len(t))]
      
      for i in range(data_size):
              y += A[i]*cos(2*pi*f[i]*t + theta[i])
      
      plot(t,y)
      show()
      

      Trials of EMD C code

      I found a simple module of EMD in C on google code: realtime-emd. The source code, however, has no main function to invoke the modules. So I wrote one as the follows.

      #include <stdio>
      #include "EmpiricalModeDecomposition.h"
      
      void imfs_output(emdData* emd){
              int i, j;
      
              for(j = 0; j < emd->size; j++){
                      for(i = 0; i < emd->order; i++) {
                              printf("%f ", emd->imfs[i][j]);
                      }   
                      printf("\n");
              }   
      }
      
      int main(void){
              FILE *inputfile = fopen("sinusoid.txt","r");
              int i=0;
              float signal[1024]={0.0};
              emdData data;
      
              while (fscanf(inputfile, "%f", &signal[i]) != EOF){
                      //printf("%d %f\n",i,signal[i]);
                      i++;
              }
      
              emdCreate(&data,i,5,10,0); // problems here
              emdDecompose(&data,signal);
              imfs_output(&data);
              emdClear(&data);
      
              fclose(inputfile);
              return 0;
      }
      


      The main problem is the arguments of the function emdCreate. I am not sure how to set proper parameters to obtain correct results, so please take this clip of code as just a reference.

      You can download the main.c and sinusoid.txt for testing. The input file sinusoid.txt was generate by adding several sinusoidal signals.