Guide flip 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import cv2image = cv2.imread("demo.jpg" ) h_flip = cv2.flip(image, 1 ) cv2.imwrite("demo-h.jpg" , h_flip) v_flip = cv2.flip(image, 0 ) cv2.imwrite("demo-v.jpg" , v_flip) hv_flip = cv2.flip(image, -1 ) cv2.imwrite("demo-hv.jpg" , hv_flip)
rotate 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 def rotate_anti_90 (image ): image = cv2.transpose(image) image = cv2.flip(image, 0 ) return image def rotate_anti_180 (image ): image = cv2.flip(image, 0 ) image = cv2.flip(image, 1 ) return image def rotate_anti_270 (image ): image = cv2.transpose(image) image = cv2.flip(image, 1 ) return image def rotate (image, angle, center=None , scale=1.0 ): (h, w) = image.shape[:2 ] if center is None : center = (w / 2. , h / 2. ) M = cv2.getRotationMatrix2D(center, angle, scale) rotated = cv2.warpAffine(image, M, (w, h)) return rotated
compression 1 2 3 cv2.imwrite(full_image_path, image, [int ( cv2.IMWRITE_JPEG_QUALITY), 100 ])
get video info 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 import datetimeimport cv2from moviepy.editor import VideoFileClipimport numpy as npdef get_video_info (video_path ): cap = cv2.VideoCapture(video_path) if not cap.isOpened(): return frame_number = cap.get(cv2.CAP_PROP_FRAME_COUNT) h = int (cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) w = int (cap.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int (cap.get(cv2.CAP_PROP_FPS)) cap.release() print ("fps = " ,fps) print ("frame_number = " ,frame_number) size = (w,h) print ("size = " ,size) duration = int (frame_number / fps) print ("seconds=" ,duration) video_time = str (datetime.timedelta(seconds = duration)) print ("video_time=" ,video_time) print ("-----------------------using VideoFileClip------------------" ) clip = VideoFileClip(video_path) duration = clip.duration print ("video duration is " + str (duration) + " seconds" ) video_time = str (datetime.timedelta(seconds = int (duration))) print ("video_time=" ,video_time) def clip_video (): clip = VideoFileClip("1.mp4" ) starting_point = 120 end_point = 420 subclip = clip.subclip(starting_point, end_point) subclip.write_videofile("/path/to/new/video.mp4" )
numpy argmax numpy argmax for 2-dim and 3-dim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import numpy as nparray = np.array([ [1 ,2 ,3 ], [4 ,5 ,6 ], [9 ,8 ,7 ], [1 ,2 ,3 ], [10 ,1 ,2 ] ]) print ("array.shape=" ,array.shape)result1 = array.argmax(axis=0 ) result2 = array.argmax(axis=1 ) print (result1)print (result1.shape)print (result2)print (result2.shape)
output
('array.shape=', (5, 3))
[4 2 2]
(3,)
[2 2 0 2 0]
(5,)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 array2 = np.array([ [3 ,2 ,1 ], [6 ,5 ,4 ], [7 ,8 ,9 ], [1 ,2 ,3 ], [1 ,1 ,10 ] ]) image = np.array([ array,array2 ]) print ("image.shape=" ,image.shape)print (image)out = image.argmax(axis=0 ) print (out)print (out.shape)print (out.dtype)
output
('image.shape=', (2, 5, 3))
[[[ 1 2 3]
[ 4 5 6]
[ 9 8 7]
[ 1 2 3]
[10 1 2]]
[[ 3 2 1]
[ 6 5 4]
[ 7 8 9]
[ 1 2 3]
[ 1 1 10]]]
[[1 0 0]
[1 0 0]
[0 0 1]
[0 0 0]
[0 0 1]]
(5, 3)
int64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 output = np.load('output.npy' ) print ("output.shape=" ,output.shape) print ("output.dtype=" ,output.dtype)image = output[0 ] print ("image.shape=" ,image.shape) print ("image.dtype=" ,image.dtype)out = image.argmax(axis=0 ) print ("out.shape=" ,out.shape) print ("out.dtype=" ,out.dtype) print (out.min ())print (out.max ())
output
('output.shape=', (1, 2, 512, 512))
('output.dtype=', dtype('float32'))
('image.shape=', (2, 512, 512))
('image.dtype=', dtype('float32'))
('out.shape=', (512, 512))
('out.dtype=', dtype('int64'))
0
1
pandas quantile basic 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 """ for array of length n: 1. pos = 1 + (n-1)*p 2. get integer part and fraction part of pos: i, f 3. return a[i-1] + (a[i]-a[i-1])*f """ import mathdef quantile_p (data, p, method=1 ): data.sort() if method == 2 : pos = 1 + (len (data)-1 )*p else : pos = (len (data) + 1 )*p i = int (math.modf(pos)[1 ]) f = pos - i Q = data[i-1 ] + (data[i]-data[i-1 ])*f Q1 = quantile_p(data, 0.25 ) Q2 = quantile_p(data, 0.5 ) Q3 = quantile_p(data, 0.75 ) IQR = Q3 - Q1 return Q1, Q2, Q3, IQR
quantile 1 2 3 4 5 6 7 8 import pandas as pdimport numpy as npdf = pd.Series(np.array([6 , 47 , 49 , 15 , 42 , 41 , 7 , 39 , 43 , 40 , 36 ]) print (dt)print ('Q1:' , df.quantile(.25 ))print ('Q2:' , df.quantile(.5 ))print ('Q3:' , df.quantile(.75 ))
pandas
use method 2: pos = 1 + (n-1)*p
image to/from base64 string 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import jsonimport base64def get_base64_str_from_file (filepath ): with open (filepath, "rb" ) as f: bytes_content = f.read() bytes_64 = base64.b64encode(bytes_content) return bytes_64.decode('utf-8' ) def save_base64_str_to_file (str_base64, to_file ): bytes_64 = str_base64.encode('utf-8' ) bytes_content = base64.decodebytes(bytes_64) with open (to_file, "wb" ) as f: f.write(bytes_content) def test_base64 (): image_path = "images/1.jpg" str_base64 = get_base64_str_from_file(image_path) save_base64_str_to_file(str_base64, "images/2.jpg" ) print ("OK" ) if __name__ == "__main__" : test_base64()
output
OK
normal string to/from base64 string 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 def str_to_base64 (normal_str ): bytes_str = normal_str.encode('utf-8' ) bytes_64 = base64.b64encode(bytes_str) return bytes_64.decode('utf-8' ) def base64_to_str (base64_str ): bytes_64 = base64_str.encode('utf-8' ) bytes_content = base64.decodebytes(bytes_64) return bytes_content.decode('utf-8' ) def test_base64 (): normal_str = "Hello World !" str_base64 = str_to_base64(normal_str) normal_str2 = base64_to_str(str_base64) print ("normal_str = " ,normal_str) print ("str_base64 = " ,str_base64) print ("normal_str2 = " ,normal_str2) if __name__ == "__main__" : test_base64()
output
normal_str = Hello World !
str_base64 = SGVsbG8gV29ybGQgIQ==
normal_str2 = Hello World !
json loads and dumps 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import jsonstr_text='{"status":0,"msg":"成功"}' dict_json = json.loads(str_text) print (type (dict_json)) print (dict_json)str_pretty_result = json.dumps( dict_json, indent=4 , sort_keys=True , ensure_ascii=False ) print (type (str_pretty_result)) print (str_pretty_result)
output
<class 'dict'>
{'status': 0, 'msg': '成功'}
<class 'str'>
{
"msg": "成功",
"status": 0
}
str to dict dict to str
datetime 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 def demo_string_datetime (): datetime_str = '09/19/18 13:55:26' datetime_object = datetime.datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S' ) print (type (datetime_object)) print (datetime_object) datetime_str = '19910403' datetime_object = datetime.datetime.strptime(datetime_str, '%Y%m%d' ) print (type (datetime_object)) print (datetime_object) datetime_result_str = datetime_object.strftime('%Y%m%d' ) print (datetime_result_str)
simplekml 1 2 3 4 5 import simplekmlkml = simplekml.Kml() kml.newpoint(name="point a" , coords=[(18.432314 ,-33.988862 )]) kml.newpoint(name="point b" , coords=[(28.432314 ,-43.988862 )]) kml.save("1.kml" )
1.kml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?xml version="1.0" encoding="UTF-8" ?> <kml xmlns ="http://www.opengis.net/kml/2.2" xmlns:gx ="http://www.google.com/kml/ext/2.2" > <Document id ="1" > <Placemark id ="3" > <name > point a</name > <Point id ="2" > <coordinates > 18.432314,-33.988862,0.0</coordinates > </Point > </Placemark > <Placemark id ="5" > <name > point b</name > <Point id ="4" > <coordinates > 28.432314,-43.988862,0.0</coordinates > </Point > </Placemark > </Document > </kml >
python requests install pip install requests
conda install requests
usage >>> requests.get('https://httpbin.org/get')
>>> requests.post('https://httpbin.org/post', data={'key':'value'})
>>> requests.put('https://httpbin.org/put', data={'key':'value'})
>>> requests.delete('https://httpbin.org/delete')
>>> requests.head('https://httpbin.org/get')
>>> requests.patch('https://httpbin.org/patch', data={'key':'value'})
>>> requests.options('https://httpbin.org/get')
code example 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 import requestsimport jsondata = {"name" :"admin" , "password" :"21232f297a57a5a743894a0e4a801fc3" } r = requests.post( "127.0.0.1/api/login" , headers={"Accept" : "application/json" , "Content-Type" : "application/json" }, data=json.dumps(data) ) print (r.text)r = requests.post( "127.0.0.1/api/login" , headers={"Accept" : "application/json" , "Content-Type" : "application/json" }, json=data ) print (r.text)session_id = "[email protected] " myheaders={"Accept" : "application/json" , "Content-Type" : "application/json" , "session_id" :session_id} r=requests.get( "127.0.0.1/api/book" , headers=myheaders ) print (r.text)
requests-html requests-html for human
install
pip install requests-html
usage
1 2 3 4 5 >>> from requests_html import HTMLSession>>> session = HTMLSession()>>> r = session.get('https://python.org/' )>>> r.text>>> r.html.find('title' , first=True ).text
selenium chromedriver versions
Selenium – version 3.11.0
Chrome Browser – version 77
ChromeDriver – version 77
steps
download and install chrome browser 77
download ChromeDriver for Chrome Browser 77
install and check version
commands
sudo cp chromedriver /usr/local/bin
chromedriver -v
ChromeDriver 77.0.3865.40
smote using smote to over sampling datasets
install
pip install smote_variants
pip install imbalanced_databases
class member vs instance member 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 import timeclass Info (object ): rects = [] counter = 0 def __init__ (self ): self .new_rects = [] self .new_counter = 0 def func (): it = Info() return it def test1 (): info = func() info.rects.extend([1 ,2 ,3 ,4 ,5 ]) info.counter += 1 info.new_rects.extend([1 ,2 ,3 ,4 ,5 ]) info.new_counter += 1 print ("rect size" , len (info.rects),len (info.new_rects)) print ("counter" , info.counter,info.new_counter) print ("--------------------------" ) info2 = func() info2.rects.extend([1 ,2 ,3 ,4 ,5 ]) info2.counter += 1 info2.new_rects.extend([1 ,2 ,3 ,4 ,5 ]) info2.new_counter += 1 print ("rect size" , len (info2.rects),len (info2.new_rects)) print ("counter" , info2.counter,info2.new_counter) print ("--------------------------" ) if __name__ == "__main__" : test1() """ rect size 5 5 counter 1 1 -------------------------- rect size 10 5 counter 1 1 -------------------------- """
pyyaml install
pip install pyyaml
pip freeze > requirements.txt
usage cfg.yaml
1 2 3 4 5 --- debug: true input_dir: "./input/" output_dir: "./output/"
code
1 2 3 4 5 6 7 8 9 10 import yaml def load_cfg (cfg_path='./cfg.yaml' ): cfg = None if os.path.exists(cfg_path): cfg = yaml.load(open (cfg_path)) input_dir = cfg.get("input_dir" ) else : print ("{} not exist" .format (cfg_path)) return cfg
cupy
CuPy: NumPy-like API accelerated with CUDA. CuPy: numpy on GPU
install (For CUDA 8.0)
% pip install cupy-cuda80
(For CUDA 9.0)
% pip install cupy-cuda90
(For CUDA 9.1)
% pip install cupy-cuda91
(For CUDA 9.2)
% pip install cupy-cuda92
(For CUDA 10.0)
% pip install cupy-cuda100
(For CUDA 10.1)
% pip install cupy-cuda101
(Install CuPy from source)
% pip install cupy
usage 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 >>> import cupy as cp>>> x = cp.arange(6 ).reshape(2 , 3 ).astype('f' )>>> xarray([[ 0. , 1. , 2. ], [ 3. , 4. , 5. ]], dtype=float32) >>> x.sum (axis=1 )array([ 3. , 12. ], dtype=float32) >>> x = cp.arange(6 , dtype='f' ).reshape(2 , 3 )>>> y = cp.arange(3 , dtype='f' )>>> kernel = cp.ElementwiseKernel(... 'float32 x, float32 y' , 'float32 z' ,... '''if (x - 2 > y) { ... z = x * y;... } else {... z = x + y;... }''' , 'my_kernel' )>>> kernel(x, y)array([[ 0. , 2. , 4. ], [ 0. , 4. , 10. ]], dtype=float32)
SORT SORT: A Simple, Online and Realtime Tracker based on Kalman
code
1 2 3 4 5 6 7 8 9 10 11 12 13 from sort import *mot_tracker = Sort() ... track_bbs_ids = mot_tracker.update(detections) ...
Reference
History