Python 讀取數據基礎

需要用python進行數據整理,將raw data整理成CNN可以識別的形式.在整理數據的時候用的python語言.

  • 讀取整個文件夾下面的文件名
list = os.listdir("Kinfu_Toolbox1_dark/rgb_noseg")

然后這個list里面就是所有的文件名
如果想讀取某個文件路徑里面的文件名,或者最后一個文件夾的名字 用split命令

fpath, fname=os.path.split(filepath)

如果想讀取一個字符串里面的數字,(比如 "91818aik011", 讀出來是"91818011")

number=filter(str.isdigit, fname)

判斷number是不是空的

if number =="":
  print "no number in the file name"
f=open(txtpath) #讀取txt文件,把整個文件放進f里面
        lines= f.read().splitlines() #把整個文件放入一個list,這個方法比較好

        transf_str=lines[8]  #讀列表的第8行 transf_str 是一個string
        transf_list = [float(x) for x in transf_str.split()] 
        #convert the str into list
        transf_matrix=array(transf_list) #convert the list into array
        label[0,4:7]=transf_matrix #賦值給label
        #依次做上面的事情
        for index in range(4, 7): 
            rotation_str=lines[index]
            rotation_list = [float(x) for x in rotation_str.split()]
            rotation_matrix[index-4]=array(rotation_list)
        #print rotation_matrix 
        quation= mat2quat(rotation_matrix) #旋轉矩陣轉換成四元數
        label[0,:4]=quation

值得注意的是如何安裝第三方包 transforms3d

pip install transform3d

這樣 不會產生路徑找不到的問題.

寫HDF5 文件

with h5py.File('train.h5', 'w') as f:
    f['data'] = img_mid  #read image into a numpy darray
    f['label'] = label_mid
    f.close()

讀取HDF5 文件

f = h5py.File(filename, 'r') # read h5 file

label=f['label'] #'label' is the group in h5 now transfer it to the variable
print label.shape     #label now is a numpy darray
print type(label)  
print label[3,:]
img=f['data'][19,:,:,:]  #read another data which actually is a image
print img.shape
print img
cv2.imshow("4", img)

python opencv 讀取圖片 并顯示

img = cv2.imread(img_path, cv2.IMREAD_COLOR)
cv2.imshow("4", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

但是!!!!

這時候 img是個dnarray 如果你對他進行操作之后再讀數據

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容