-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranskribus.py
More file actions
73 lines (57 loc) · 2.87 KB
/
transkribus.py
File metadata and controls
73 lines (57 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import shutil
from pathlib import Path
import xml.etree.ElementTree as ET
def flatten_transkribus_for_kraken(root_dir, output_dir):
"""
Átalakítja a Transkribus struktúrát Kraken tanításhoz.
1. Iterál a mappákon.
2. XML + JPG párokat hoz létre egyedi névvel (mappa + fájlnév).
3. Frissíti az imageFilename attribútumot az XML-ben.
"""
root_path = Path(root_dir)
out_path = Path(output_dir)
out_path.mkdir(parents=True, exist_ok=True)
# PAGE XML névtér regisztrálása
ns = {'pc': 'http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15'}
ET.register_namespace('', ns['pc'])
# 1. Iterálás a mappákon (pl. SZEEKL_VIII_9_a_1884-1887)
for folder in root_path.iterdir():
if not folder.is_dir():
continue
page_dir = folder / "page"
if not page_dir.exists():
continue
print(f"Feldolgozás: {folder.name}")
# 2. XML fájlok keresése a 'page' mappában
for xml_file in page_dir.glob("*.xml"):
base_name = xml_file.stem
# Megkeressük a hozzá tartozó képet (jpg vagy jpeg)
image_file = folder / f"{base_name}.jpg"
if not image_file.exists():
image_file = folder / f"{base_name}.jpeg"
if image_file.exists():
# Új fájlnév generálása: mappa_neve + eredeti_név
new_base_name = f"{folder.name}_{base_name}"
new_xml_path = out_path / f"{new_base_name}.xml"
new_img_path = out_path / f"{new_base_name}{image_file.suffix}"
# 3. XML módosítása (imageFilename frissítése)
try:
tree = ET.parse(xml_file)
root = tree.getroot()
# Megkeressük a Page elemet és frissítjük az attribútumot
page_elem = root.find('.//pc:Page', ns)
if page_elem is not None:
page_elem.set('imageFilename', new_img_path.name)
# Mentés az új helyre
tree.write(new_xml_path, encoding='UTF-8', xml_declaration=True)
# Kép másolása az új névvel
shutil.copy2(image_file, new_img_path)
except Exception as e:
print(f" [!] Hiba a(z) {xml_file.name} feldolgozásakor: {e}")
print(f"\nKész! Az adathalmaz itt található: {output_dir}")
if __name__ == "__main__":
# Állítsd be az útvonalakat a saját környezetednek megfelelően!
SOURCE_DIRECTORY = r"C:\_py\Vezetotestuleti\Transkribus" # Ahol a mappáid vannak
TARGET_DIRECTORY = r"C:\_py\Vezetotestuleti\Kraken_Dataset"
flatten_transkribus_for_kraken(SOURCE_DIRECTORY, TARGET_DIRECTORY)