python 语法

1.遍历目录 os.walk()

import os
for root, dirs, files in os.walk(path):
    for name in files:
        print(os.path.join(root, name))
    for name in dirs:
        print(os.path.join(root, name))

2.随机打乱列表:random

import random

random.seed(666)
random.shuffle(imgs_list)

3. glob 文件名模式匹配,不用遍历整个目录判断每个文件是不是符合。

import glob
for name in glob.glob('dir/*.png'):
    print (name)

##or
imgs_list = glob.glob(os.path.join(root, sDir)+'/*.png')

4.文件夹不存在就创建 makedirs

#os.makedirs会递归的建立输入的路径,即使是上层的路径不存在,它也会建立这个路径,而os.mkdir父级路径不存在,那么就会报错
def makedir(new_dir):
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)

5.os.path.split()与os.path.splitext()

*os.path.split()返回文件的路径和文件名
*os.path.splitext()将文件名和扩展名分开

fname,fename=os.path.split("/media/data_2/Data/cat.png")
#fname = “/media/data_2/Data”
#fename=“cat.png”

fname,fename=os.path.splitext("("/media/data_2/Data/cat.png")")
#fname = “/media/data_2/Data/cat”
#fename=“.png”
for root, dirs, files in os.walk(dataset_dir):
        for sDir in dirs:
            imgs_list = glob.glob(os.path.join(root, sDir)+'/*.png')
            random.seed(666)
            random.shuffle(imgs_list)
            imgs_num = len(imgs_list)

            train_point = int(imgs_num * train_per)
            valid_point = int(imgs_num * (train_per + valid_per))

            for i in range(imgs_num):
                if i < train_point:
                    out_dir = train_dir + sDir + '/'
                elif i < valid_point:
                    out_dir = valid_dir + sDir + '/'
                else:
                    out_dir = test_dir + sDir + '/'

                makedir(out_dir)
                out_path = out_dir + os.path.split(imgs_list[i])[-1]
                shutil.copy(imgs_list[i], out_path)

6.创建图像

B_img = np.zeros(mask.shape, np.uint8)
img = np.zeros(img.shape[0:2], dtype='uint8')

7. 掩膜mask cv2.merge cv2.addWeighted R_img[mask == i] = r

img 是原图
mask 是原图上面对于的字条掩膜,每个字条像素是1,2,3累加计数的,其他区域像素为0

for i in range(bboxes.shape[0]):
        cv2.drawContours(gt_text, [bboxes[i]], -1, i + 1, -1)

这样得到mask的
下面函数是要把原图上面显示掩膜。
用到了 R_img[mask == i] ; mask==i表示mask中像素值为i的坐标,加上 R_img[mask == i]表示R_img中相应的位置

def draw_mask(mask, img):
    import random
    # mask = cv2.imread(mask_dir + item)
    # b, g, r = cv2.split(mask)
    if type(mask) == type(None):
        return mask
    #if mask.shape[2] > 1:
    #   mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    R_img, G_img, B_img = np.zeros(mask.shape, np.uint8), np.zeros(mask.shape, np.uint8), np.zeros(mask.shape, np.uint8)
    label_num = np.max(mask) + 1
    # print(label_num)
    for i in range(1, label_num):
        r = random.random()*255
        g = random.random()*255
        b = random.random()*255
        R_img[mask == i] = r
        G_img[mask == i] = g
        B_img[mask == i] = b

    check = cv2.merge([R_img, G_img, B_img])
    dst=cv2.addWeighted(img,0.7,check,0.3,0)  
    return dst

8.读写文本

一个文件夹下面的文本,img_1.txt,...,img_10000.txt,每个文本里面的内容格式都是像下面的,需要把###换成LOVE

48,323,798,320,795,370,48,375,###
43,42,890,30,890,90,44,102,###
503,207,899,203,900,265,503,269,###
46,209,349,209,349,268,46,268,###
38,272,454,272,454,319,38,319,###
713,516,839,516,839,573,713,573,###
import os
root = "/media/data_1/2019_project_test/PSENet/data/ICDAR2015/Challenge4/ch4_training_localization_transcription_gt/"
save_path = "/media/data_1/2019_project_test/PSENet/data/ICDAR2015/Challenge4/save/"
file_list = os.listdir(root)
cnt = 0
for file_name in file_list:
    path_file = root + file_name
    cnt += 1
    print("cnt=%d,name=%s"%(cnt,file_name))
    with open(save_path + file_name,'w') as fw:
        with open(path_file, 'r') as fread:
            for line in fread.readlines():
                line = line.replace("###","LOVE")
                fw.write(line)

总结:以后读文本就用这个:

      with open(path_file, 'r') as fread:
            for line in fread.readlines():  
                line = line.strip() 
                ...

9.python list 自定义排序例子

eg1:

In [53]: L = [('b',6),('a',1),('c',3),('d',4)] 
In [54]: L.sort(key=lambda x:x[1])
In [56]: L
 Out[56]: [('a', 1), ('c', 3), ('d', 4), ('b', 6)]

eg2:班级学生的成绩信息集合:字段包括学号,姓名,语文成绩,数学成绩

item=[[101,'张三',80,75],[102,'李四',80,85],[103,'王五',63,90],[104,'小明',95,75],[105,'小红',75,65]]

这就是一种列表中嵌套列表的方法,假如我们要对语文成绩进行排序:

item=[[101,'张三',80,75],[102,'李四',80,85],[103,'王五',63,90],[104,'小明',95,75],[105,'小红',75,65]] 
item.sort(key=lambda x:x[2], reverse=True)
 print(item)
#[[104, '小明', 95, 75], [101, '张三', 80, 75], [102, '李四', 80, 85], [105, '小红', 75, 65], [103, '王五', 63, 90]]

如果语文成绩相同则按数学成绩的高低排序:

item=[[101,'张三',80,75],[102,'李四',80,85],[103,'王五',63,90],[104,'小明',95,75],[105,'小红',75,65]] 
item.sort(key=lambda x:(x[2],x[3]), reverse=True)
print(item)
#可以看到张三和李四的位置互换了 
#[[104, '小明', 95, 75], [102, '李四', 80, 85], [101, '张三', 80, 75], [105, '小红', 75, 65], [103, '王五', 63, 90]]

那如果语文成绩和数学成绩都相同,则按学号升序排:
就可以这样做(在学号字段前面加个‘-’号):

item=[[101,'张三',80,75],[102,'李四',80,85],[103,'王五',63,90],[104,'小明',95,75],[105,'小红',75,65]] 
item.sort(key=lambda x:(x[2],x[3],-x[0]), reverse=True)
print(item)
#[[104, '小明', 95, 75], [102, '李四', 80, 85], [101, '张三', 80, 75], [105, '小红', 75, 65], [103, '王五', 63, 90]]

10.python opencv 像素操作

img = cv2.imread('image0.jpg',1)
(b,g,r) = img[100,100]


for i in range(im.height):
 for j in range(im.width):
  im[i,j]


 height = image.shape[0] 
width = image.shape[1] 
channels = image.shape[2]
 print("width: %s  height: %s  channels: %s"%(width, height, channels))
 for row in range(height): 
     for col in range(width):
         for c in range(channels):
                pv = image[row , col, c] #获取每个像素点的每个通道的数值 image[row, col, c]=255 - pv 

11.numpy reshape

import numpy as np
a = np.array([1,2,3,4,5,6,7,8])

#转换成2D数组
print('*2D*********************************************')
b = a.reshape((2,4))
print(b)
print('*2D*********************************************\n\n')

#转换成3D数组
print('*3D*********************************************')
c = a.reshape((2,2,2))
print(c)
print('*3D*********************************************')
*2D*********************************************
[[1 2 3 4]
 [5 6 7 8]]
*2D*********************************************


*3D*********************************************
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
*3D*********************************************

10. br = np.min(np.where(a>10),axis=0)-1 啥意思

一一道来:

>>> a = np.arange(16).reshape(4,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> np.where(a>10)
(array([2, 3, 3, 3, 3]), array([3, 0, 1, 2, 3]))
>>> np.min(np.where(a>10),axis=1)
array([2, 0])
>>> np.min(np.where(a>10),axis=0)
array([2, 0, 1, 2, 3])
>>> np.min(np.where(a>10),axis=0)-1
array([ 1, -1,  0,  1,  2])
>>> br = np.min(np.where(a>10),axis=0)-1
>>> print(br)
[ 1 -1  0  1  2]

np.where(a>10) #返回的二维坐标,两个元祖,如果是三维的,就是三个元祖
np.min(np.where(a>10),axis=0) #axis=0 表示纵向 axis=1表示横向 0,1其实和shape有关,shape[0]代表纵 shape[1]代表横向
np.min(np.where(a>10),axis=0)-1 #广播 所有元素都减去1

11.返回上一级目录并先建文件夹

root_img = "/media/data_2/everyday/0801/5555/mark/"
root_pt_txt = "/media/data_2/everyday/0801/5555/txt/"
save_dir = os.path.dirname(os.path.dirname(root_img)) + '/rot/'
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

12.打印list

print("normMean = {}".format(means))
print("normStd = {}".format(stdevs))
##normMean = [0.49680823, 0.48622987, 0.44980356]
##normStd = [0.24765104, 0.24397221, 0.26272318]

13. neg_score = score[gt_text <= 0.5]

neg_score = score[gt_text <= 0.5]
score 是640*640
gt_text 是640*640
neg_score 是(396390,)

自己写的例子如下:
>>> import numpy as np
>>> a = np.arange(9)
>>> b = a.reshape(3,3)
>>> print(a)
[0 1 2 3 4 5 6 7 8]
>>> print(b)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
#取对角线
>>> c = np.array([True,False,False],[False,True,False],[False,False,True])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: only 2 non-keyword arguments accepted
>>> c = np.array([[True,False,False],[False,True,False],[False,False,True]])
>>> d = b[c]
>>> print (d)
[0 4 8]
>>> 

## 可以看出d就是一维的了

14.数据和标签同时一一对应随机打断 from sklearn.utils import shuffle

>>> import numpy as np
>>> from sklearn.utils import shuffle
>>> X = np.array([[1., 0.], [2., 1.], [0., 0.]])
>>> y = np.array([0, 1, 2])
>>> X_1,Y_1 = shuffle(X,y)
>>> print X_1,Y_1
[[2. 1.]
 [1. 0.]
 [0. 0.]] [1 0 2]

>>> import numpy as np
>>> from sklearn.utils import shuffle
>>> X = np.array([[1., 0.], [2., 1.], [0., 0.]])
>>> y = np.array([0, 1, 2])
>>> X1,y1 = shuffle(X,y)
>>> print(X1,y1)
(array([[1., 0.],
       [2., 1.],
       [0., 0.]]), array([0, 1, 2]))
>>> X1,y1 = shuffle(X,y)
>>> print(X1,y1)
(array([[2., 1.],
       [0., 0.],
       [1., 0.]]), array([1, 2, 0]))
>>> X1,y1 = shuffle(X,y)
>>> print(X1,y1)
(array([[0., 0.],
       [2., 1.],
       [1., 0.]]), array([2, 1, 0]))

15. a[:,0]

x[m,n]是通过numpy库引用数组或矩阵中的某一段数据集的一种写法,
m代表第m维,n代表m维中取第几段特征数据。
通常用法:
x[:,n]或者x[n,:]
x[:,n]表示在全部数组(维)中取第n个数据,直观来说,x[:,n]就是取所有集合的第n个数据,

>>> import numpy as np
>>> X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]])
>>> print(X[:,0])
[ 0  2  4  6  8 10 12 14 16 18]
>>> print(X[:,1])
[ 1  3  5  7  9 11 13 15 17 19]

16. python [...]用法

还有点儿迷糊
1.

import numpy as np
a = np.array([[[1,2,21],[3,4,34]],[[5,6,56],[7,8,78]]])
print('a==',a)
print('a.shape:',a.shape)

b = a[...,0:2]
print('b :',b)
print('shape.b:',b.shape)

c = a[...,None]
print('c :',c)
print('shape.c:',c.shape)

输出:

a== [[[ 1  2 21]
  [ 3  4 34]]

 [[ 5  6 56]
  [ 7  8 78]]]
a.shape: (2, 2, 3)
b : [[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
shape.b: (2, 2, 2)
c : [[[[ 1]
   [ 2]
   [21]]

  [[ 3]
   [ 4]
   [34]]]


 [[[ 5]
   [ 6]
   [56]]

  [[ 7]
   [ 8]
   [78]]]]
shape.c: (2, 2, 3, 1)
import numpy as np
a = np.array([[[1,2,21],[3,4,34]],[[5,6,56],[7,8,78]]])
print('a==',a)
print('a.shape:',a.shape)

b = a[...,1]
print('b :',b)
print('shape.b:',b.shape)
a== [[[ 1  2 21]
  [ 3  4 34]]

 [[ 5  6 56]
  [ 7  8 78]]]
a.shape: (2, 2, 3)
b : [[2 4]
 [6 8]]
shape.b: (2, 2)

17. 查看是什么类型__class__

>>> age = 35
>>> age.__class__
<type 'int'>

>>> name = 'bob'
>>> name.__class__
<type 'str'>

>>> def foo(): pass
>>> foo.__class__
<type 'function'>

>>> class Bar(object): pass
>>> b = Bar()
>>> b.__class__
<class '__main__.Bar'>

18.with-as表达式语法

with-as表达式语法需要python中class的支持:

class TEST:
    def __enter__ (self):
        do ...
        return somethings

    def __exit__ (self, type, value, traceback):
        do ... (finally)

当执行with-as时(with TEST() as t),首先调用__enter__函数,
然后把该函数的return值返回给as后面指定的变量。之后执行执行正常代码块,
最终,流程正常完毕或有异常状况,都会执行__exit__函数。
示例:

class Pool:
    def __init__(self):
        self.pool = multiprocessing.Pool()

    def __enter__(self):
        return self.pool

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.pool.close()
        self.pool.join()


if __name__ == '__main__':
    TASK_MODE = [None, 'Thread', 'Process']
    with Pool() as pool:
        summary(
            lambda: summary(lambda: invoke(aug, 10, 'Process', True), n_test=10, name='Aug[%d]' % COUNT),
            name='Total'
        )

19.python 读写文本

import os

path_txt = '/data_2/everyday/1.txt'
with open("/data_2/everyday/2.txt",'w') as fw:
    with open(path_txt,'r') as fr:
        for line in fr.readlines():
            line = line.strip()
            print(line)
            v_str = line.split("	")
            label = v_str[0]
            new_str = label + " 1:" + v_str[1] + " 2:" + v_str[2] + " 3:" + v_str[3] + " 4:" + v_str[4] +" 5:" + v_str[5]+" 6:" + v_str[6]+" 7:" + v_str[7]+" 8:" + v_str[8]
            fw.write(new_str + '\n')

20.Python改变工作目录

import os
os.getcwd()   #取得当前工作目录
os.chdir(r'目标目录')   # os.chdir(r'd:\learnpy')

21.列表内循环

a = ['2.0','4.0','3.0','8.0','9.0']
b = [int(x.replace(".0","")) for x in a]
print(a)
print(b)

打印如下:

['2.0', '4.0', '3.0', '8.0', '9.0']
[2, 4, 3, 8, 9]
posted @ 2019-06-14 17:20  无左无右  阅读(291)  评论(0编辑  收藏  举报