ubuntu & python & opencv 实用小技巧小结

ubuntu

  • tmux 真的好用:

  • 实用教程: https://wdxtub.com/2016/03/30/tmux-guide/

  • ctrl + b + [ : 可移动光标

  • C++ char 和 string 转换

  • SSH 挂载远程的 Linux 文件系统或者目录:

  • ubuntu 挂载的另一种方法:

    • 卸载之前的挂载:sudo umount nonv(挂载目录)
      
    • 挂载命令:sudo mount --bind /volume4/HDMP4/nonv_10.209.37.43_vol2/(要挂载的原地址) ./nonvehicle (需要挂载的目的地址)
  • ls -lR|grep “^-”| wc -l :快速统计某个文件夹下的所有文件的个数

  • ls -l|grep “^d”| wc -l : 统计某个文件夹下文件夹的个数

  • ps :查看进程使用情况

  • kill - 9 23365(进程号):杀死不需要的进程

  • htop: 查看显存使用情况

  • nvidia-smi: 查看 GPU 使用情况

  • watch -n 1 nvidia-smi : 每秒刷新一次 GPU 使用情况

  • [git 分支管理]
    (https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840038939c291467cc7c747b1810aab2fb8863508000): 维护代码版本神器

    • 重复修改保存一些文件比较很乱,git 管理起来就很方便 ^ _ ^
    • git add : 添加要更新的文件
    • git status : 查看仓库当前状态
    • git commit -m “本次推送描述,方便以后回退历史版本”
    • git log:查看历史推送
    • git reset --hard 937d5b: 回退到某个历史版本(版本号的前几位~唯一就行)
    • git reflog:查看命令历史(以便回到旧版本后再返回到未来的某个版本)
  • chmod u+x new_run.sh : 给 .sh 文件执行权限

  • 显示文件前10行:head -n 10 test.txt

  • 显示文件后10行: tail -n 10 test.txt

caffe-ssd

opencv

python

  • numpy:

    • np.sum(): 求和(np.sum(a,axis = 1) 对行求和,axis = 0: 对列求和, np.amin,np.amax 同)

    • np.square():求平方

    • np.sqrt(): 求方差

    • np.amin():求最小值

    • np.amax():求最大值

    • np.exp(-x) : e^(-x)

    • np.unique(A): 对于一维数组或者列表,unique函数去除其中重复的元素,并按元素由大到小返回一个新的无元素重复的元组或者列表

    • np.loadtxt() : 可把 .txt 文档以 list 取出(可转换数据类型 np.loadtxt( , int))

    • np.savetxt(): 可把 list 直接存储为 .txt

os

  • os.path.exists(new_path) : 判断文件是否存在

  • os.makedirs(new_path) :创建新文件,会递归创建文件夹即使上一个文件不存在

random

  • random.uniform(x, y) 方法将随机生成一个实数,它在 [x,y] 范围内

其他

  • Python 离线安装 packges:

  • step1 打包: sudo pip install --download /home/deepglint/anaconda2/lib/python2.7/site-packages/ scikit-learn

  • step2 安装:pip install --no-index --find-links="/tmp/tranferred_packages" scikit-learn-0.19.2.tar.gz

  • sys.path.insert(): python 指定包路径

  • shutil.rmtree(): 删除文件夹

  • tqdm: 显示进度条

  • python 如果在函数里修改全局变量不会生效? 变量名前加上 global 关键字

  • python 实用小技巧

    • 下面附上实现代码
import cv2
import os
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
%matplotlib inline
root_path = '/media/deepglint/Data/deep_sort/cluster/bbox_frames'
ips = [
    d for d in os.listdir(root_path) # os.listdir() 对文件进行迭代操作
    if os.path.isdir(os.path.join(root_path, d))
]
for ip in ips:
    new_id = -1
    all_feat = []
    print('Extract features from ip: {}'.format(ip))
    ip_dir_path = os.path.join(root_path, ip) #os.path.join() 路径拼接
    print(ip_dir_path)
    datas = [
        d for d in os.listdir(ip_dir_path)
        if os.path.isdir(os.path.join(ip_dir_path, d))
    ]
    for data in datas:
        extract_path = os.path.join(os.path.join(os.path.join(ip_dir_path,data), \
                            'extract_feat'),'feat4cross.txt')
        
        new_feat = []
        f_id = -1
        with open(extract_path, 'r') as f: # 'r' : 读
            for line in f.readlines(): # 按行读取
                new_feat.append(line.strip().split(' ')) # .strip() 去掉行末的空格、换行符等
        for i in new_feat:                               # .split(‘ ’) 以空格分隔读取内容
            if f_id != i[1]:
                new_id += 1
                f_id = i[1]
            i[1] = str(new_id)
            all_feat.append(i)
    with open(os.path.join(ip_dir_path,'all_feat.txt'), 'w') as f: # ‘w’: 写
        for i in all_feat:                                         # 'a': 追加
            f.write(' '.join(i)) # ' '.join(i) 把对象 i 里的元素以 空格(‘ ’: 可把空格替换为 ',' 等) 分隔
            f.write('\n')
import os
root = './nonv'
num = -1
line = []
files_path = './insightface.list'
for i in os.listdir(root):
    id_path = os.path.join(root, i)
    num += 1
    for img in os.listdir(id_path):
        img_path = os.path.join(id_path, img)
        line.append('\t'.join([str(1), img_path, str(num)]))
        
with open(files_path, 'w') as f:
    for i in line:
        f.write(i)
        f.write('\n')

版权声明:本文为WYK1823376647原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。