0%

Environment and Packages Management

For environment, you can use virtualenv to create a virtual python environment to seperate this to the original environment.

1
virtualenv XX

Note. the virtual env folder will be located to your current folder of the bash locates. When you change the path of the env folder, it will cause problem for some direct running module. Like pip, jupyter

For packages, you can use the following command to export installed packages in env1 and install those packages in env2

1
2
(env1) $ pip freeze > requirement.txt
(env2) $ pip install -r requirement.text

Opencv has a lot of useful functions.

Show image/video stream

1
2
3
while True:
cv2.imshow(img)
cv2.waitKey(1)

Color Space Convert

1
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Histogram and Backprojection

1
2
hist = cv2.calcHist([img], [0, 1], None, [180, 256], [0, 180, 0, 256])
mask = cv2.calcBackProject([target], [0, 1], hist, [0, 180, 0, 256], 1)

Otsu threshold

1
_, mask = cv2.threshold(img[:, :, 1], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

Geometric

Contour of images

1
_, contours, _ = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

Convex Hull and Convex Defect

1
2
hull = cv2.convexHull(contour, returnPoints=False)
defects = cv2.convexityDefects(contour, hull)

Below are some basic machine learning skills

  • Clustering
    • K-means
  • Instance-based Algorithms
    • KNN
  • Bayesian Algorithms
    • Naive Bayes
      • 假设输入的feature之间互相独立,因此概率的联合分布函数可以写成连乘
  • Regression
    • OLSR,最小二乘
    • Linear Regression
    • Logistic Regression
  • Decision Tree
    • ID3
    • C4.5
    • CART
  • Ensemble Algorithms:多个弱分类器共同构成强分类器
    • Boosting
    • Bagging
    • Random Forest
    • Gradient Boosted Regression Trees
  • Artificial Neural Network
    • Perceptron
  • SVM
  • Dimensionality Reduction Algorithms
    • PCA
    • PCR

Reference

机器学习算法集锦:从贝叶斯到深度学习及各自优缺点

Object detection is kind of classificitaion which you need to figure out where the object is located in the image. The output is usually described as:
$$
y = [P_c, P_x, P_y, W, H, c_1, c_2, c_3, …]
$$
$P_c$ means whether this image contain the content you’re detecting, $c_i$ the probability of the label_i object exists.

Object detection includes both localization and detection.

Sliding Windows Detection

Convolution Implementation

YOLO(You Only Look Once) Algorithm

Evaluationg Object Localization

$$
Intersection\ Over\ Union = \frac{size\ of\ overlaping\ area}{size\ of\ true\ and\ detection\ area}
$$

Non-max Suppression Algorithm

Overlapping Objects

Import a personal lib

Importing the .ipynb file we wrote as a module to a new .ipynb file is different from importing them in a .py file. The easiest way is to convert those .ipynb file to a .py file and you can import those in a new .ipynb file. However, this method doesn’t allow you to modify the code and reload it easily.

When the .ipynb lib file is in the same folder with you

For example, the lib’s name is test.ipynb. There is a function called hello() within it. In a new .ipynb file, you need to type the following.

1
2
3
4
5
6
7
# !pip install import-ipynb
import import_ipynb
import importlib
importlib.reload(test)
import test

test.hello()

The import_ipynb lib is allowing you to import a .ipynb file. The importlib file is allowing you to reload the module thus the new change to the lib file can be reloaded into your file.

When the .ipynb lib file is in the subfolder of current folder

For example, the lib file in the folder of lib. The lib’s name is test.ipynb. There is a function called hello() within it. In a new .ipynb file, you need to type the following.

Powershell is a more powerfull shell than CMD in Windows. However, the layout of powershell in Windows is very ugle, especially the default font. In order to change the font of powershell, we need to do several works.

Descipline

Actually the font can be change in the powershell if we click the top left corner of the powreshell interface. While if the CodePage is 936(Chinese), you can only change the font of powershell among several Chineses fonts, which are not well performed in the powershell.

So we need to find some way to change the CodePage of powershell to 437(English), then we can change the font to some English font like Consolas.

In order to change the default page to another, there are two steps need to finish that. One is for the powershell launched by Win+R, and another is the powershell launched by the start menu. It is because the software launched by shortcut follows other disciplines which I don’t know much.

Change for the Win+R powershell

  1. Open the Win+R, and input regedit
  2. Navigate to \HKEY_CURRENT_USER\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
  3. Create/Change the CodePage of type DWORD to decimal 437

Change for the shortcut powershell

  1. Right click the powershell in start menu and go to the file location
  2. Right clikc the ico and go the properties
  3. Add the following words to the end

-NoExit -Command "chcp 437; cls"

The command above means the powershell will run the two commands automatically. The chcp 437 means to swtich the Codepage to 437; and the cls means to clear the powershell.

There is a lot of development frameworks for Java. This post talks about Guice (for Dependency Injection design pattern implementation), Spring (Java integration environment), JUnit and Mockito (for unit test).

Read more »

Tree is a basic data structure. The most popular tree is the binary tree, which has a left child and a right child. The exercise usually gives you the root node of the tree. The basic method to solve a tree problem is recursive.

Traverse the tree

Iteration

1
2
3
4
5
6
openlist = [root]
while len(openlist) > 0:
node = openlist.pop() # openlist.pop(0) if you want BFS
check(node)
openlist.append(node.left)
openlist.append(node.right)

Recursive

1
2
3
4
def check_tree(node):
if node:
check_tree(node.left)
check_tree(node.right)

The above two ara both DFS(Deep First Search), in order to achieve the BFS(Breadth First Search), in other means, traverse the tree layer by layer, we need to use a dictionary to store the node

1
2
3
4
5
6
7
8
9
10
dic_tree = {}

def check_tree_breadth(node, depth=1):
if node:
if depth not in dic_tree:
dic_tree[depth] = [node.val]
else:
dic_tree[depth].append(node.val)
check_tree_breadth(node.left, depth+1)
check_tree_breadth(node.right, depth+1)

Check the leaf

1
2
3
4
5
6
7
8
9
leafs = []

def check_leaf(node):
if node:
if not node.left and not node.right:
leafs.append(node.val)
else:
check_leaf(node.left)
check_leaf(node.right)

Calculate the depth

Iteration

Establish a list to collect the tuple like (node: depth)

Recursive

1
2
3
4
5
def get_depth(node):
if node:
return 1 + max(get_depth(node.left), get_depth(node.right))
else:
return 0

Check the path

1
2
3
4
5
6
7
8
9
paths = []

def check_path(node, path=[]):
if node:
path.append(node.val)
check_path(node.left, path.copy())
check_path(node.right, path.copy())
else:
paths.append(path)