Python 讀取數(shù)據(jù)基礎(chǔ)

需要用python進(jìn)行數(shù)據(jù)整理,將raw data整理成CNN可以識(shí)別的形式.在整理數(shù)據(jù)的時(shí)候用的python語(yǔ)言.

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

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

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

如果想讀取一個(gè)字符串里面的數(shù)字,(比如 "91818aik011", 讀出來(lái)是"91818011")

number=filter(str.isdigit, fname)

判斷number是不是空的

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

        transf_str=lines[8]  #讀列表的第8行 transf_str 是一個(gè)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) #旋轉(zhuǎn)矩陣轉(zhuǎn)換成四元數(shù)
        label[0,:4]=quation

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

pip install transform3d

這樣 不會(huì)產(chǎn)生路徑找不到的問題.

寫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()

但是!!!!

這時(shí)候 img是個(gè)dnarray 如果你對(duì)他進(jìn)行操作之后再讀數(shù)據(jù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容