#!/usr/bin/env python # coding: utf-8 # # Installation # # - python enviroment # - anaconda # - linux/virtualenv # - [WSL on windows](https://ubuntu.com/wsl) # - virtualenv --system-site-packages p3 # - jupyter # - pip install jupyter # # Linux terminal essential # ``` # cd .. # cd # back to home dir # cd ~/p2 # ~ means home dir as well # # ls # dir # ``` # # \# install vim: sudo is to notify running as administrater, # \# apt-get install to install package, # \# vim is a common editor (extension of vi) # ``` # sudo apt-get install vim # sudo apt-cache search vim # search packages relating to vi # ``` # # ``` # gedit tmp.py # open to edit a file # ctrl-z # break the current job # bg # send job to background # fg # send job back to foreground # ctrl-c # kill foreground job # ps aux | grep gedit # list all jobs and then search for "gedit" jobs # kill -9 12345 # kill job with job number 12345 # ``` # # \# extract the audio channel of dummy.mp4 # \# audio format suppose to be aac though # \# change extension (.ac3/.flac) accordingly # ``` # ffmpeg -i dummy.mp4 -acodec copy -vn audio.aac # ``` # # #extract audio channels from all mp4 files in current folder # ``` # ls -1 \*.mp4|sed 's/\(.*\)\.mp4/ffmpeg -i "&" -acodec copy -vn "\1.aac"/'|sh # ``` # # # Setup virtualenv (tested on Ubuntu 18.04) # ``` # sudo apt-get install python-virtualenv # virtualenv ~/p2 # create a virtualenv with python2 # virtualenv --python=/usr/bin/python3 ~/p3 # create a virtualenv with python3 # source ~/p2/bin/activate # ``` # For windows # # ``` # conda create -n p36 python=3.6 # dlib seems to be faster than in 3.77 # conda activate p36 # ``` # # help # In[99]: help('if') # In[6]: import numpy # In[1]: # help(numpy) # In[2]: # help(numpy.sort) # In[7]: get_ipython().run_line_magic('pinfo', 'numpy.sort') # In[106]: dir(numpy) # In[107]: dir(numpy.sort) # In[110]: import inspect inspect.getmembers(numpy.sort) # In[111]: help(inspect.getmembers) # In[112]: inspect.getsourcefile(numpy.sort) # In[114]: print(inspect.getsource(numpy.sort)) # # arithmetics # In[6]: 2**3 # In[5]: 1+1 # In[ ]: #a,b ctrl-_ # In[9]: import math math.cos(90) # In[10]: import numpy as np np.cos(90) # # if elif else # In[11]: #if if 1>0: print('1>0') else: print('error') # In[14]: x=[1,2] bigger=x[0] if x[0]>x[1] else x[1] print(bigger) # In[18]: state='oklahoma' if state.lower()=='oklahoma': abr='ok' elif state.lower()=='texas': abr='tx' elif state.lower()=='hawaii': abr='hi' print(abr) # # for loop # In[15]: alist=[1,2,3] #for for l in alist: print(l) # In[20]: for i in range(10): print(i) # ## shorthand # In[39]: odd_numbers2=[c*2+1 for c in range(10)] print(odd_numbers2) # In[37]: odd_numbers=[c for c in range(10) if c%2==1] print(odd_numbers) # ## enumerate # In[1]: states=['texas','oklahoma','hawaii'] for state in states: print(state) for i,state in enumerate(states): print(str(i)+': '+state) # ## zip # In[2]: for i,state in zip(range(3),states): print(str(i)+': '+state) # ## = does not create copy # In[3]: states_copy = states states_copy.append('kansas') print(states) # In[4]: states_copy = states.copy() states_copy.append('tenesse') print(states_copy) print(states) # In[5]: ''.join([c*3 for c in 'computer vision']) # ## append and pop # # # In[6]: print(states) # In[7]: states.pop(1) # In[8]: print(states) # In[9]: states.insert(1,'oklahoma') print(states) # In[10]: states.remove('hawaii') print(states) # In[11]: odd_numbers = [i for i in range(20)] for i in odd_numbers: if i%2==0: odd_numbers.remove(i) print(odd_numbers) # # dict # In[12]: states=dict() # In[13]: states # In[14]: states={'ok':'oklahoma','tx':'texas','hi':'hawaii'} # In[15]: for key in states: print(key) # In[16]: states.keys() # In[17]: states.values() # In[18]: abrvs=['ok','tx','hi'] states=['oklahoma','texas','hawaii'] state_map=dict([(abrv,state) for abrv,state in zip(abrvs,states)]) # In[19]: print(state_map) # In[20]: state_map.pop('tx') # In[21]: state_map # ## find key by value # In[22]: list(state_map.keys())[list(state_map.values()).index('hawaii')] # # Fun # In[23]: from datetime import date, timedelta # In[24]: date.today() # In[25]: date(2022,1,16)+timedelta(weeks=1) # # numpy # In[27]: import numpy as np np.array([1,2,3,4]) # In[28]: np.array(range(16)).reshape((4,4)) # In[29]: np.ones(4) # In[30]: np.linalg.eig(np.random.randn(4,4)) # In[31]: np.linalg.svd(np.random.randn(4,5)) # In[32]: all_one=np.ones((3,3)) np.zeros_like(all_one) # In[33]: x=np.ones((1,3)) x @ all_one @ x.T # # IO # In[34]: with open('opencv basic.ipynb') as fpt: print(fpt.read()) # In[35]: import pickle a={'hello':'world'} with open('dummy.pickle','wb') as fpt: pickle.dump(a,fpt) with open('dummy.pickle','rb') as fpt: b = pickle.load(fpt) b # In[36]: from scipy.io import savemat,loadmat # save and load matlab mat files x=range(10) mdic={'x':x} savemat('dummy.mat',mdic) # In[37]: mdic2=loadmat('dummy.mat') mdic2 # # plotting # In[38]: from matplotlib import pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') plt.figure(figsize=(10,10)) plt.plot(range(10)) plt.xlabel('dummy') plt.title('hello')