简体   繁体   English

将图像转换为 numpy 数据集以进行 tesseract ocr 训练

[英]Convert image to numpy dataset for tesseract ocr training

I am trying to create a dataset for tesseract.我正在尝试为 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.以下代码应输出一个包含图像路径和图像标签特征以及 .npz 文件的 csv 文件。 But the code does append any files in the csv但是代码确实在 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. .npz 不是公共区域,尝试将它与不同的背景匹配模式一起使用。

Sample: Using Pandas ( data frame as your requirements ) and Tensorflow示例:使用 Pandas(数据框作为您的要求)和 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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