Category Archives: How to Fix

You need to resolve your current index first

A branch from A switch to another branch B, to pull switch B branch after the operation, because of the pull actually contains the fetch + the merge operation, when performing the merge operation, due to long time no out the pull of B branch/merge operation, the local treasuries and remote B B difference is very big in the subsidiary (and these differences is the development of other colleagues files), when the merge conflict, causing the B branch state for merging, actually refers to the failed to merge, Stuck in the merge state, and unable to perform pull operations. Instead of resolving the conflict, instead of executing checkout/switchto from the B branch and attempting to switchto another branch, the report is:
[plain] view plaincopy in CODE looks at the CODE slice that is descended to my CODE slice

… Java: needs merge
…… Java: needs merge
…… Java: needs merge
error: you need to resolve your current index first

>, googled for a long time, finally found the answer on stackoverflow, the question is: merge failed, due to conflicts, can:

1, execute merge again after conflicts;

2, back to merge

well, since the merge conflict is a colleague’s file, I don’t need to resolve conflicts, then back to merge, just change my file and push, run the following code:

git reset –merge

done!

by the way, could you stick the stackoverflow links:

http://stackoverflow.com/questions/6006737/git-merge-errors

Reading and saving opencv Python video

Capture Video from Camera

gets video from the camera:

to capture video, you need to create a VideoCapture object. Its parameters can be the device index or the name of the video file (described below). The device index simply specifies the number of which camera. Zero represents the first camera and one represents the second camera. After that, you can capture the video frame by frame. But finally, don’t forget to release the capture.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

cap.read () : returns a Boolean (True/False). This returns true if the frame was read correctly, false otherwise. You can check this return value to see if the video is over.

cap. IsOpened () : checks if the cap is initialized. If it is not initialized, open it with cap.open () . The above code reports an error when the CAP is not initialized.

Get (propId) :

cap.get (propId) : accesses some of the features of the video, where propId is a number from 0 to 18, each number representing the video’s Property Identifier. Some of these values can be modified using cap.set (propId, value) , and value is the modified value.

For example, I check the frame width and height by cap.get (3) and cap.get (4). The default value is 640×480. But I want to change it to 320×240, using ret = cap.set (3, 320) and RET = cap.set (4, 240).


Playing Video from file

to play video from file:

is the same as capturing video from the camera, just change the camera index and video file name. When displaying frames, select the appropriate cv2.waitkey () time. If this value is too small, the video will be very fast, and if it is too large, the video will be slow (this can be used to display the video in slow motion). Normally, 25 milliseconds will do.

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Saving a Video

save video:

creates a VideoWriter object, specifying the output file name (for example: output.avi). Then you specify the FourCC code (FourCC is the 4-byte code used to specify the video codec. List of available code. Next, pass in frames per second (FPS) and frame size. The last is the isColor flag. If it is True, the encoder encodes a color frame; otherwise, a grayscale frame.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

official document

From in Python__ future__ The role of import *

when we read the code, we always see it beginning with the words from __import * with ___. What this does is introduce the features of the new version into the current version, meaning that we can use some of the features of the new version in the current version.

For example, in python2.x and python3.x the standard way of writing print is

# python 2.x
print "Hello World"

# python 3.x
print("Hello World")

If you want to use python2.x to experience python3.x, use the import print_function from ___ future__,

# python 2.x
from __future__ import print_function
print("Hello World")

and in that case if you go back to python2.x the standard way of writing it would be an error,

# python 2.x
from __future__ import print_function
print "Hello World"

>>> print "Hello World"
  File "<stdin>", line 1
    print "Hello World"
                      ^
SyntaxError: invalid syntax

in addition to the print function, the module with ___ future__ has many other functions,

1. Integer division

# python 2.x
5/2
>>> 2

from __future__ import division
5/2
>>> 2.5

2. With

# python 2.x
try:
    with open('test.txt', 'w') as f:
    f.write('Hello World')
finally:
    f.close()

# 用with替代上述异常检测代码:
from __future__ import with_statement
with open('test.txt', 'w') as f:
    f.write('Hi there!')

3. Absolute introduction (absolute_import)

Absolute

is mainly introduced for python2.4 and earlier versions which, when introducing a.py file, first look for it in the current directory. If so, the file in the current package is referenced first. If we want to reference python’s built-in.py file, we need to use

from __future__ import absolute_import


How to get all the keys of map by golang

best way: according to the length of the map, create a new array, walk through the map one by one press

method 1:

func getKeys1(m map[int]int) []int {
	// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
	j := 0
	keys := make([]int, len(m))
	for k := range m {
		keys[j] = k
		j++
	}
	return keys
}

method 2:

func getKeys2(m map[int]int) []int {
	// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
	keys := make([]int, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	return keys
}

other methods:

method 3:

func getKeys3(m map[int]int) []int {
	// 注意:由于数组默认长度为0,后面append时,需要重新申请内存和拷贝,所以效率较低
	keys := []int{}
	for k := range m {
		keys = append(keys, k)
	}
	return keys
}

method 4:

func getKeys4(m map[int]int) int {
	// 注意:虽然此写法简洁,但MapKeys函数内部操作复杂,效率极低
	keys := reflect.ValueOf(m).MapKeys()
	return len(keys)
}

experimental results are shown in the figure (you can see that methods 1 and 2 have the highest efficiency and least memory operation) :

complete code as follows:

package test

import (
	"reflect"
	"testing"
)

// 初始化map
func initMap() map[int]int {
	m := map[int]int{}
	for i := 0; i < 10000; i++ {
		m[i] = i
	}
	return m
}

func getKeys1(m map[int]int) []int {
	// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
	j := 0
	keys := make([]int, len(m))
	for k := range m {
		keys[j] = k
		j++
	}
	return keys
}

func getKeys2(m map[int]int) []int {
	// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率较高
	keys := make([]int, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	return keys
}

// 初始化默认
func getKeys3(m map[int]int) []int {
	// 注意:由于数组默认长度为0,后面append时,需要重新申请内存和拷贝,所以效率较低
	keys := []int{}
	for k := range m {
		keys = append(keys, k)
	}
	return keys
}

// 使用反射
func getKeys4(m map[int]int) int {
	// 注意:虽然此写法简洁,但MapKeys函数内部操作复杂,效率极低
	keys := reflect.ValueOf(m).MapKeys()
	return len(keys)
}

func BenchmarkMapkeys1(b *testing.B) {
	// 初始化map
	m := initMap()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		getKeys1(m)
	}
}
func BenchmarkMapkeys2(b *testing.B) {
	// 初始化map
	m := initMap()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		getKeys2(m)
	}
}

func BenchmarkMapkeys3(b *testing.B) {
	// 初始化map
	m := initMap()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		getKeys3(m)
	}
}

func BenchmarkMapkeys4(b *testing.B) {
	// 初始化map
	m := initMap()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		getKeys4(m)
	}
}


Solution to sudden change of win10 input method into traditional Chinese

this is because the ide code formatting shortcut, CTRL shift f, conflicts with the shortcut for simple and traditional switching.

solution, in the input method Settings, the shortcut key point into, drag to the bottom, turn off the simple switch can be

then — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

—-

Linux command line cursor moving skills

http://blog.csdn.net/leonzhang2008/article/details/6932268 to see a real expert operation command line is definitely a good experience – cursor in shuttling back and forth between words, different rolling command line.
here strongly builds developers to adapt GUI programs to try working under the prompt. But it’s not that simple. You still need to know “how to do it.” To jump between words, use Ctrl+ left or right.

Ctrl+a jumps to the beginning of the line,
Ctrl+e jumps to the end of the page.
Ctrl + u delete words in front of the current cursor
Ctrl + k – deletes the current text cursor at the back of the
Ctrl + Alt + w and d – delete operation for the current word, w to delete the character of the word ahead of the cursor, d is the character after the delete
Alt + Backsapce – deletes the current cursor behind the words,
if you remove errors, use the Ctrl + y to restore Ctrl + L to screen clearing operations

CTRL +a: move the cursor to the beginning of the line.
CTRL +b: move the cursor left one letter
CTRL +c: kill the current process.
CTRL +d: exit the current Shell.
CTRL +e: move cursor to end of line.
CTRL +h: delete the previous character of the cursor, same as the backspace key.
CTRL +k: clears the cursor to the end of the line.
CTRL +l: clear screen, equivalent to clear.
CTRL +r: search for previously typed commands. There will be a prompt to search bash’s history
CTRL +u based on the keyword you entered: clear everything from before the cursor to the beginning of the line.
CTRL +w: remove a word before the cursor
CTRL +t: swap two characters before the cursor position
CTRL +y: paste or restore the previous deletion
CTRL +d: delete the cursor’s letter; Note the difference between backspace and CTRL +h, which are the characters before deleting the cursor
, CTRL +f: move cursor to the right
, CTRL +z: move the current process to the background and restore using the command ‘fg’. For example, top-d1, then CTRL +z, go to the background, then fg, restore
esc combination
esc+d: a word after the cursor is removed
esc+f: skip a word to the right
esc+f: skip a word to the left
esc+t: two words before the cursor is switched.

Python random selects elements randomly from a collection

USES the python random module’s choice method to randomly select an element

foo = ['a', 'b', 'c', 'd', 'e']
from random import choice
print choice(foo)

USES the python random module’s sample function to randomly select a set of elements from the list

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
slice = random.sample(list, 5)  #从list中随机获取5个元素,作为一个片断返回  
print slice  
print list #原有序列并没有改变。  

The conversion between [Python] bytes and hex strings.

[Python] bytes and hex string conversion.
repeatedly played around with the assembly parsing and readable printing of code streams in several environments, always encountering hex string and bytes conversion, recorded here.

  1. on python 2.7.x (which is really hard to handle in older environments), the hex string is converted between bytes like this:
>>> a = 'aabbccddeeff'
>>> a_bytes = a.decode('hex')
>>> print(a_bytes)
b'\xaa\xbb\xcc\xdd\xee\xff'
>>> aa = a_bytes.encode('hex')
>>> print(aa)
aabbccddeeff
>>>
  1. in python 3 environment, because the implementation of string and bytes has changed significantly, this conversion can no longer be completed with encode/decode.

2.1 before python3.5, one way of doing this conversion was to look like this:

>>> a = 'aabbccddeeff'
>>> a_bytes = bytes.fromhex(a)
>>> print(a_bytes)
b'\xaa\xbb\xcc\xdd\xee\xff'
>>> aa = ''.join(['%02x' % b for b in a_bytes])
>>> print(aa)
aabbccddeeff
>>>

2.2 after python 3.5, you can do the following:

>>> a = 'aabbccddeeff'
>>> a_bytes = bytes.fromhex(a)
>>> print(a_bytes)
b'\xaa\xbb\xcc\xdd\xee\xff'
>>> aa = a_bytes.hex()
>>> print(aa)
aabbccddeeff
>>>
last
data = b'\x820\xb100\x03\xc3\xb4'
print('type(data) = ', type(data))
#type(data) =  <class 'bytes'>


lst = list(data)
print(lst)
print(type(lst[0]))
#[130, 48, 177, 48, 48, 3, 195, 180]
#<class 'int'>

tmp = data.hex()

print(type(tmp), tmp)
# <class 'str'> 8230b1303003c3b4

strr = '8230b1303003c3b4'

num = bytes.fromhex(strr)

print(type(num), num)
# <class 'bytes'> b'\x820\xb100\x03\xc3\xb4'

Idea startup project: main class not found or cannot be loaded

in the process of using IDEA, the service is often broken or restarted. Recently, when the service is interrupted and restarted, a startup error is encountered:

error: the main class

cannot be found or loaded

guess: 1, failed to compile;

try: menu -> Build – “Rebuild Prodject

result: the startup service still reports the same error

2, caching problem;

try: menu — “File–” Invalidate Caches/Restart select Invalidate and Restart or just Invalidate, clear the cache, and then Rebuild Project

results: start successfully, problem solving