赞
踩
import numpy as np
import cv2
import zipfile
if __name__ == '__main__':
path = 'D:\\DeepBlue\\samples\\celeba\\Eval\\Img\\img_ali.gn_cel.eba.zip' # 解压路径
ca = CelebA()
cv2.imshow(ca.imgs[1000])
cv2.waitKey()
class CelebA:
def __init__(self,path):
self.imgs = []
with zipfile.ZipFile(path) as zf: # 解压文件
for info in zf.filelist:
if info.is_dir(): continue # is_die(检查是否是目录)————>bool
img = zf.read(info.filename)
img = np.frombuffer(img, np.uint8) # frombuffer 收集数据,uint8:将收集的数据转制为八位的数组
img = cv2.imdecode(img, 1) # cv2.imdecode:解码成图,1是彩图,2是灰度图
name = info.filename[info.filename.rfind('/')+1:]
self.imgs.append(img)
if len(self.imgs) % 10000 == 0:
print('Read %d imgs' % len(self.imgs))
print('Read %d imgs from %s successfully!' % (len(self.imgs),path),flush=True) #flush参数主要是刷新, 默认flush = False,不刷新,True则反之
完整代码:
import zipfile import numpy as np import cv2 class CelebA: def __init__(self,path): self.imgs = [] with zipfile.ZipFile(path) as zf: # 解压文件 for info in zf.filelist: if info.is_dir(): continue# is_die(检查是否是目录)————>bool img = zf.read(info.filename) img = np.frombuffer(img, np.uint8) # frombuffer 收集数据,uint8:将收集的数据转制为八位的数组 img = cv2.imdecode(img, 1) # cv2.imdecode:解码成图,1是彩图,2是灰度图 name = info.filename[info.filename.rfind('/')+1:] self.imgs.append(img) if len(self.imgs) % 10000 == 0: print('Read %d imgs' % len(self.imgs)) print('Read %d imgs from %s successfully!' % (len(self.imgs),path),flush=True) #flush参数主要是刷新, 默认flush = False,不刷新,True则反之。 if __name__ == '__main__': path = 'D:\\DeepBlue\\samples\\celeba\\Eval\\Img\\img_ali.gn_cel.eba.zip' # 解压路径 ca = CelebA() cv2.imshow(ca.imgs[1000]) cv2.waitKey()
结果如图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。