File size: 18,925 Bytes
5525d1a cd9ae6e 5525d1a |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
import io
import os
import xml.etree.ElementTree as ET
from glob import glob
from pathlib import Path, PurePath
import cv2
import numpy as np
from datasets import (
BuilderConfig,
DatasetInfo,
Features,
GeneratorBasedBuilder,
Image,
Sequence,
Split,
SplitGenerator,
Value,
)
from PIL import Image as PILImage
from shapely.geometry import Polygon
class HTRDatasetConfig(BuilderConfig):
"""Configuration for each dataset variant."""
def __init__(self, name, description, process_func, features, **kwargs):
super().__init__(name=name, description=description, **kwargs)
self.process_func = process_func
self.features = features
class HTRDataset(GeneratorBasedBuilder):
# Define feature structures for each dataset type
text_recognition_features = Features(
{
"image": Image(),
"transcription": Value("string"),
}
)
segmentation_features = Features(
{
"image_name": Value("string"),
"image": Image(),
"annotations": Sequence(
{
"polygon": Sequence(Sequence(Value("float32"))),
"transcription": Value("string"),
"class": Value("string"),
}
),
}
)
BUILDER_CONFIGS = [
HTRDatasetConfig(
name="text_recognition",
description="textline dataset for text recognition of historical Swedish",
process_func="text_recognition",
features=text_recognition_features,
),
HTRDatasetConfig(
name="inst_seg_lines_within_regions",
description="Cropped text region images with text line annotations",
process_func="inst_seg_lines_within_regions",
features=segmentation_features,
),
HTRDatasetConfig(
name="inst_seg_regions_and_lines",
description="Original images with both region and line annotations",
process_func="inst_seg_regions_and_lines",
features=segmentation_features,
),
HTRDatasetConfig(
name="inst_seg_lines",
description="Original images with text line annotations only",
process_func="inst_seg_lines",
features=segmentation_features,
),
HTRDatasetConfig(
name="inst_seg_regions",
description="Original images with text region annotations only",
process_func="inst_seg_regions",
features=segmentation_features,
),
]
def _info(self):
return DatasetInfo(features=self.config.features)
def _split_generators(self, dl_manager):
# Define URLs for images and XMLs
"""
images_url = [
f"https://huggingface.co/datasets/Riksarkivet/ra_enstaka_sidor/resolve/main/data/images/ra_enstaka_sidor_images_{i}.tar.gz"
for i in range(1, 3)
]
xmls_url = [
f"https://huggingface.co/datasets/Riksarkivet/ra_enstaka_sidor/resolve/main/data/page_xmls/ra_enstaka_sidor_page_xmls_{i}.tar.gz"
for i in range(1, 3)
]
"""
images = dl_manager.download_and_extract(
[
f"https://huggingface.co/datasets/Riksarkivet/gota_hovratt_seg/resolve/main/data/images/gota_hovratt_seg_images_{i}.tar.gz"
for i in range(1, 3)
]
)
xmls = dl_manager.download_and_extract(
[
f"https://huggingface.co/datasets/Riksarkivet/gota_hovratt_seg/resolve/main/data/page_xmls/gota_hovratt_seg_page_xmls_{i}.tar.gz"
for i in range(1, 3)
]
)
# Download and extract images and XMLs
# images = dl_manager.download_and_extract(images_url)
# xmls = dl_manager.download_and_extract(xmls_url)
# Define supported image file extensions
image_extensions = [
"*.jpg",
"*.jpeg",
"*.png",
"*.gif",
"*.bmp",
"*.tif",
"*.tiff",
"*.JPG",
"*.JPEG",
"*.PNG",
"*.GIF",
"*.BMP",
"*.TIF",
"*.TIFF",
]
# Collect and sort image and XML file paths
imgs_flat = self._collect_file_paths(images, image_extensions)
xmls_flat = self._collect_file_paths(xmls, ["*.xml"])
# Ensure the number of images matches the number of XML files
assert len(imgs_flat) == len(xmls_flat)
# Pair images and XML files
imgs_xmls = list(
zip(sorted(imgs_flat, key=lambda x: Path(x).stem), sorted(xmls_flat, key=lambda x: Path(x).stem))
)
return [
SplitGenerator(
name=Split.TRAIN,
gen_kwargs={"imgs_xmls": imgs_xmls},
)
]
def _collect_file_paths(self, folders, extensions):
"""Collects file paths recursively from specified folders."""
files_nested = [
glob(os.path.join(folder, "**", ext), recursive=True) for ext in extensions for folder in folders
]
return [file for sublist in files_nested for file in sublist]
def _generate_examples(self, imgs_xmls):
process_func = getattr(self, self.config.process_func)
return process_func(imgs_xmls)
def text_recognition(self, imgs_xmls):
"""Process for line dataset with cropped images and transcriptions."""
for img, xml in imgs_xmls:
img_filename, volume = self._extract_filename_and_volume(img, xml)
lines_data = self.parse_pagexml(xml)
image_array = cv2.imread(img)
for i, line in enumerate(lines_data):
line_id = str(i).zfill(4)
cropped_image = self.crop_line_image(image_array, line["coords"])
transcription = line["transcription"]
if not transcription:
print(f"Invalid transcription: {transcription}")
continue
unique_key = f"{volume}_{img_filename}_{line_id}"
yield unique_key, {"image": cropped_image, "transcription": transcription}
def inst_seg_lines_within_regions(self, imgs_xmls):
"""Process for cropped images with text line annotations."""
for img_path, xml_path in imgs_xmls:
img_filename, volume = self._extract_filename_and_volume(img_path, xml_path)
image = PILImage.open(img_path)
root = self._parse_xml(xml_path)
namespaces = {"ns": "http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15"}
# Iterate through each TextRegion
for reg_ind, region in enumerate(root.findall(".//ns:TextRegion", namespaces=namespaces)):
reg_id = str(reg_ind).zfill(4)
region_polygon = self._get_polygon(region, namespaces)
min_x, min_y, max_x, max_y = self._get_bbox(region_polygon)
cropped_region_image = self.crop_image(image, region_polygon)
annotations = self._get_line_annotations_within_region(
region, namespaces, min_x, min_y, region_polygon
)
unique_key = f"{volume}_{img_filename}_{reg_id}"
try:
yield (
unique_key,
{
"image": {"bytes": self._image_to_bytes(cropped_region_image)},
"annotations": annotations,
"image_name": unique_key,
},
)
except:
print("still error")
continue
def inst_seg_regions_and_lines(self, imgs_xmls):
"""Process for original images with both region and line annotations."""
for img_path, xml_path in imgs_xmls:
img_filename, volume = self._extract_filename_and_volume(img_path, xml_path)
image = PILImage.open(img_path)
root = self._parse_xml(xml_path)
annotations = self._get_region_and_line_annotations(root)
unique_key = f"{volume}_{img_filename}"
yield unique_key, {"image_name": unique_key, "image": image, "annotations": annotations}
def inst_seg_lines(self, imgs_xmls):
"""Process for original images with text line annotations only."""
for img_path, xml_path in imgs_xmls:
img_filename, volume = self._extract_filename_and_volume(img_path, xml_path)
image = PILImage.open(img_path)
root = self._parse_xml(xml_path)
annotations = self._get_line_annotations(root)
unique_key = f"{volume}_{img_filename}"
yield unique_key, {"image_name": unique_key, "image": image, "annotations": annotations}
def inst_seg_regions(self, imgs_xmls):
"""Process for original images with text region annotations only."""
for img_path, xml_path in imgs_xmls:
img_filename, volume = self._extract_filename_and_volume(img_path, xml_path)
image = PILImage.open(img_path)
root = self._parse_xml(xml_path)
annotations = self._get_region_annotations(root)
unique_key = f"{volume}_{img_filename}"
yield unique_key, {"image_name": unique_key, "image": image, "annotations": annotations}
def _extract_filename_and_volume(self, img, xml):
"""Extracts the filename and volume from the image and XML paths."""
assert Path(img).stem == Path(xml).stem
img_filename = Path(img).stem
volume = PurePath(img).parts[-2]
return img_filename, volume
def _parse_xml(self, xml_path):
"""Parses the XML file and returns the root element."""
try:
tree = ET.parse(xml_path)
return tree.getroot()
except ET.ParseError as e:
print(f"XML Parse Error: {e}")
return None
def _get_line_annotations_within_region(self, region, namespaces, min_x, min_y, region_polygon):
"""Generates annotations for text lines within a region."""
annotations = []
for line in region.findall(".//ns:TextLine", namespaces=namespaces):
line_polygon = self._get_polygon(line, namespaces)
clipped_line_polygon = self.clip_polygon_to_region(line_polygon, region_polygon)
if len(clipped_line_polygon) < 3:
print(f"Invalid polygon detected for line: {line_polygon}, clipped: {clipped_line_polygon}")
continue
translated_polygon = [(x - min_x, y - min_y) for x, y in clipped_line_polygon]
transcription = "".join(line.itertext()).strip()
annotations.append(
{
"polygon": translated_polygon,
"transcription": transcription,
"class": "textline",
}
)
return annotations
def _get_region_and_line_annotations(self, root):
"""Generates annotations for both text regions and lines."""
annotations = []
# Get region annotations
annotations.extend(self._get_region_annotations(root))
# Get line annotations
annotations.extend(self._get_line_annotations(root))
return annotations
def _get_line_annotations(self, root):
"""Generates annotations for text lines only."""
namespaces = {"ns": "http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15"}
annotations = []
for region in root.findall(".//ns:TextRegion", namespaces=namespaces):
for line in region.findall(".//ns:TextLine", namespaces=namespaces):
line_polygon = self._get_polygon(line, namespaces)
transcription = "".join(line.itertext()).strip()
annotations.append(
{
"polygon": line_polygon,
"transcription": transcription,
"class": "textline",
}
)
return annotations
def _get_region_annotations(self, root):
"""Generates annotations for text regions only."""
namespaces = {"ns": "http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15"}
annotations = []
for region in root.findall(".//ns:TextRegion", namespaces=namespaces):
region_polygon = self._get_polygon(region, namespaces)
annotations.append(
{
"polygon": region_polygon,
"transcription": "",
"class": "textregion",
}
)
return annotations
def _image_to_bytes(self, image):
"""Converts a PIL image to bytes."""
with io.BytesIO() as output:
image.save(output, format="PNG")
return output.getvalue()
def crop_image(self, img_pil, coords):
coords = np.array(coords)
img = np.array(img_pil)
mask = np.zeros(img.shape[0:2], dtype=np.uint8)
try:
# Ensure the coordinates are within the bounds of the image
coords[:, 0] = np.clip(coords[:, 0], 0, img.shape[1] - 1)
coords[:, 1] = np.clip(coords[:, 1], 0, img.shape[0] - 1)
# Draw the mask
cv2.drawContours(mask, [coords], -1, (255, 255, 255), -1, cv2.LINE_AA)
# Apply mask to image
res = cv2.bitwise_and(img, img, mask=mask)
rect = cv2.boundingRect(coords)
# Ensure the bounding box is within the image dimensions
rect = (
max(0, rect[0]),
max(0, rect[1]),
min(rect[2], img.shape[1] - rect[0]),
min(rect[3], img.shape[0] - rect[1]),
)
wbg = np.ones_like(img, np.uint8) * 255
cv2.bitwise_not(wbg, wbg, mask=mask)
# Overlap the resulted cropped image on the white background
dst = wbg + res
# Use validated rect for cropping
cropped = dst[rect[1] : rect[1] + rect[3], rect[0] : rect[0] + rect[2]]
# Convert the NumPy array back to a PIL image
cropped_pil = PILImage.fromarray(cropped)
return cropped_pil
except Exception as e:
print(f"Error in cropping: {e}")
return img_pil # Return the original image if there's an error
def _create_mask(self, shape, coords):
"""Creates a mask for the specified polygon coordinates."""
mask = np.zeros(shape, dtype=np.uint8)
cv2.drawContours(mask, [np.array(coords)], -1, (255, 255, 255), -1, cv2.LINE_AA)
return mask
def parse_pagexml(self, xml):
"""Parses the PAGE XML and extracts line data."""
root = self._parse_xml(xml)
if not root:
return []
namespaces = {"ns": "http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15"}
lines_data = []
for region in root.findall(".//ns:TextRegion", namespaces):
for line in region.findall(".//ns:TextLine", namespaces):
try:
line_id = line.get("id")
coords = self._get_polygon(line, namespaces)
transcription = line.find("ns:TextEquiv/ns:Unicode", namespaces).text or ""
lines_data.append({"line_id": line_id, "coords": coords, "transcription": transcription})
except Exception as e:
print(f"Error parsing line: {e}")
return lines_data
def crop_line_image(self, img, coords):
"""Crops a line image based on the provided coordinates."""
mask = self._create_mask(img.shape[:2], coords)
coords = np.array(coords)
# Apply mask to image
res = cv2.bitwise_and(img, img, mask=mask)
rect = cv2.boundingRect(coords)
# Create a white background and overlay the cropped image
wbg = np.ones_like(img, np.uint8) * 255
cv2.bitwise_not(wbg, wbg, mask=mask)
dst = wbg + res
cropped = dst[rect[1] : rect[1] + rect[3], rect[0] : rect[0] + rect[2]]
return self.cv2_to_pil(cropped)
def _get_polygon(self, element, namespaces):
"""Extracts polygon points from a PAGE XML element."""
coords = element.find(".//ns:Coords", namespaces=namespaces).attrib["points"]
return [tuple(map(int, p.split(","))) for p in coords.split()]
def _get_bbox(self, polygon):
"""Calculates the bounding box from polygon points."""
min_x = min(p[0] for p in polygon)
min_y = min(p[1] for p in polygon)
max_x = max(p[0] for p in polygon)
max_y = max(p[1] for p in polygon)
return min_x, min_y, max_x, max_y
def clip_polygon_to_region(self, line_polygon, region_polygon):
"""
Clips a line polygon to ensure it's inside the region polygon using Shapely.
Returns the original line polygon if the intersection is empty.
"""
# Convert lists of points to Shapely Polygons
line_poly = Polygon(line_polygon)
region_poly = Polygon(region_polygon)
# Compute the intersection of the line polygon with the region polygon
try:
intersection = line_poly.intersection(region_poly)
except Exception:
return line_polygon
# Return the intersection points as a list of tuples
if intersection.is_empty:
print(
f"No intersection found for line_polygon {line_polygon} within region_polygon {region_polygon}, returning original polygon."
)
return line_polygon
elif intersection.geom_type == "Polygon":
return list(intersection.exterior.coords)
elif intersection.geom_type == "MultiPolygon":
# If the result is a MultiPolygon, take the largest by area (or another heuristic)
largest_polygon = max(intersection.geoms, key=lambda p: p.area)
return list(largest_polygon.exterior.coords)
elif intersection.geom_type == "LineString":
return list(intersection.coords)
else:
print(f"Unexpected intersection type: {intersection.geom_type}")
return line_polygon
def cv2_to_pil(self, cv2_image):
"""Converts an OpenCV image to a PIL Image."""
cv2_image_rgb = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2RGB)
return PILImage.fromarray(cv2_image_rgb)
|