<jupyter_start><jupyter_text>Leitura de uma base de imagens na forma* base/    * treino/        * classes/...            * imagens/...    * teste/        * classes/...            * imagens/... Será salva no formato[matriz, numero da classe, nome da pasta onde a imagem esta salva (nome da classe)]<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
import pickle
import os
import pathlib
DATADIR = "../bases/dados/imagens/cifar10/train"

DATADIR = pathlib.Path(DATADIR)

IMG_SIZE = 2#'original'#Tamanho mxm e original

formato = 'png'

nome_saida = "cifar10-train-3-2x2"

local_saida = "../bases/pickle/imagens/cifar10/"
CATEGORIES = os.listdir(DATADIR)
CATEGORIES.sort()

try:
    CATEGORIES.remove('.ipynb_checkpoints')
except:
    pass
print(CATEGORIES)
SIZE_OF_DATASET = len(list(DATADIR.glob('*/*.'+formato)))

print("Number of training images: ",SIZE_OF_DATASET)
def prep_data(DATA_DIR, CATEGORIES, IMG_SIZE):
    data = []
    for category in CATEGORIES:
        i=0
        path = os.path.join(DATA_DIR,category)
        class_num = CATEGORIES.index(category)#############

        for img in tqdm(os.listdir(path)):

#            img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)
            img_array = cv2.imread(os.path.join(path,img) ,cv2.cv2.IMREAD_COLOR)
            
            if IMG_SIZE == 'original':
                new_array = img_array
            else:
                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
            
            data.append([new_array, class_num, category])
            i=i+1
            if i >= 3:
                break

        plt.figure(figsize=(1,1))
        plt.imshow(new_array, cmap='gray')
        plt.show()
    return data
data = prep_data(DATADIR, CATEGORIES, IMG_SIZE)
print('Number of images: ',len(data))
plt.figure(figsize=(10,10))
m = 70
if( len(data) < 70 ):
    m = len(data)
for i in range(m):
    plt.subplot(7,10,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(data[i][0], cmap='gray')
    plt.xlabel("{}\n({})".format( data[i][1] , data[i][2] ), color='white')    
plt.show()
def save_data(save_name, data):
    pickle_out = open(save_name+"-pickle.pickle","wb")
    print('Arquivo gravado como: '+save_name+"-pickle.pickle")
    pickle.dump(data, pickle_out)
    pickle_out.close()
save_data(local_saida+nome_saida, data)<jupyter_output>Arquivo gravado como: ../bases/pickle/imagens/cifar10/cifar10-train-3-2x2-pickle.pickle
{'fraction_non_alphanumeric': 0.14626391096979333, 'fraction_numerical': 0.017488076311605722, 'mean_word_length': 2.9513343799058083, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '37839', 'license': 'MIT', 'n_tokens_mistral': 985, 'n_tokens_neox': 958, 'n_words': 180, 'path': '.ipynb_checkpoints/Leitura da base-pickle-checkpoint.ipynb', 'repo_name': 'Birunda3000/Reconhecimento-de-imagem', 'size': '2516'}