Update README.md
Browse files
README.md
CHANGED
|
@@ -1,26 +1,79 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
dataset_info:
|
| 4 |
-
features:
|
| 5 |
-
- name: image
|
| 6 |
-
dtype:
|
| 7 |
-
array3_d:
|
| 8 |
-
shape:
|
| 9 |
-
- 512
|
| 10 |
-
- 512
|
| 11 |
-
- 3
|
| 12 |
-
dtype: uint8
|
| 13 |
-
- name: filename
|
| 14 |
-
dtype: string
|
| 15 |
-
splits:
|
| 16 |
-
- name: train
|
| 17 |
-
num_bytes: 34195458528
|
| 18 |
-
num_examples: 18614
|
| 19 |
-
download_size: 6667979906
|
| 20 |
-
dataset_size: 34195458528
|
| 21 |
-
configs:
|
| 22 |
-
- config_name: default
|
| 23 |
-
data_files:
|
| 24 |
-
- split: train
|
| 25 |
-
path: data/train-*
|
| 26 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
dataset_info:
|
| 4 |
+
features:
|
| 5 |
+
- name: image
|
| 6 |
+
dtype:
|
| 7 |
+
array3_d:
|
| 8 |
+
shape:
|
| 9 |
+
- 512
|
| 10 |
+
- 512
|
| 11 |
+
- 3
|
| 12 |
+
dtype: uint8
|
| 13 |
+
- name: filename
|
| 14 |
+
dtype: string
|
| 15 |
+
splits:
|
| 16 |
+
- name: train
|
| 17 |
+
num_bytes: 34195458528
|
| 18 |
+
num_examples: 18614
|
| 19 |
+
download_size: 6667979906
|
| 20 |
+
dataset_size: 34195458528
|
| 21 |
+
configs:
|
| 22 |
+
- config_name: default
|
| 23 |
+
data_files:
|
| 24 |
+
- split: train
|
| 25 |
+
path: data/train-*
|
| 26 |
+
---
|
| 27 |
+
```
|
| 28 |
+
from datasets import load_dataset
|
| 29 |
+
from PIL import Image
|
| 30 |
+
import numpy as np
|
| 31 |
+
import os
|
| 32 |
+
from tqdm import tqdm
|
| 33 |
+
|
| 34 |
+
# 加载数据集
|
| 35 |
+
dataset_path = "path_to/style_fonts_img"
|
| 36 |
+
dataset = load_dataset(dataset_path)
|
| 37 |
+
|
| 38 |
+
# 创建保存目录
|
| 39 |
+
save_dir = os.path.join(dataset_path, "extracted_images")
|
| 40 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
# 获取数据集大小
|
| 43 |
+
total_samples = len(dataset['train'])
|
| 44 |
+
print(f"数据集共有 {total_samples} 个样本")
|
| 45 |
+
|
| 46 |
+
# 使用tqdm创建进度条
|
| 47 |
+
for i, example in tqdm(enumerate(dataset['train']), total=total_samples, desc="处理图像"):
|
| 48 |
+
try:
|
| 49 |
+
# 获取图像数据
|
| 50 |
+
image_array = example['image']
|
| 51 |
+
|
| 52 |
+
# 转换为PIL图像
|
| 53 |
+
image = Image.fromarray(np.uint8(image_array))
|
| 54 |
+
|
| 55 |
+
# 获取文件名
|
| 56 |
+
filename = example['filename']
|
| 57 |
+
|
| 58 |
+
# 保存图像
|
| 59 |
+
image_path = os.path.join(save_dir, filename)
|
| 60 |
+
image.save(image_path)
|
| 61 |
+
|
| 62 |
+
# 保存文本
|
| 63 |
+
if 'text' in example and example['text']:
|
| 64 |
+
text_filename = os.path.splitext(filename)[0] + '.txt'
|
| 65 |
+
text_path = os.path.join(text_dir, text_filename)
|
| 66 |
+
with open(text_path, 'w', encoding='utf-8') as f:
|
| 67 |
+
f.write(example['text'])
|
| 68 |
+
|
| 69 |
+
# print(f"已保存 {filename}")
|
| 70 |
+
|
| 71 |
+
# 只处理前10个样本(可选)
|
| 72 |
+
if i >= 9:
|
| 73 |
+
break
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"处理样本 {i} 时出错: {e}")
|
| 77 |
+
|
| 78 |
+
print("处理完成!")
|
| 79 |
+
```
|