在处理数据,绘图,批量处理文件时,python能大大提高工作的效率,这篇文章就记录一下在各种场景下常用的一些功能实现。
批量复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import osimport shutilsource_dir = "C:/Users/xxx/source" target_dir = "C:/Users/xxx/target" for root, dirs, files in os.walk(source_dir): for file in files: file_path = os.path.join(root, file) shutil.copy(file_path, target_dir) print ("All files have been copied or moved to the target directory." )
显示obj模型 1 2 3 4 5 6 7 8 9 10 11 import open3d as o3dmesh = o3d.io.read_triangle_mesh("D:/WORK/DTact/obj_gather/00000003_1ffb81a71e5b402e966b9341_trimesh_002.obj" ) mesh.compute_vertex_normals() o3d.visualization.draw_geometries([mesh])
数据可视化 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 import osimport numpy as npimport matplotlib.pyplot as pltimport seaborn as snsimport pandas as pdfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlibh=0.04494988870551895 data_list = np.load("./6d_force/food/food.npy" ) force_x = [] force_y = [] force_z = [] torque_x = [] torque_y = [] torque_z = [] for data in data_list: force_x.append(data[0 ]) force_y.append(data[1 ]) force_z.append(data[2 ]) torque_x.append(data[3 ]) torque_y.append(data[4 ]) torque_z.append(data[5 ]) x = [] y = [] center_list = np.load("./image/food/food.npy" ) for center in center_list: x.append(center[0 ]) y.append(center[1 ]) My_fx = [] Mx_fy = [] scale_x = 400 scale_y = 400 bias_x = 1 bias_y = 0.5 for i in range (len (x)): x[i] = x[i]/scale_x-bias_x y[i] = y[i]/scale_y-bias_y Mx_fy.append(torque_x[i] - force_z[i]*y[i]) My_fx.append(torque_y[i] + force_z[i]*x[i]) sns.jointplot(x=Mx_fy, y=force_y, kind='kde' ) plt.show() sns.set (style="whitegrid" ) tips = sns.load_dataset("tips" ) g = sns.jointplot(x=force_x, y=force_y, kind='kde' , color='purple' ) g.plot_joint(sns.kdeplot,fill=True ) plt.show()
需要注意的一点是matplotlib的版本,建议更新到最新版。我一开始下载的旧版结果一直报错,后来才发现是库的bug
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 import osimport cv2import numpy as npfrom matplotlib import pyplot as pltimg_list = [] tresh_list = [] center_list = [] for subfolder in os.listdir("./cliber/geometry" ): subfolder_path = os.path.join("./cliber/geometry" , subfolder) for file in os.listdir(subfolder_path): file_path = os.path.join(subfolder_path, file) img = cv2.imread(file_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 150 , 255 , cv2.THRESH_BINARY_INV) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) filted_contours = [] centroids = [] areas = [] for contour in contours: area = cv2.contourArea(contour) if area > 0 : filted_contours.append(contour) M = cv2.moments(contour) cx = int (M['m10' ]/M['m00' ]) cy = int (M['m01' ]/M['m00' ]) centroids.append([cx, cy]) areas.append(area) if len (areas) == 0 : continue centroids = np.array(centroids) areas = np.array(areas) Cx = np.sum (centroids[:,0 ]*areas)/np.sum (areas) Cy = np.sum (centroids[:,1 ]*areas)/np.sum (areas) Cx = int (Cx) Cy = int (Cy) center_list.append([Cx, Cy]) np.save("./cliber/geometry/center.npy" , center_list)