簡體   English   中英

將圖像轉換為 numpy 數據集以進行 tesseract ocr 訓練

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

我正在嘗試為 tesseract 創建一個數據集。 但不能這樣做。 以下代碼應輸出一個包含圖像路徑和圖像標簽特征以及 .npz 文件的 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()

您的目標是創建文件並保存輸出及其值。

.npz 不是公共區域,嘗試將它與不同的背景匹配模式一起使用。

示例:使用 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