PyOpenCV 将Fashion数据集由二进制转换为jpg

# -*- coding: utf-8 -*-


import cv2
import numpy as np
import os


img_path = 'C:/Users/Downloads/fashion_28_28/train-images-idx3-ubyte/train-images-idx3-ubyte'
label_path = 'C:/Users/Downloads/fashion_28_28/train-labels-idx1-ubyte/train-labels-idx1-ubyte'
save_path = 'E:/pycharm/data/Fashion_28_28/train'
img_path2 = 'C:/Users/Downloads/fashion_28_28/t10k-images-idx3-ubyte'
label_path2 = 'C:/Users/Downloads/fashion_28_28/t10k-labels-idx1-ubyte'
save_path2 = 'E:/pycharm/data/Fashion_28_28/test'

classes = {0: 'T-shirt_or_top',
           1: 'Trouser',
           2: 'Pullover',
           3: 'Dress',
           4: 'Coat',
           5: 'Sandal',
           6: 'Shirt',
           7: 'Sneaker',
           8: 'Bag',
           9: 'Ankle boot'}


def save(img_path, label_path, savepath, num):
    f = open(img_path, 'rb')
    la_f = open(label_path, 'rb')
    la_f.read(8)
    f.read(16)
    dict = {}
    for n in range(num):
        image = []
        for i in range(28 * 28):
            image.append(ord(f.read(1)))
        image = np.array(image).reshape(28, 28)
        name = classes[ord(la_f.read(1))]
        filepath = os.path.join(savepath, name)
        if not os.path.isdir(filepath):
            os.makedirs(filepath)
        if name not in dict:
            dict[name] = 1
        else:
            dict[name] += 1
        jpg = str(dict[name]) + '.jpg'
        save_path = os.path.join(filepath, jpg)
        cv2.imwrite(save_path, image)
    la_f.close()
    f.close()


save(img_path, label_path, save_path, num=60000)
save(img_path2, label_path2, save_path2, num=10000)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值