Saturday 24 August 2013

make videos by linux + PowerDirector

Recently I need to do a video project for my daughter's birthday. the project involves two chapters: the first chapter is to slide her photos with 0.2 seconds interval, each photos has approximately the same head size and eye position is close. The second chapter is to combine all of the interesting videos of her.


The first one is going to be massive if one uses gimp or photoshop to do that. therefore, I decided to use scripts to handle these problem. After digging, I found opencv in python and matlab has the function of finding faces from files. So I decided to use opencv as a start. I found a code (updated from http://stackoverflow.com/questions/13211745/detect-face-then-autocrop-pictures) I did further change so that each output file has put the face right in the middle with same size (translations and scaling)

here is the working scripts :


'''
Sources:
http://opencv.willowgarage.com/documentation/python/cookbook.html
http://www.lucaamore.com/?p=638
'''

#Python 2.7.2
#Opencv 2.4.2
#PIL 1.1.7

import cv #Opencv
import Image #Image from PIL
import glob
import os


# x correction
adj=0.05
# the width of output image
picx = 1024.1
# the height of output image
picy = 768.1
# the x ratio of picture in the whole output image
xpra=0.4
# the x ratio where picture starts
xra = (1-xpra-adj)/2
# the y ratio where picture starts
yra = (picy-xpra*picx)/picy/2



def DetectFace(image, faceCascade, returnImage=False):
    # This function takes a grey scale cv image and finds
    # the patterns defined in the haarcascade function
    # modified from: http://www.lucaamore.com/?p=638

    #variables    
    min_size = (200,200)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0



    
    # Equalize the histogram
    cv.EqualizeHist(image, image)

    # Detect the faces
    faces = cv.HaarDetectObjects(
            image, faceCascade, cv.CreateMemStorage(0),
            haar_scale, min_neighbors, haar_flags, min_size
        )

    # If faces are found
    if faces and returnImage:
        for ((x, y, w, h), n) in faces:
            # Convert bounding box to two CvPoints
            pt1 = (int(x), int(y))
            pt2 = (int(x + w), int(y + h))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)
    if returnImage:
        return image
    else:
        return faces

def pil2cvGrey(pil_im):
    # Convert a PIL image to a greyscale cv image
    # from: http://pythonpath.wordpress.com/2012/05/08/pil-to-opencv-image/
    pil_im = pil_im.convert('L')
    cv_im = cv.CreateImageHeader(pil_im.size, cv.IPL_DEPTH_8U, 1)
    cv.SetData(cv_im, pil_im.tostring(), pil_im.size[0]  )
    return cv_im

def cv2pil(cv_im):
    # Convert the cv image to a PIL image
    return Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())

def imgCrop(image, cropBox, boxScale=1):
    # Crop a PIL image with the provided box [x(left), y(upper),
    # w(width), h(height)]

    # Calculate scale factors
    xDelta=max(cropBox[2]*(boxScale-1),0)
    yDelta=max(cropBox[3]*(boxScale-1),0)

    # Convert cv box to PIL box [left, upper, right, lower]
    PIL_box=[cropBox[0]-xDelta,
             cropBox[1]-yDelta,
             cropBox[0]+cropBox[2]+xDelta,
             cropBox[1]+cropBox[3]+yDelta]

    return image.crop(PIL_box)

def faceCrop(imagePattern,boxScale=1):
    # Select one of the haarcascade files:
    #   haarcascade_frontalface_alt.xml  <-- Best one?
    #   haarcascade_frontalface_alt2.xml
    #   haarcascade_frontalface_alt_tree.xml
    #   haarcascade_frontalface_default.xml
    #   haarcascade_profileface.xml
    faceCascade = cv.Load(
        '/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml')
    #  faceCascade = cv.Load('haarcascade_frontalface_alt.xml')

    imgList=glob.glob(imagePattern)
    if len(imgList)<=0:
        print 'No Images Found'
        return

    for img in imgList:
        pil_im=Image.open(img)
        cv_im=pil2cvGrey(pil_im)
        faces=DetectFace(cv_im,faceCascade)
        if faces:
            n=1
            for face in faces:
               #  scale factor new/old
               scf=(picx*xpra)/face[0][3]
               #  new =old*scf
               rsx=int(pil_im.size[0]*scf)
               rsy=int(pil_im.size[1]*scf)
               resizedImage=pil_im.resize(
                   (rsx,rsy)
                   ,Image.ANTIALIAS)
               croppedImage=imgCrop(
                   resizedImage,
                   (int(face[0][0]*scf-xra*picx),int(face[0][1]*scf-yra*picy),int(picx),int(picy)),
#                   (int(face[0][0]*scf),int(face[0][1]*scf),int(picx),int(picy)),
#                   (int(face[0][0]*scf),int(face[0][1]*scf),int(face[0][2]*scf),int(face[0][3]*scf)),
                   boxScale=boxScale)  
               croppedImage2=imgCrop(pil_im, face[0],boxScale=boxScale)
               fname,ext=os.path.splitext(img)
               #resizedImage.save(fname+'_resized'+str(n)+ext)
               croppedImage.save(fname+'_r_c'+str(n)+ext)               
               croppedImage2.save(fname+'_cropori'+str(n)+ext)
               n+=1
        else:
            print 'No faces found:', img



            
def test(imageFilePath):
    pil_im=Image.open(imageFilePath)
    cv_im=pil2cvGrey(pil_im)
    # Select one of the haarcascade files:
    #   haarcascade_frontalface_alt.xml  <-- Best one?
    #   haarcascade_frontalface_alt2.xml
    #   haarcascade_frontalface_alt_tree.xml
    #   haarcascade_frontalface_default.xml
    #   haarcascade_profileface.xml
    faceCascade = cv.Load('haarcascade_frontalface_alt.xml')
    face_im=DetectFace(cv_im,faceCascade, returnImage=True)
    img=cv2pil(face_im)
    img.show()
    img.save('test.png')


# Test the algorithm on an image
#test('testPics/faces.jpg')

# Crop all jpegs in a folder. Note: the code uses glob which
# follows unix shell rules.
# Use the boxScale to scale the cropping area. 1=opencv box,
#    2=2x the width and height
#faceCrop('/home/debianchenming/Downloads/1/*.JPG',boxScale=1)
faceCrop('/media/D0CC0864CC0846E6/Portrait/secondtime/12-12-09/*.JPG',boxScale=1)

Thursday 22 August 2013

Slackware study

1. i found my laptop (toshiba is not happy with network-manager). when it is scheduled on boot by


# chmod +x /etc/rc.d/rc.networkmanager
the computer freezes without starting X desktop.
so i just switch it off by
# chmod -x /etc/rc.d/rc.networkmanager
and use wpa_supplicant to connect to internet

later I found enable wicd on boot is also problematic (system freezes when input username in). so I have to disable that on boot as well.
# chmod -x /etc/rc.d/rc.wicd

#ifconfig wlan0 up
#slackpkg update
note that before update, one has to enable the mirrors as by default they are commented out. so just go to mirror location:
## mirror location
/etc/slackpkg/mirrors

and uncomment the mirror you want to use

#slackpkg install-new
#slackpkg upgrade-all
#slackpkg clean-system

# upgradepkg --install-new /home/chenming/Downloads/slapt-get-0.10.2p-x86_64-1.tgz

# slapt-get --update
# slapt-get -i htop

remember, real slackers install software from compilling.
I have found that to install chromium, one has to install libevent first. it can be found from
http://slackbuilds.org/repository/14.0/libraries/libevent/

that is also required by i3 (However, i3 requires libev, which is in confilict with libevent. but most software requires libevent.).

apparantly sbopkg does not solve dependency problem properly. but still it gives a better way to easy install packages.

for the virtualbox, it seems as if one can direct install the binary files provided by official website

i3 can be installed easily from sbopkg.




Tuesday 20 August 2013

How to connect to wifi through wpa_supplicant and wicd-cli (in particular get connected to eduroam in australia)


iw(list/config) can only handle WEP.
You need wpa-supplicant for this.
sudo apt-get install wpasupplicant
In /etc/wpa_supplicant.conf you put your ssid and password.
gksu gedit /etc/wpa_supplicant.conf 
Example:
network={
            ssid="ssid_name"
             psk="password"
}
Assuming your interface is wlan0 you can connect to it with...
wpa_supplicant -B -iwlan0 -c/etc/wpa_supplicant.conf -Dwext && dhclient wlan0
C--------------------------------

Now I have a case for connecting it in eduroam in australia.

first, make a file named as "wpa_supplicant.conf" on the desktop.


ctrl_interface=/var/run/wpa_supplicant
eapol_version=1
ap_scan=1
fast_reauth=1
network={
 ssid="eduroam"
 scan_ssid=1
 key_mgmt=WPA-EAP WPA-NONE IEEE8021X
 eap=PEAP
 phase2="auth=PAP auth=MSCHAPV2"
 identity="PUT YOUR account name here"
 password="PUT YOUR password here"
priority=2
auth_alg=OPEN
}
 be aware that you need to put account name and password in the file

then run
# wpa_supplicant -c /home/chenming/Desktop/wpa_supplicant.conf -iwlan0 -Dwext
# dhclient wlan0
enjoy!
##-----------------------------another comments ------------------------

in scientific linux distro, the current version is 6.4, which only support wicd 1.7.0, however, it has been found that this wicd 1.7.0 does not work well with eduroam. if one has to stick SL, compile wicd 1.7.2 from source is required, I did that in SL and found it is be very buggy, in particular, when using wicd-gtk to specify connectionn type and passwork and so on, the window can not be closed. then I decided to use wicd-cli, a command line based app to connect the 1.7.2 to eduroam. So far from the man page, I can't see any thing that is equivalent to -c in wpa_supplicant which specifiy the connection type and these sort of things. however, those information is needed for complex eduroam. so I dicided to stop and just wait when redhat update to 7.

make satellite toshiba laptop working well with linux

This type of laptop has got a wired hardware so that it doesn't work properly with:
1. debian wheezy: problem freeze at logon window.
2. ubuntu 12.04: freeze at logon
3. archlinux can not install
4. debian live disk. freeze at install.
5. scientific linux 6.4. the speed to eduroam is very slow. (same thing apply to debian squeeze)

I have tried many network drivers for RTL8188ce, like from official web, linuxwireless, none of them work properly. later on I found this wireless driver is working properly with wicd, however, once it is in version 1.7.0, it always show wrong password. (which I found is a bug)

it looks as if we 1. not using gnome 3, 2. using wicd 1.7.2, all the problems may solve out automatically. therefore, I dicided to use lubuntu, which uses lxde as default desktop.

however, again wicd becomes wired. after excuting

    $ sudo service wicd start

it doesn't working properly. it turns out that there is another bug in wicd (wicd is really buggy!!)

https://bugs.launchpad.net/ubuntu/+source/wicd/+bug/1132529

to solve it out I did:


  rm /etc/resolv.conf
    ln -s /run/resolvconf/resolv.conf
    rm /var/lib/wicd/resolv.conf.orig
    ln -s /run/resolvconf/resolv.conf /var/lib/wicd/resolv.conf.orig

and removed network-manager. Now the speed working fantastically!!!

Thursday 15 August 2013

ubuntu study

UBUNTU Study
  1. about how to map the local NTFS drive into UBUNTU (2012-3-10)
$ sudo mount -t ntfs -o nls=utf8,umask=0222 /dev/sda2 /home/uqczhan2/d
$ sudo mount -t ntfs -o nls=utf8,umask=0222 /dev/sda1 /home/uqczhan2/c
  1. Resize the driver.
Right now I found that the traditional partition method do not work for the new system, like partition manager. So the only way is to change it into some other software. Gparted seems to be a very good solution, and it is a free software
  1. select a column in a text
geany is an integrated development environment that provides good column editing support which is worth checking out (an example here)
Simply put, press down Alt+Shift instead of just Shift while making selections to invoke column mode. One important limitation is that the edits in column mode are (as yet) not undo-able.
To achieve the same with the mouse, hold down Ctrl while selecting. This actually works with most Scintilla-based editors (including SciTE and Geany).
    1. How to drag a file
    2. how to cancel the preview of files in the browser.
Go to the browser window ->edit->preferences->preview.
    1. Wine HQ
A very good place to check which software is good
    1. delete a directory
sudo rm -rf /home/soft/

8. mount hdrive
$ sshfs uqczhan2@mango.eait.uq.edu.au:/software /home/uqczhan2/software
$ sshfs uqczhan2@mango.eait.uq.edu.au:/home/users/uqczhan2 /home/uqczhan2/hdrive
now I found a new way of doing this
$ gksu gedit /etc/fstab
then add this line at the end of the fil
sshfs#uqczhan2@mango.eait.uq.edu.au:/home/users/uqczhan2 /home/uqczhan2/hdrive fuse comment=sshfs,noauto,users,exec,uid=1000,gid=1000,allow_other,reconnect,transform_symlinks 0 0
after that do
$ sudo gedit /etc/fuse.conf
then add
user_allow_other
then you will get the access to hdrive after inputing the password.

  1. open a xp virtual machine
$ DISPLAY=:0 VBoxManage startvm "winxp"
$ DISPLAY=:0 VBoxManage startvm "xpdomain"
  1. a problem you will have when using idb
Gtk-WARNING **: Unable to locate theme engine in module_path: "pixmap",
the way of solving it is
$ sudo apt-get install gtk2-engines-pixbuf
  1. Install wireless card to my laptop:
My laptop can not install ubuntu 11.04 due to some issues. So I downgraded into 10.04. however, this edition doesn't come up with the Wifi driver. So I have to find it out. Then the big problem comes. The first thing is that installing the package requires installing building-essential, but I can not install the building-essential package individually because it is dependent on some other software. There are several ways to obtain that 1. setup your own repository. 2. using installing disc. 3. using
I can only choose the second.
The file is rtl8192ce_linux_2.6.0006.0321.2011.tar.gz
when you get in, read the readme to get the instruction.
Sudo apt-get install building-essential
sudo su
make
make install
reboot now
    1. add user into virtualbox :
$ sudo usermod -G vboxusers -a uqczhan2

  1. to extract the gar.gz files
$ tar -xzvf *.
  1. To find out where the installation is located
$ whereis eclipse
To get the list of installated files, go for
$ dpkg --get-selections > my_software.txt
  1. To delete a program
$ sudo apt-get purge eclipse-platform
    1. To synchronise files
-a archive mode; equals -rlptgoD (no -H,-A,-X)
means file will be archive all the folders and subdirectories
/home/uqczhan2/syncTest/Gran source path
--delete to delete extraneous files from destination dirs be careful to use it.
But if no this, you will not delete the file, but you also will not sync the new file in archive back to the origin folders
--max-delete=NUM don't delete more than NUM files
--stats give some file-transfer stats
-h, --human-readable output numbers in a human-readable format
--progress show progress during transfer-i,
--itemize-changes output a change-summary for all updates
--out-format=FORMAT output updates using the specified FORMAT
--log-file=FILE log what we're doing to the specified FILE
-u, --update skip files that are newer on the receiver
this means that the newer file in the destination will not be removed
-n, --dry-run perform a trial run with no changes made
finally


$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update /home/uqczhan2/hdrive/Application/ --delete /home/uqczhan2/MobiC/Application/ --dry-run

$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update /home/uqczhan2/hdrive/Saltmarsh/ --delete /home/uqczhan2/MobiC/Saltmarsh/ --dry-run

$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update /home/uqczhan2/hdrive/Study/ --delete /home/uqczhan2/MobiC/Study/ --dry-run

$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update /home/uqczhan2/hdrive/Work/ --delete /home/uqczhan2/MobiC/Work/ --dry-run
sync the file in the mobile drive back into h drive
$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update --dry-run /home/uqczhan2/MobiC/Application/ --delete /home/uqczhan2/hdrive/Application/

$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update --dry-run /home/uqczhan2/MobiC/Saltmarsh/ --delete /home/uqczhan2/hdrive/Saltmarsh/

$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update --dry-run /home/uqczhan2/MobiC/Study/ --delete /home/uqczhan2/hdrive/Study/

$ rsync -a --progress --human-readable --log-file=sync.txt -stat --update --dry-run /home/uqczhan2/MobiC/Work/ --delete /home/uqczhan2/hdrive/Work/
  1. solve the slow scrollI know this topic is outdated, but the problem still exists and I found the answer:
    The module-init-tools package installs the /etc/modprobe.d/blacklist-framebuffer.conf file, in which it blacklists all the console framebuffer modules. For some reason the vga16fb module is not on the list, and (at least for me) it is loaded during startup and causes the slow scrolling. Try to blacklist it and reboot!
  2. Mount disk on the local folders
$ sudo mount -a /dev/sdb1 /home/uqczhan2/MobiC
$ sudo mount -a /dev/sdb5 /home/uqczhan2/MobiD
$ sudo mount -a /dev/sdb6 /home/uqczhan2/MobiE
    1. Checking PCI harddrive
$ lspci
    1. Checking the hard disk condition
$ sudo fdisk -l
$ sudo blkid
25.Using evolution as mail receiver
the problem here still is the calenda. I still could not make it right
  1. changing the default opening program
right click the file-> properties->open with
    1. Nautilus is the file explorer manager. Use ctrl+1 to open it
    2. To disable the prompt: do you want to run '' or display its contents?
Go into the Nautilus Edit-Preferences-Behaviour and set the appropriate option.
  1. Changing eps into png (high resolution)
using Inkscape->export bitmap->export area Drawing and high resolution
  1. connect to Ssh
  1. map folder in the local path
~$ sshfs uqczhan2@barrine.hpcu.uq.edu.au:/home/uqczhan2 /home/uqczhan2/Barrine
  1. Check your id information
$ id
  1. get the virtualbox guest additions
$ sudo apt-get install virtualbox-guest-additions-iso
  1. solving the problem for matlab when opening it.
/usr/local/MATLAB/R2011b/bin/util/oscheck.sh: 605: /lib64/libc.so.6: not found
run this command
$ sudo ln -s /lib/x86_64-linux-gnu/libc.so.6 /lib64/libc.so.6
  1. Three more ways of uninstall the program:
$ sudo apt-get remove google-chrome-stable
$ sudo dpkg -r chromium
find it in sypanic and then uninstall
  1. install deb file
$ sudo dpkg -i abd.deb
  1. add shortcut to .bashrc
$ gvim .bashrc
then add

## chenming shortcut for matlab
alias matlab='/usr/local/MATLAB/R2012a/bin/matlab'
## end shortcut
  1. install intel fortran compiller
a. install build-essential
b. download l_fcompxe_intel64_2011.9.293.tgz from intel website
c. use $ tar -zxvf x.tgz to extract the file
d. install
e. use $ /usr/intel/bin/compilervars.sh intel64 to add the source in the repository
f. add 'source /opt/intel/bin/compilervars.sh intel64' at the end of the .bashrc file excute $. .bashrc to reload the bash file so that the update will be loaded without reinstall the machine.
  1. install invidia driver
if you have a computer with invidia driver, you have to use additional drivers in ubuntu app to install drivers. However, people say the repository is old. Therefore, you can also add the following commands to install the newest
$ sudo add-apt-repository ppa:ubuntu-x-swat/x-updates
$ sudo apt-get update
$ sudo apt-get upgrade
then you will get the newest version of nvidia driver
after that you need to pop in the app Nvidia x server settings to activate the second screen. This is mosly needed if you have two video card. Then you have to save the settings back into a file rather than excute it directly. This is the difference between windows and ubuntu because in ubuntu you have to save the settings in a file and then you can reload after reboot.
For the rotating, you have to add one line in
Section "Device"
Identifier "Device1"
Driver "nvidia"
VendorName "NVIDIA Corporation"
BoardName "Quadro FX 580"
BusID "PCI:3:0:0"
Option "Rotate" "left"
EndSection
$ sudo gedit /etc/X11/xorg.conf

  1. install matlab 2012a
there is one thing you have to be aware of, which is that 'glnxa64' means linux 64 bit, so you have to extract 'matlab_R2012a_glnxa64_installer.zip' file, then you could see install is a excutable file.
Also add alias matlab='/usr/local/MATLAB/R2012a/bin/matlab' in .bashrc to activate shortcut input, then use $ . ~/.bashrc to reload bash file without rebooting.
  1. installing the video card driver to Toshiba laptop.
It is not always correct to install all the drivers from suggestions given by the app 'additional drivers', althrough this method works quite well for the Video card of T550 (choose 'NVIDIA accelerated graphics driver (version current) [recommended]'), this method doesn't apply to the video card of toshiba laptop. From forum it says that usually the suggestion from additional drivers is quite old because the repository is not updating to the newest driver.sfdf
  1. permanently mount a local drive
$ sudo blkid #use this command to find out the UUID
$ gksudo gedit /etc/fstab
then add this line
UUID=<uuid>     /place/to/mount     ext4     defaults    0     2
where <uuid> is the UUID we copied from the blkid command. /place/to/mount is the mount directory. ext4 is the filesystem
then you can reload the fstab file by
$ sudo mount -a
then apply the follow setence to obtain the write permission
$ sudo chown <username>:<username> /place/to/mount
  1. mount CD file *.bin in UBUNTU
$ sudo aptitude install fuseiso
$ fuseiso -p image_file.bin /path/to/mount
$ fusermount -u -z /path/to/mount
the -z in the last command is used when the disk is busy.
  1. install software that sophos do not like in windows
you can stop the process SavService.exe from task manager, then immediately open the file you want to open.
    1. find out where the application is installed.
$ locate chromium
    1. a useful tool to separate the screen
$ sudo apt-get install compizconfig-settings-manager
then you can use $ CCSM to adjust it
  1. installing network card to the desktop
I found the newly bought TL-WN951N 300M doesn't work properly in ubuntu 11.10. when you connect into the network, you couldn't log on internet even if it appears to be connected. and there is no linux driver from the website. However, lateron I used
$ lspci -v
$ lspci -nn
and I found the driver I am using is ath9k. So I downloaded the new driver (compat-wireless-3.4-rc3-1.tar.bz2 )
after extracting it, using
$ ./scripts/driver-select ath9k
$ sudo make install
$sudo make unload
then it works perfectly.
  1. install vpn in ubuntu
choosing PPTP
Gateway: vpn.eait.uq.edu.au
user name: uqczhan2
NT domain: EAIT
activating: MSCHAPv2
tick Use Point-to-point encryption (MPPE)
also after the connection, use $ sudo ifconfig ppp0 mtu 1200
49. check whether you are working on unity 2D or 3D
$ echo $DESKTOP_SESSION

find out whether how good you are working on Unity 3D
$ /usr/lib/nux/unity_support_test -p
50. first console mode
Ctrl+alt+F1
to let it come back
Ctrl+alt+F7