简体   繁体   中英

Convert image to numpy dataset for tesseract ocr training

I am trying to create a dataset for tesseract. But unable to do so. The following code should output a csv file containing the image path and image label feature and.npz file. But the code does append any files in the csv

import numpy as np
import os
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import pandas as pd


image_dataset_dir = "datasets/images"
new_dataset_folder = "datasets/new"


dataset = {
    "image" :[],
    "label" : []
}
for label in os.listdir(image_dataset_dir):
     images_dir= image_dataset_dir + "/" + label
     if not os.path.isdir(images_dir):
        continue
     for image_file in os.listdir(images_dir):
#         if not image_file.endswith(".jpg", ".png",".tiff"):
#             continue 
        img = load_img(os.path.join(image_dataset_dir, label, image_file))
        x = img_to_array(img)                  
        

        rel_path = label + "/" + os.path.splitext(image_file)[0] + '.npz'
        os.makedirs(new_dataset_folder + "/" + label, exist_ok=True)
        npz_file = os.path.join(new_dataset_folder, rel_path)
        np.savez(npz_file, x)
#         print(rel_path)
        dataset["image"].append(rel_path)
        dataset["label"].append(label)

                         
df = pd.DataFrame(dataset)
df.to_csv(os.path.join(new_dataset_folder, "train.csv"), index=False)

print('Dataset converted to npz and saved here at %s '%new_dataset_folder)

df.head()

Your objective, create files and save the output and their values.

.npz is none public zones, try using it with different backgrounds matching patterns.

Sample: Using Pandas ( data frame as your requirements ) and Tensorflow

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
BATCH_SIZE = 1
IMG_SIZE = (32, 32)
new_dataset_folder = "F:\\temp\\Python\\excel"

PATH = 'F:\\datasets\\downloads\\cats_name'
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

train_dataset = tf.keras.utils.image_dataset_from_directory(train_dir, shuffle=True,
    batch_size=BATCH_SIZE, image_size=IMG_SIZE)
                                                            
class_names = train_dataset.class_names

print( 'class_names: ' + str( class_names ) )
print( train_dataset )

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Dataset
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
dataset = {
    "image" :[],
    "label" : []
}

file_order = 0
for data in train_dataset :
    file_path = new_dataset_folder + "\\" + str(int(data[1][0])) + ".npz"
    dataset["image"].append(file_path)
    dataset["label"].append(str(int(data[1][0])))
    # Save
    encoding = "utf-8"
    with open( new_dataset_folder + "\\" + str(file_order), "wb" ) as f:
        f.write(str(data[0]).encode(encoding))
    
    file_order = file_order + 1

df = pd.DataFrame(dataset)
df.to_csv(os.path.join(new_dataset_folder, "train.csv"), index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM