Friday 29 May 2020

How to keep track console output into a log file for python?

class ReDirectSTD(object):
    def __init__(self, fpath=None, console='stdout', immediately_visiable=False):
        import sys
        import os
        assert console in ['stdout', 'stderr']
        self.console = sys.stdout if console == "stdout" else sys.stderr
        self.file = fpath
        self.f = None
        self.immediately_visiable = immediately_visiable
        if fpath is not None:
            # Remove existing log file
            if os.path.exists(fpath):
                os.remove(fpath)
        if console == 'stdout':
            sys.stdout = self
        else:
            sys.stderr = self

    def __del__(self):
        self.close()

    def __enter__(self):
        pass

    def __exit__(self, **args):
        self.close()

    def write(self, msg):
        self.console.write(msg)
        if self.file is not None:
            if not os.path.exists(os.path.dirname(os.path.abspath(self.file))):
                os.mkdir(os.path.dirname(os.path.abspath(self.file)))
            if self.immediately_visiable:
                with open(self.file, 'a') as f:
                    f.write(msg)
            else:
                if self.f is None:
                    self.f = open(self.file, 'w')
                self.f.write(msg)

    def flush(self):
        self.console.flush()
        if self.f is not None:
            self.f.flush()
            import os
            os.fsync(self.f.fileno())

    def close(self):
        self.console.close()
        if self.f is not None:
            self.f.close()


def may_mkdir(fname):
    if not os.path.exists(os.path.dirname(os.path.abspath(fname))):
        os.makedirs(os.path.dirname(os.path.abspath(fname)))

def del_mkdir(folder):
    if os.path.exists(folder):
        shutil.rmtree(
folder)  # delete output folder
    os.makedirs(
folder)  # make new output folder


def time_str(fmt=None):
    if fmt is None:
        fmt = '%Y-%m-%d_%H:%M:%S'
    return datetime.datetime.today().strftime(fmt)


# log
logfiletime = time_str()
stdout_file = os.path.join('log', 'stdout_{}.txt'.format(logfiletime))
stderr_file = os.path.join('log', 'stderr_{}.txt'.format(logfiletime))
may_mkdir(stdout_file)
may_mkdir(stderr_file)
if 1:
    ReDirectSTD(stdout_file, 'stdout', False)
    ReDirectSTD(stderr_file, 'stderr', False)

Wednesday 27 May 2020

How to run sudo in windows?

> runas /noprofile /user:mymachine\administrator cmd
> runas /profile /env /user:mydomain\admin "mmc %windir%\system32\dsa.msc"
> runas /env /user:user@domain.microsoft.com "notepad \"my file.txt\""

NOTE:  Enter user's password only when prompted.
NOTE:  /profile is not compatible with /netonly.
NOTE:  /savecred is not compatible with /smartcard.

How to build protobuf in ubuntu?

git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh 
 
 
 ./configure
 make
 make check
 sudo make install
 sudo ldconfig # refresh shared library cache.
 

How to overwrite sudo python for libuvc in ubuntu, lepton?

sudo sh -c "echo 'SUBSYSTEMS==\"usb\", ATTRS{idVendor}==\"1e4e\", ATTRS{idProduct}==\"0100\", SYMLINK+=\"pt1\", GROUP=\"usb\", MODE=\"666\"' > /etc/udev/rules.d/99-pt1.rules"

Monday 25 May 2020

How to build boost 1.65.0 in Ubuntu?

  1. Go to the root directory, cd boost_1_65_0.
  2. Run ./bootstrap.sh --prefix=PREFIX where PREFIX is the directory where you want Boost. Build to be installed.
  3. Run ./b2 install .
  4. Add PREFIX/bin to your PATH environment variable.

Friday 22 May 2020

Ubuntu how to convert png to jpg and rename the filename?

convert image from png to jpg:-
mogrify -format jpg *.png

remove original png files:-
rm *.png

rename the new jpg files:-
mmv "*.jpg" "ffhq_#1.jpg"

Wednesday 20 May 2020

Tuesday 19 May 2020

Sunday 17 May 2020

How to know the GPU usage and who is using it?

nvidia-smi --query-compute-apps=gpu_bus_id,pid,process_name,used_memory --format=csv,noheader,nounits

To know who or which user is using that process pid:-
ps -o pid,user,%cpu,%mem,etime,command -p "pid"
ps -o pid,user,%cpu,%mem,etime,command -p 3388

Saturday 16 May 2020

How to setup a shared folder in ubuntu? Samba Shared Folder

Setting up the Samba File Server on Ubuntu/Linux:

1. Open the terminal
2. Install samba with the following command:  sudo apt-get install samba cifs-utils
3. Configure samba typing: vi /etc/samba/smb.conf
4. Set your workgroup (if necesary). Go down in the file, until you see :

# Change this to the workgroup/NT-domain name your Samba server will part of 
workgroup = WORKGROUP

5. Set your share folders. Do something like this (change your path and comments)

# Ninja's share
[sharename]
  comment = YOUR COMMENTS
  path = /your-share-folder
  read only = no
  guest ok = yes

6. Restart samba. type: /etc/init.d/smbd restart
7. Create the share folder: sudo mkdir /your-share-folder
8. Set the permissions: sudo chmod 0777 /your-share-folder
you are all set in ubuntu


From Client Ubuntu PC:-
1. Open a folder explorer
2. Select "Other Locations" at the bottom left
3. Enter the following URL into bottom text box of Connect to Server
smb://guest@server_ip:/sharename
4. You will see a pop up to fill in the the password under Workgroup
just key in anything like 123 as the password
5. Done.


p/s: copied from https://adrianmejia.com/how-to-set-up-samba-in-ubuntu-linux-and-access-it-in-mac-os-and-windows/

Sunday 10 May 2020

How to list image size (width and height) in a folder?

identify -format "%f %wx%h\n" ./*/*

How to count and delete the subfolder less than a number?

To list folder less than 10:-
find . -maxdepth 1 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; |   awk '$NF<10'

To delete the folder less than 5:-
find . -maxdepth 1 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; |   awk '$NF<5' | xargs rm -rf

Saturday 9 May 2020

How to crop images in a batch, imagemagick, convert?

convert -crop WxH+x+y ./*.png ./tmp/new%02d.jpg

Example:
convert -crop 3192x1652+3+42 ./*.png ./tmp/new%02d.jpg

Thursday 7 May 2020

How to set path in CentOS?

vi ~/.bash_profile
>> export PATH=$PATH

source ~/.bash_profile

How to install Intel IPP library in Centos

1.下载 https://software.intel.com/zh-cn/intel-ipp
2.tar  zxvf    l_ipp_2017.3.196.tgz
3.cd l_ipp_2017.3.196
4.sh install.sh
5. 添加/opt/intel/ipp/lib/intel64 到 /etc/ld.so.conf
6. ldconfig

p/s:copied from https://www.jianshu.com/p/fe64d9ef8384

Monday 4 May 2020

How to set the device?

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

How to duplicate the same list in dict?

folder_list = [str(i) for i in range(62)] #lazy_hack
label_dict = dict(zip(folder_list,range(0,len(folder_list))))


(Pdb) folder_list
['0', '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', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61']

(Pdb) label_dict
{'60': 60, '40': 40, '20': 20, '27': 27, '54': 54, '30': 30, '24': 24, '50': 50, '55': 55, '7': 7, '21': 21, '49': 49, '0': 0, '57': 57, '26': 26, '35': 35, '41': 41, '5': 5, '15': 15, '33': 33, '16': 16, '31': 31, '46': 46, '17': 17, '25': 25, '56': 56, '37': 37, '3': 3, '58': 58, '51': 51, '53': 53, '13': 13, '32': 32, '29': 29, '44': 44, '1': 1, '43': 43, '38': 38, '59': 59, '14': 14, '34': 34, '2': 2, '9': 9, '10': 10, '39': 39, '47': 47, '48': 48, '4': 4, '6': 6, '8': 8, '23': 23, '22': 22, '45': 45, '42': 42, '18': 18, '11': 11, '28': 28, '61': 61, '12': 12, '36': 36, '52': 52, '19': 19}

(Pdb)

Sunday 3 May 2020

Cosine Similarity / Cosine Distance


p/s: picture copied from https://i.ytimg.com/vi/cpg6RTYlMUw/maxresdefault.jpg