├── tests ├── __init__.py ├── test_files │ ├── .gitignore │ ├── label_map.txt │ ├── page1.jpg │ ├── alto_dataset │ │ ├── output.jpg │ │ ├── Bruyere07.jpg │ │ ├── Bruyere09.jpg │ │ ├── Bruyere07.xml │ │ └── Bruyere09.xml │ ├── annot2.txt │ └── annot1.txt ├── nano-yolo-ladas.pt ├── test_utils.py ├── test_convert.py └── test_kraken.py ├── yaltai ├── __init__.py ├── cli │ ├── __init__.py │ ├── krakn.py │ └── yaltai.py ├── models │ ├── __init__.py │ ├── yolo.py │ └── krakn.py ├── template.xml ├── preprocessing.py └── utils.py ├── MANIFEST.in ├── requirements.txt ├── .github └── workflows │ └── test.yml ├── CITATION.CFF ├── README.md ├── .gitignore ├── setup.py └── LICENSE /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /yaltai/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /yaltai/cli/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /yaltai/models/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include yaltai/template.xml -------------------------------------------------------------------------------- /tests/test_files/.gitignore: -------------------------------------------------------------------------------- 1 | page1.xml -------------------------------------------------------------------------------- /tests/test_files/label_map.txt: -------------------------------------------------------------------------------- 1 | Class0 2 | Class1 3 | 4 | Stuff -------------------------------------------------------------------------------- /tests/nano-yolo-ladas.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PonteIneptique/YALTAi/HEAD/tests/nano-yolo-ladas.pt -------------------------------------------------------------------------------- /tests/test_files/page1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PonteIneptique/YALTAi/HEAD/tests/test_files/page1.jpg -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | kraken~=5.3.0 2 | mean-average-precision==2021.4.26.0 3 | tabulate~=0.8.10 4 | ultralytics~=8.3.0 5 | fast_deskew==1.0 -------------------------------------------------------------------------------- /tests/test_files/alto_dataset/output.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PonteIneptique/YALTAi/HEAD/tests/test_files/alto_dataset/output.jpg -------------------------------------------------------------------------------- /tests/test_files/alto_dataset/Bruyere07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PonteIneptique/YALTAi/HEAD/tests/test_files/alto_dataset/Bruyere07.jpg -------------------------------------------------------------------------------- /tests/test_files/alto_dataset/Bruyere09.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PonteIneptique/YALTAi/HEAD/tests/test_files/alto_dataset/Bruyere09.jpg -------------------------------------------------------------------------------- /tests/test_files/annot2.txt: -------------------------------------------------------------------------------- 1 | 23 0.4914115952466691 0.45760844079718643 0.6449045732805185 0.4102297772567409 2 | 23 0.4933705437522506 0.7764806565064478 0.6465250270075622 0.2320398593200469 3 | 31 0.7885343896290963 0.11840562719812427 0.033172488296723084 0.026182883939038688 4 | 35 0.4834569679510263 0.1191535756154748 0.4334101548433561 0.024438452520515828 5 | 24 0.48678790061217136 0.20451113716295427 0.6372740367302845 0.10247831184056272 6 | 28 0.47840835433921497 0.14436342321219228 0.24528628015844436 0.025828839390386868 -------------------------------------------------------------------------------- /tests/test_files/annot1.txt: -------------------------------------------------------------------------------- 1 | 14 0.49139869729173813 0.3715823573386494 0.6673020226259856 0.07884874158983304 2 | 14 0.49408639012684263 0.23613506105158236 0.6722831676379842 0.15367804634936458 3 | 35 0.49086047308878983 0.1285771243458759 0.26186150154268084 0.03464490406179915 4 | 31 0.8086732944806309 0.13971592324943932 0.03529310935893041 0.03310740094692251 5 | 14 0.49913952691121016 0.594151507600299 0.6766986630099417 0.31005232992773485 6 | 14 0.507857387727117 0.8290804884126588 0.6902228316763799 0.1168950909543982 7 | 1 0.9231984916009598 0.4728781460254174 0.1535995886184436 0.18793919760777475 -------------------------------------------------------------------------------- /yaltai/template.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | pixel 7 | 8 | %Filename% 9 | 10 | 11 | 12 | 13 | 14 | %Tags% 15 | 16 | 17 | 18 | 21 | 25 | %Textblocks% 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | # This workflow will install Python dependencies, run tests and lint with a single version of Python 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions 3 | 4 | name: Test 5 | 6 | on: [push, pull_request] 7 | 8 | jobs: 9 | test: 10 | runs-on: ubuntu-latest 11 | strategy: 12 | matrix: 13 | python-version: ["3.8", "3.9", "3.10", "3.11"] 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Set up Python 17 | uses: actions/setup-python@v5 18 | with: 19 | python-version: ${{ matrix.python-version }} 20 | - name: Display Python version 21 | run: python -c "import sys; print(sys.version)" 22 | - name: Install dependencies 23 | run: | 24 | pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu 25 | pip install pytest pytest-cov coveralls pytest-sugar 26 | - name: Run Tests 27 | run: | 28 | pytest --doctest-modules --cov=yaltai --verbose 29 | -------------------------------------------------------------------------------- /CITATION.CFF: -------------------------------------------------------------------------------- 1 | # This CITATION.cff file was generated with cffinit. 2 | # Visit https://bit.ly/cffinit to generate yours today! 3 | 4 | cff-version: 1.2.0 5 | title: 'YALTAi: You Actually Look Twice At it' 6 | message: "If you use this software, please cite both the article from preferred-citation and the software itself." 7 | type: software 8 | authors: 9 | - given-names: Thibault 10 | family-names: Clérice 11 | email: thibault.clerice@inria.fr 12 | affiliation: Inria 13 | orcid: 'https://orcid.org/0000-0003-1852-9204' 14 | identifiers: 15 | - type: swh 16 | value: 'swh:1:snp:d9ca209ae2271537ff57d3748c8a76c49fd6222b' 17 | - type: other 18 | value: 'https://enc.hal.science/hal-03723208v3' 19 | description: Paper 20 | repository-code: 'https://github.com/PonteIneptique/yaltai' 21 | url: 'https://pypi.org/project/YALTAi/' 22 | license: GPL-3.0 23 | version: 1.0.0 24 | date-released: '2023-01-01' 25 | preferred-citation: 26 | authors: 27 | - family-names: Clérice 28 | given-names: Thibault 29 | title: "You Actually Look Twice At it (YALTAi): using an object detection approach instead of region segmentation within the Kraken engine" 30 | type: article 31 | volume-title: 'Historical Documents and automatic text recognition' 32 | journal: "Journal of Data Mining & Digital Humanities" 33 | doi: 'doi.org/10.46298/jdmdh.9806' 34 | date-published: "2023-12-01" 35 | -------------------------------------------------------------------------------- /yaltai/preprocessing.py: -------------------------------------------------------------------------------- 1 | from typing import Tuple, List 2 | from PIL import Image 3 | import fast_deskew 4 | import cv2 5 | import numpy as np 6 | 7 | 8 | def deskew(image: str) -> Tuple[Image.Image, float]: 9 | _, best_angle = fast_deskew.deskew_image(image, False) 10 | img = Image.open(image) 11 | return img.rotate(best_angle), best_angle 12 | 13 | 14 | def rotatebox(bbox: List[List[int]], image: Image.Image, angle: float): 15 | # https://gist.githubusercontent.com/Joanne03/5941a9b4db4fa7c652a2d7f67b11a09b/raw/f99b2f18ad0fbe680ac631e531782b957f158def/rotate_bbox.py 16 | height, width = image.size 17 | image_center_x, image_center_y = width // 2, height // 2 18 | 19 | rotated_bbox = [] 20 | 21 | for i, coord in enumerate(bbox): 22 | rot_matrix = cv2.getRotationMatrix2D((image_center_x, image_center_y), angle, 1.0) 23 | cosinus, sinus = abs(rot_matrix[0, 0]), abs(rot_matrix[0, 1]) 24 | new_width = int((height * sinus) + (width * cosinus)) 25 | new_height = int((height * cosinus) + (width * sinus)) 26 | rot_matrix[0, 2] += (new_width / 2) - image_center_x 27 | rot_matrix[1, 2] += (new_height / 2) - image_center_y 28 | v = [coord[0], coord[1], 1] # ? 29 | adjusted_coord = np.dot(rot_matrix, v) 30 | rotated_bbox.append((int(adjusted_coord[0]), int(adjusted_coord[1]))) 31 | 32 | return rotated_bbox 33 | -------------------------------------------------------------------------------- /yaltai/models/yolo.py: -------------------------------------------------------------------------------- 1 | from typing import List, Dict 2 | from ultralytics import YOLO 3 | from ultralytics.engine.results import Results 4 | 5 | from yaltai.preprocessing import deskew, rotatebox 6 | 7 | 8 | def segment( 9 | model: YOLO, 10 | input: str, 11 | apply_deskew: bool = False, 12 | max_angle: float = 10.0 13 | ) -> Dict[str, List[List[int]]]: 14 | """ 15 | 16 | Returns { 17 | cls_name: [ 18 | [[x0, y0], [x1, y0], [x1, y1], [x0, y1], [x0, y0]] 19 | ] 20 | } 21 | """ 22 | rotated_input = None 23 | angle = 0 24 | predictions: List[Results] = [] 25 | if apply_deskew: 26 | rotated_input, angle = deskew(input) 27 | if abs(angle) > max_angle: 28 | predictions = model.predict(input, save=False) 29 | rotated_input = None 30 | else: 31 | predictions = model.predict(rotated_input, save=False) 32 | else: 33 | predictions = model.predict(input, save=False) 34 | 35 | names: List[str] = list(set([ 36 | name 37 | for res in predictions 38 | for name in res.names.values() 39 | ])) 40 | 41 | out = { 42 | name: [] 43 | for name in names 44 | } 45 | for pred in predictions: 46 | for box, cls_id in zip(pred.boxes.xyxy, pred.boxes.cls): 47 | cls_name = pred.names[cls_id.item()] 48 | x0, y0, x1, y1 = box.tolist() 49 | points = [[x0, y0], [x1, y0], [x1, y1], [x0, y1], [x0, y0]] 50 | 51 | if apply_deskew and rotated_input is not None: 52 | points = rotatebox(points, rotated_input, -angle) 53 | points.append(points[0]) 54 | out[cls_name].append(points) 55 | 56 | return out 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YALTAi 2 | You Actually Look Twice At it 3 | 4 | This provides an adapter for Kraken to use YOLOv8 (1.0.0 update; use previous version to reuse YOLOv5 models) Object Detection routine. 5 | 6 | This tool can be used for both segmenting and conversion of models. 7 | 8 | # Routine 9 | 10 | ## Instal 11 | 12 | ```bash 13 | pip install YALTAi 14 | ``` 15 | 16 | ## Training 17 | 18 | Convert (and split optionally) your data 19 | 20 | ```bash 21 | # Keeps .1 data in the validation set and convert all alto into YOLOv5 format 22 | # Keeps the segmonto information up to the regions 23 | yaltai convert alto-to-yolo PATH/TO/ALTOorPAGE/*.xml my-dataset --shuffle .1 --segmonto region 24 | ``` 25 | 26 | And then [train YOLO](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data) 27 | 28 | ```bash 29 | yolo task=detect mode=train model=yolov8n.pt data=my-dataset/config.yml epochs=100 plots=True device=0 batch=8 imgsz=960 30 | ``` 31 | 32 | ## Predicting 33 | 34 | YALTAi has the same CLI interface as Kraken, so: 35 | 36 | - You can use base BLLA model for line or provide yours with `-i model.mlmodel` 37 | - Use a GPU (`--device cuda:0`) or a CPU (`--device cpu`) 38 | - Apply on batch (`*.jpg`) 39 | 40 | ```bash 41 | # Retrieve the best.pt after the training 42 | # It should be in runs/train/exp[NUMBER]/weights/best.pt 43 | # And then annotate your new data with the same CLI API as Kraken ! 44 | yaltai kraken --device cuda:0 -I "*.jpg" --suffix ".xml" segment --yolo runs/train/exp5/weights/best.pt 45 | ``` 46 | 47 | ## Metrics 48 | 49 | The metrics produced from various libraries never gives the same mAP or Precision. I tried 50 | 51 | - `object-detection-metrics==0.4` 52 | - `mapCalc` 53 | - `mean-average-precision` which ended up being the chosen one (cleanest in terms of how I can access info) 54 | 55 | and of course I compared with YOLOv5 raw results. Nothing worked the same. And the library YOLOv5 derives its metrics from is uninstallable through pip. 56 | -------------------------------------------------------------------------------- /tests/test_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | from yaltai.utils import read_labelmap, parse_box_labels, XYXY 4 | 5 | 6 | def test_read_labelmap(): 7 | """Asserts that reading a label map works""" 8 | labels = read_labelmap(os.path.join( 9 | os.path.dirname(__file__), 10 | "test_files", 11 | "label_map.txt" 12 | )) 13 | assert labels == ["Class0", "Class1", "Stuff"] 14 | 15 | 16 | def test_read_files(): 17 | """Asserts that parsing COCO/YOLO formats work""" 18 | annots, arrays = parse_box_labels([ 19 | os.path.join( 20 | os.path.dirname(__file__), 21 | "test_files", 22 | "annot1.txt" 23 | ), 24 | os.path.join( 25 | os.path.dirname(__file__), 26 | "test_files", 27 | "annot2.txt" 28 | ) 29 | ]) 30 | assert annots == { 31 | 'boxes': [ 32 | XYXY(x0=15, y0=33, x1=82, y1=41), 33 | XYXY(x0=15, y0=15, x1=83, y1=31), 34 | XYXY(x0=35, y0=11, x1=62, y1=14), 35 | XYXY(x0=79, y0=12, x1=82, y1=15), 36 | XYXY(x0=16, y0=43, x1=83, y1=74), 37 | XYXY(x0=16, y0=77, x1=85, y1=88), 38 | XYXY(x0=84, y0=37, x1=99, y1=56), 39 | XYXY(x0=16, y0=25, x1=81, y1=66), 40 | XYXY(x0=17, y0=66, x1=81, y1=89), 41 | XYXY(x0=77, y0=10, x1=80, y1=13), 42 | XYXY(x0=26, y0=10, x1=70, y1=13), 43 | XYXY(x0=16, y0=15, x1=80, y1=25), 44 | XYXY(x0=35, y0=13, x1=60, y1=15) 45 | ], 46 | 'labels': [14, 14, 35, 31, 14, 14, 1, 23, 23, 31, 35, 24, 28] 47 | } 48 | assert (arrays[0] == np.array([[15, 33, 82, 41, 14, 0, 0], 49 | [15, 15, 83, 31, 14, 0, 0], 50 | [35, 11, 62, 14, 35, 0, 0], 51 | [79, 12, 82, 15, 31, 0, 0], 52 | [16, 43, 83, 74, 14, 0, 0], 53 | [16, 77, 85, 88, 14, 0, 0], 54 | [84, 37, 99, 56, 1, 0, 0]])).all() 55 | assert (arrays[1] == np.array([[16, 25, 81, 66, 23, 0, 0], 56 | [17, 66, 81, 89, 23, 0, 0], 57 | [77, 10, 80, 13, 31, 0, 0], 58 | [26, 10, 70, 13, 35, 0, 0], 59 | [16, 15, 80, 25, 24, 0, 0], 60 | [35, 13, 60, 15, 28, 0, 0]])).all() 61 | -------------------------------------------------------------------------------- /tests/test_convert.py: -------------------------------------------------------------------------------- 1 | import tempfile 2 | import os 3 | import glob 4 | from click.testing import CliRunner 5 | 6 | from yaltai.cli.yaltai import yaltai_cli 7 | 8 | 9 | def test_yaltai_single_alto_to_xml(): 10 | """Ensures that we can convert to YOLO format""" 11 | runner = CliRunner() 12 | 13 | with tempfile.TemporaryDirectory() as tempdir: 14 | # Run the Click command 15 | result = runner.invoke( 16 | yaltai_cli, 17 | [ 18 | "convert", 19 | "alto-to-yolo", 20 | os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_files", "alto_dataset", "output.xml"), 21 | tempdir, 22 | ]) 23 | 24 | # Ensure the command ran successfully 25 | assert result.exit_code == 0 26 | assert "Found 1 to convert." in result.output 27 | assert "- 00001 NumberingZone" in result.output, "Correct number of zone types are found" 28 | assert "- 00001 RunningTitleZone" in result.output, "Correct number of zone types are found" 29 | assert "- 00001 MainZone-P-Continued" in result.output, "Correct number of zone types are found" 30 | assert "- 00004 MainZone-P" in result.output, "Correct number of zone types are found" 31 | with open(os.path.join(tempdir, "labels", "output.txt")) as f: 32 | data = [line.split() for line in f.read().split("\n")] 33 | assert data == [ 34 | ['0', '0.345606', '0.117500', '0.037501', '0.020206'], 35 | ['1', '0.612827', '0.118333', '0.212882', '0.020736'], 36 | ['2', '0.616390', '0.210833', '0.578172', '0.142851'], 37 | ['3', '0.616390', '0.365833', '0.577029', '0.162800'], 38 | ['3', '0.616390', '0.547500', '0.574365', '0.199581'], 39 | ['3', '0.616390', '0.727500', '0.576050', '0.161861'], 40 | ['3', '0.611639', '0.819167', '0.583077', '0.022773'] 41 | ] 42 | 43 | 44 | def test_yaltai_shuffle_alto_to_xml(): 45 | """Ensures that we can convert to YOLO format""" 46 | runner = CliRunner() 47 | 48 | with tempfile.TemporaryDirectory() as tempdir: 49 | # Run the Click command 50 | result = runner.invoke( 51 | yaltai_cli, 52 | [ 53 | "convert", 54 | "alto-to-yolo", 55 | "--shuffle", 56 | ".3", 57 | *glob.glob(os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_files", "alto_dataset", "*.xml")), 58 | tempdir, 59 | ]) 60 | assert "Found 3 to convert." in result.output 61 | assert "1/3 image for validation." in result.output 62 | assert result.exit_code == 0 63 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Yolo data for test 2 | nl 3 | *.pt 4 | *.mlmodel 5 | # Test results 6 | test_*.jpg 7 | test_*.xml 8 | converted 9 | my-dataset 10 | ex 11 | table-test 12 | runs 13 | yolo-dataset 14 | *.tar.gz 15 | *.sh 16 | # PyCharm 17 | .idea 18 | # Byte-compiled / optimized / DLL files 19 | __pycache__/ 20 | *.py[cod] 21 | *$py.class 22 | 23 | # C extensions 24 | *.so 25 | 26 | # Distribution / packaging 27 | .Python 28 | build/ 29 | develop-eggs/ 30 | dist/ 31 | downloads/ 32 | eggs/ 33 | .eggs/ 34 | lib/ 35 | lib64/ 36 | parts/ 37 | sdist/ 38 | var/ 39 | wheels/ 40 | pip-wheel-metadata/ 41 | share/python-wheels/ 42 | *.egg-info/ 43 | .installed.cfg 44 | *.egg 45 | MANIFEST 46 | 47 | # PyInstaller 48 | # Usually these files are written by a python script from a template 49 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 50 | *.manifest 51 | *.spec 52 | 53 | # Installer logs 54 | pip-log.txt 55 | pip-delete-this-directory.txt 56 | 57 | # Unit test / coverage reports 58 | htmlcov/ 59 | .tox/ 60 | .nox/ 61 | .coverage 62 | .coverage.* 63 | .cache 64 | nosetests.xml 65 | coverage.xml 66 | *.cover 67 | *.py,cover 68 | .hypothesis/ 69 | .pytest_cache/ 70 | 71 | # Translations 72 | *.mo 73 | *.pot 74 | 75 | # Django stuff: 76 | *.log 77 | local_settings.py 78 | db.sqlite3 79 | db.sqlite3-journal 80 | 81 | # Flask stuff: 82 | instance/ 83 | .webassets-cache 84 | 85 | # Scrapy stuff: 86 | .scrapy 87 | 88 | # Sphinx documentation 89 | docs/_build/ 90 | 91 | # PyBuilder 92 | target/ 93 | 94 | # Jupyter Notebook 95 | .ipynb_checkpoints 96 | 97 | # IPython 98 | profile_default/ 99 | ipython_config.py 100 | 101 | # pyenv 102 | .python-version 103 | 104 | # pipenv 105 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 106 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 107 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 108 | # install all needed dependencies. 109 | #Pipfile.lock 110 | 111 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 112 | __pypackages__/ 113 | 114 | # Celery stuff 115 | celerybeat-schedule 116 | celerybeat.pid 117 | 118 | # SageMath parsed files 119 | *.sage.py 120 | 121 | # Environments 122 | .env 123 | .venv 124 | env/ 125 | venv/ 126 | ENV/ 127 | env.bak/ 128 | venv.bak/ 129 | 130 | # Spyder project settings 131 | .spyderproject 132 | .spyproject 133 | 134 | # Rope project settings 135 | .ropeproject 136 | 137 | # mkdocs documentation 138 | /site 139 | 140 | # mypy 141 | .mypy_cache/ 142 | .dmypy.json 143 | dmypy.json 144 | 145 | # Pyre type checker 146 | .pyre/ 147 | /article-dataset/ 148 | -------------------------------------------------------------------------------- /yaltai/utils.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | from collections import namedtuple 3 | from typing import List, Tuple, Optional, Dict, Union 4 | from pathlib import Path 5 | import numpy as np 6 | 7 | XYXY = namedtuple("XYXY", ["x0", "y0", "x1", "y1"]) 8 | 9 | 10 | @dataclass 11 | class AltoToYoloZone: 12 | BOX: List[Tuple[int, int]] 13 | PAGE_WIDTH: int 14 | PAGE_HEIGHT: int 15 | tag: int 16 | _xywh: Optional[Tuple[int, int, int, int]] = None 17 | 18 | @property 19 | def height(self): 20 | return self.xywh[-1] 21 | 22 | @property 23 | def width(self): 24 | return self.xywh[-2] 25 | 26 | @property 27 | def x_center(self) -> int: 28 | return int(self.width / 2 + self.xywh[0]) 29 | 30 | @property 31 | def y_center(self) -> int: 32 | return int(self.height / 2 + self.xywh[1]) 33 | 34 | @property 35 | def xywh(self): 36 | if self._xywh: 37 | return self._xywh 38 | 39 | box = np.array(self.BOX) 40 | x_min, y_min = box.min(axis=0) 41 | x_max, y_max = box.max(axis=0) 42 | 43 | width = x_max - x_min 44 | height = y_max - y_min 45 | 46 | self._xywh = (x_min, y_min, width, height) 47 | return self._xywh 48 | 49 | def yoloV5(self): 50 | try: 51 | return (f"{self.tag}" 52 | f" {self.x_center / self.PAGE_WIDTH:.6f}" 53 | f" {self.y_center / self.PAGE_HEIGHT:.6f}" 54 | f" {self.width / self.PAGE_WIDTH:.6f}" 55 | f" {self.height / self.PAGE_HEIGHT:.6f}") 56 | except Exception as E: 57 | print(E) 58 | return None 59 | 60 | 61 | @dataclass 62 | class YoloV5Zone: 63 | tag: int 64 | xc: float 65 | yc: float 66 | w: float 67 | h: float 68 | 69 | @classmethod 70 | def from_txt(cls, tag, *box): 71 | return YoloV5Zone(int(tag), *[float(b) for b in box]) 72 | 73 | @property 74 | def xyxy(self): 75 | return XYXY( 76 | self.xc - self.w / 2, 77 | self.yc - self.h / 2, 78 | self.xc + self.w / 2, 79 | self.yc + self.h / 2 80 | ) 81 | 82 | @property 83 | def xyxy100(self): 84 | return XYXY( 85 | *[int(100 * b) for b in self.xyxy] 86 | ) 87 | 88 | 89 | def parse_box_labels( 90 | files: List[str], 91 | gt: bool = True 92 | ) -> Tuple[Dict[str, Union[List[Union[XYXY, int]]]], List[np.array]]: 93 | """Parse a list of YOLO/COCO BB annotation files 94 | 95 | This function is only used to compute metrics 96 | 97 | :param files: List of file path in YOLO / COCO formats 98 | :param gt: If data are ground Truth of if they are predicted (I don't remember why) 99 | """ 100 | parsed = {"boxes": [], "labels": []} 101 | arrays = [] 102 | for file in sorted(files): 103 | with open(file) as f: 104 | start_index = len(parsed["boxes"]) 105 | for line in f: 106 | z = YoloV5Zone.from_txt(*line.strip().split()[:5]) 107 | parsed["boxes"].append(z.xyxy100) 108 | parsed["labels"].append(z.tag) 109 | arrays.append( 110 | np.array([ 111 | [*xyxy, cls_idx] + ([0, 0] if gt else [1]) 112 | for xyxy, cls_idx in zip(parsed["boxes"][start_index:], parsed["labels"][start_index:]) 113 | ]) 114 | ) 115 | 116 | return parsed, arrays 117 | 118 | 119 | def read_labelmap(path: str) -> List[str]: 120 | """ Reads a labelmap YAML file and parses the classes 121 | """ 122 | with open(path) as f: 123 | lines = [line.strip() for line in f.read().split() if line.strip()] 124 | return lines 125 | 126 | 127 | def read_manifest(manifest: Union[str, Path]) -> List[str]: 128 | """ Reads a manifest (for training purposes ?) 129 | """ 130 | with open(manifest, 'r') as f: 131 | out = [x.strip() for x in f.read().splitlines() if x.strip()] 132 | return out 133 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | # Note: To use the 'upload' functionality of this file, you must: 5 | # $ pip install twine 6 | 7 | import io 8 | import os 9 | import sys 10 | from shutil import rmtree 11 | 12 | from setuptools import find_packages, setup, Command 13 | 14 | here = os.path.abspath(os.path.dirname(__file__)) 15 | 16 | # Package meta-data. 17 | NAME = 'YALTAi' 18 | DESCRIPTION = "You Actually Look Twice At it, YOLOv5-Kraken adapter for region detection " 19 | URL = 'https://github.com/ponteineptique/yaltai' 20 | AUTHOR = 'Thibault Clérice' 21 | REQUIRES_PYTHON = '>=3.8.0' 22 | VERSION = "2.0.5" 23 | 24 | # What packages are required for this module to be executed? 25 | 26 | with open(os.path.join(here, 'requirements.txt')) as f: 27 | REQUIRED = f.read().splitlines() 28 | 29 | # What packages are optional? 30 | EXTRAS = {} 31 | 32 | # The rest you shouldn't have to touch too much :) 33 | # ------------------------------------------------ 34 | # Except, perhaps the License and Trove Classifiers! 35 | # If you do change the License, remember to change the Trove Classifier for that! 36 | 37 | 38 | # Import the README and use it as the long-description. 39 | # Note: this will only work if 'README.md' is present in your MANIFEST.in file! 40 | try: 41 | with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f: 42 | long_description = '\n' + f.read() 43 | except FileNotFoundError: 44 | long_description = DESCRIPTION 45 | 46 | # Load the package's __version__.py module as a dictionary. 47 | about = {} 48 | if not VERSION: 49 | project_slug = NAME.lower().replace("-", "_").replace(" ", "_") 50 | with open(os.path.join(here, project_slug, '__version__.py')) as f: 51 | exec(f.read(), about) 52 | else: 53 | about['__version__'] = VERSION 54 | 55 | 56 | class UploadCommand(Command): 57 | """Support setup.py upload.""" 58 | 59 | description = 'Build and publish the package.' 60 | user_options = [] 61 | 62 | @staticmethod 63 | def status(s): 64 | """Prints things in bold.""" 65 | print('\033[1m{0}\033[0m'.format(s)) 66 | 67 | def initialize_options(self): 68 | pass 69 | 70 | def finalize_options(self): 71 | pass 72 | 73 | def run(self): 74 | try: 75 | self.status('Removing previous builds…') 76 | rmtree(os.path.join(here, 'dist')) 77 | except OSError: 78 | pass 79 | 80 | self.status('Building Source and Wheel (universal) distribution…') 81 | os.system('{0} setup.py sdist bdist_wheel --universal'.format(sys.executable)) 82 | 83 | self.status('Uploading the package to PyPI via Twine…') 84 | os.system('twine upload dist/*') 85 | 86 | self.status('Pushing git tags…') 87 | os.system('git tag v{0}'.format(about['__version__'])) 88 | os.system('git push --tags') 89 | 90 | sys.exit() 91 | 92 | 93 | # Where the magic happens: 94 | setup( 95 | name=NAME, 96 | version=about['__version__'], 97 | description=DESCRIPTION, 98 | long_description=long_description, 99 | long_description_content_type='text/markdown', 100 | author=AUTHOR, 101 | python_requires=REQUIRES_PYTHON, 102 | url=URL, 103 | packages=find_packages(exclude=('tests', 'env', 'venv',)), 104 | # If your package is a single module, use this instead of 'packages': 105 | # py_modules=['mypackage'], 106 | 107 | entry_points={ 108 | 'console_scripts': ['yaltai=yaltai.cli.yaltai:yaltai_cli'], 109 | }, 110 | install_requires=REQUIRED, 111 | extras_require=EXTRAS, 112 | include_package_data=True, 113 | license='MIT', 114 | classifiers=[ 115 | # Trove classifiers 116 | # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers 117 | 'License :: OSI Approved :: MIT License', 118 | 'Programming Language :: Python', 119 | 'Programming Language :: Python :: 3', 120 | 'Programming Language :: Python :: 3.8', 121 | 'Programming Language :: Python :: Implementation :: CPython', 122 | 'Programming Language :: Python :: Implementation :: PyPy', 123 | 'Topic :: Text Processing :: Linguistic' 124 | ], 125 | # $ setup.py publish support. 126 | cmdclass={ 127 | 'upload': UploadCommand, 128 | } 129 | ) 130 | -------------------------------------------------------------------------------- /yaltai/cli/krakn.py: -------------------------------------------------------------------------------- 1 | import click 2 | import os 3 | import dataclasses 4 | from typing import cast 5 | from kraken.kraken import ( 6 | # Constants 7 | SEGMENTATION_DEFAULT_MODEL, 8 | # CLI Stuff 9 | message, logger, # Logics 10 | get_input_parser, partial 11 | ) 12 | from PIL import Image 13 | from kraken.containers import Segmentation 14 | from ultralytics import YOLO 15 | 16 | 17 | def segmenter(model, text_direction, mask, device, yolo_model, ignore_lines, deskew, max_angle, input, output) -> None: 18 | import json 19 | import yaltai.models.krakn 20 | import yaltai.models.yolo 21 | 22 | ctx = click.get_current_context() 23 | 24 | if ctx.meta['first_process']: 25 | if ctx.meta['input_format_type'] != 'image': 26 | input = get_input_parser(ctx.meta['input_format_type'])(input).imagename 27 | ctx.meta['first_process'] = False 28 | 29 | if 'base_image' not in ctx.meta: 30 | ctx.meta['base_image'] = input 31 | 32 | try: 33 | im = Image.open(input) 34 | except IOError as e: 35 | raise click.BadParameter(str(e)) 36 | 37 | if mask: 38 | try: 39 | mask = Image.open(mask) 40 | except IOError as e: 41 | raise click.BadParameter(str(e)) 42 | 43 | message(f'Segmenting {ctx.meta["orig_file"]}\t', nl=False) 44 | try: 45 | regions = yaltai.models.yolo.segment( 46 | yolo_model, input=input, 47 | apply_deskew=deskew, max_angle=max_angle 48 | ) 49 | res: Segmentation = yaltai.models.krakn.segment( 50 | im, text_direction, mask=mask, model=model, device=device, 51 | regions=regions, ignore_lignes=ignore_lines, 52 | raise_on_error=ctx.meta['raise_failed'], autocast=ctx.meta["autocast"] 53 | ) 54 | except Exception as E: 55 | if ctx.meta['raise_failed']: 56 | raise 57 | message('\u2717', fg='red') 58 | ctx.exit(1) 59 | 60 | if ctx.meta['last_process'] and ctx.meta['output_mode'] != 'native': 61 | with click.open_file(output, 'w', encoding='utf-8') as fp: 62 | fp = cast('IO[Any]', fp) 63 | logger.info('Serializing as {} into {}'.format(ctx.meta['output_mode'], output)) 64 | from kraken import serialization 65 | fp.write( 66 | serialization.serialize( 67 | results=res, 68 | image_size=im.size, 69 | template=ctx.meta['output_template'], 70 | template_source='custom' if ctx.meta['output_mode'] == 'template' else 'native', 71 | processing_steps=ctx.meta['steps'] 72 | ) 73 | ) 74 | else: 75 | with click.open_file(output, 'w') as fp: 76 | fp = cast('IO[Any]', fp) 77 | json.dump(dataclasses.asdict(res), fp) 78 | message('\u2713', fg='green') 79 | 80 | 81 | from kraken.kraken import cli as kcli 82 | 83 | 84 | @kcli.command('segment') 85 | @click.pass_context 86 | @click.option('-i', '--model', 87 | default=None, 88 | show_default=True, type=click.Path(exists=True), 89 | help='Baseline detection model to use') 90 | @click.option('-y', '--yolo', 91 | default=None, 92 | show_default=True, type=click.Path(exists=True), 93 | help='YOLO model to use') 94 | @click.option('-d', '--text-direction', default='horizontal-lr', 95 | show_default=True, 96 | type=click.Choice(['horizontal-lr', 'horizontal-rl', 97 | 'vertical-lr', 'vertical-rl']), 98 | help='Sets principal text direction') 99 | @click.option('-m', '--mask', show_default=True, default=None, 100 | type=click.File(mode='rb', lazy=True), help='Segmentation mask ' 101 | 'suppressing page areas for line detection. 0-valued image ' 102 | 'regions are ignored for segmentation purposes. Disables column ' 103 | 'detection.') 104 | @click.option('-d', '--deskew', show_default=True, default=False, is_flag=True, 105 | help='Prior to applying YOLO model, ' 106 | 'deskew the image: this will produced oriented bounding box. The final output' 107 | 'is realigned with the original image.') 108 | @click.option('--max-angle', show_default=True, default=10, type=float, 109 | help='Maximum deskewing angle') 110 | @click.option('-n', '--ignore-lines', show_default=True, default=False, is_flag=True, 111 | help='Does not run line segmentation through Kraken, only Zone from YOLO') 112 | def yaltai_segment(ctx, model, text_direction, mask, yolo, ignore_lines, deskew, max_angle): 113 | """ 114 | Segments page images into text lines. 115 | """ 116 | 117 | if not model: 118 | model = SEGMENTATION_DEFAULT_MODEL 119 | if not yolo: 120 | raise Exception("No YOLOv8 model given") 121 | ctx.meta['steps'].append({'category': 'processing', 122 | 'description': 'Baseline and region segmentation', 123 | 'settings': {'model': os.path.basename(model), 124 | 'text_direction': text_direction}}) 125 | 126 | from kraken.lib.vgsl import TorchVGSLModel 127 | message(f'Loading ANN {model}\t', nl=False) 128 | try: 129 | model = TorchVGSLModel.load_model(model) 130 | model.to(ctx.meta['device']) 131 | except Exception: 132 | if ctx.meta['raise_failed']: 133 | raise 134 | message('\u2717', fg='red') 135 | ctx.exit(1) 136 | 137 | message('\u2713', fg='green') 138 | 139 | yolo = YOLO(yolo) 140 | yolo.to(ctx.meta["device"]) 141 | 142 | return partial(segmenter, model, text_direction, mask, ctx.meta['device'], yolo, ignore_lines, deskew, max_angle) 143 | -------------------------------------------------------------------------------- /tests/test_kraken.py: -------------------------------------------------------------------------------- 1 | import os 2 | import logging 3 | import pytest 4 | import yaml 5 | from click.testing import CliRunner 6 | from ultralytics.utils import LOGGER 7 | from yaltai.cli.yaltai import yaltai_cli 8 | from kraken.lib.xml import XMLPage 9 | from collections import defaultdict 10 | import tempfile 11 | 12 | 13 | @pytest.fixture(scope='function') 14 | def custom_logger(): 15 | class CustomLoggingHandler(logging.Handler): 16 | def __init__(self): 17 | super().__init__() 18 | # A list to store log records (level and message) 19 | self.records = [] 20 | 21 | def emit(self, record): 22 | # Append a tuple of the log level and the message to the records list 23 | self.records.append((record.levelname, record.getMessage())) 24 | 25 | def clear(self): 26 | self.records = [] 27 | 28 | # Create a logger 29 | logger = LOGGER 30 | logger.setLevel(logging.DEBUG) # Set the logger to handle all log levels 31 | 32 | # Create and add the custom handler to the logger 33 | custom_handler = CustomLoggingHandler() 34 | logger.addHandler(custom_handler) 35 | 36 | # Clear the records list before each test 37 | custom_handler.records.clear() 38 | 39 | # Provide both the logger and handler for access during tests 40 | yield custom_handler 41 | 42 | custom_handler.records.clear() 43 | 44 | # Optional: Remove the handler after the test to prevent interference 45 | logger.removeHandler(custom_handler) 46 | 47 | 48 | def test_yaltai_single_alto_to_xml(custom_logger): 49 | """Ensures that we can convert to YOLO format""" 50 | runner = CliRunner() 51 | 52 | # Trigger a warning. 53 | result = runner.invoke( 54 | yaltai_cli, 55 | [ 56 | "kraken", 57 | "--alto", 58 | "-i", 59 | os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_files", "page1.jpg"), 60 | os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_files", "page1.xml"), 61 | "segment", 62 | "-y", 63 | os.path.join(os.path.abspath(os.path.dirname(__file__)), "nano-yolo-ladas.pt") 64 | ] 65 | ) 66 | assert result.exit_code == 0 67 | assert "page1.jpg: 640x352 2 GraphicZones, 1 MainZone-P-Continued, 1 MainZone-Sp, 2 QuireMarksZones" in "\n".join([ 68 | record[1] 69 | for record in custom_logger.records 70 | ]) 71 | 72 | page = XMLPage(os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_files", "page1.xml")) 73 | assert { 74 | region_type: [region.boundary for region in regions] 75 | for region_type, regions in page.regions.items() 76 | } == { 77 | 'GraphicZone': [[(614.0, 12.0), 78 | (2614.0, 12.0), 79 | (2614.0, 819.0), 80 | (614.0, 819.0), 81 | (614.0, 12.0)], 82 | [(720.0, 0.0), 83 | (2634.0, 0.0), 84 | (2634.0, 343.0), 85 | (720.0, 343.0), 86 | (720.0, 0.0)]], 87 | 'MainZone-P-Continued': [[(122.0, 1536.0), 88 | (2218.0, 1536.0), 89 | (2218.0, 2888.0), 90 | (122.0, 2888.0), 91 | (122.0, 1536.0)]], 92 | 'MainZone-Sp': [[(95.0, 2974.0), 93 | (2201.0, 2974.0), 94 | (2201.0, 4854.0), 95 | (95.0, 4854.0), 96 | (95.0, 2974.0)]], 97 | 'QuireMarksZone': [[(1617.0, 4877.0), 98 | (1825.0, 4877.0), 99 | (1825.0, 4980.0), 100 | (1617.0, 4980.0), 101 | (1617.0, 4877.0)], 102 | [(1531.0, 4841.0), 103 | (1814.0, 4841.0), 104 | (1814.0, 4983.0), 105 | (1531.0, 4983.0), 106 | (1531.0, 4841.0)]] 107 | } 108 | 109 | assert len([line.baseline for line in page.lines.values()]) 110 | # ToDo: Add a test to check for line being part of regions 111 | 112 | 113 | def test_alto_to_yolo_with_lines(custom_logger): 114 | """Test line region detection in ALTO to YOLO conversion""" 115 | runner = CliRunner() 116 | test_files_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_files") 117 | 118 | with tempfile.TemporaryDirectory() as tempdir: 119 | result = runner.invoke( 120 | yaltai_cli, 121 | [ 122 | "convert", "alto-to-yolo", 123 | os.path.join(test_files_dir, "page1.xml"), 124 | tempdir, 125 | "--line-as-region", "default" 126 | ] 127 | ) 128 | 129 | assert result.exit_code == 0 130 | assert os.path.exists(f"{tempdir}/labels/page1.txt") 131 | 132 | with open(f"{tempdir}/config.yml") as f: 133 | labelmap = yaml.safe_load(f)["names"] 134 | 135 | # Verify line detection 136 | zones = defaultdict(list) 137 | with open(f"{tempdir}/labels/page1.txt") as f: 138 | for line in f: 139 | z, *position = line.split() 140 | zones[labelmap[int(z)]].append(position) 141 | assert len(zones["default"]) == 19, "There should be 19 lines found" 142 | assert zones["default"][0] == ['0.536184', '0.111331', '0.244152', '0.027338'], "First line should be this one" 143 | -------------------------------------------------------------------------------- /yaltai/models/krakn.py: -------------------------------------------------------------------------------- 1 | from typing import Optional, Callable, Union, List, Dict, Any, Literal 2 | 3 | import PIL 4 | import logging 5 | import uuid 6 | import numpy as np 7 | import shapely.geometry as geom 8 | from kraken.blla import compute_segmentation_map, vec_lines 9 | 10 | from kraken.containers import BaselineLine, Region, Segmentation 11 | from kraken.lib.segmentation import (polygonal_reading_order, scale_regions, neural_reading_order, is_in_region) 12 | from kraken.lib import vgsl 13 | from kraken.lib.exceptions import KrakenInvalidModelException 14 | from kraken.lib.util import get_im_str 15 | 16 | logger = logging.getLogger(__name__) 17 | 18 | 19 | def region_to_objects(regions: Dict[str, List[List[int]]]) -> Dict[str, List[Region]]: 20 | new_regions = {} 21 | for region_type, list_of_regions in regions.items(): 22 | new_regions[region_type] = [ 23 | Region(id=str(uuid.uuid4()), boundary=x, tags={'type': region_type}) 24 | for x in list_of_regions 25 | ] 26 | return new_regions 27 | 28 | 29 | def segment(im: PIL.Image.Image, 30 | text_direction: Literal['horizontal-lr', 'horizontal-rl', 'vertical-lr', 'vertical-rl'] = 'horizontal-lr', 31 | mask: Optional[np.ndarray] = None, 32 | reading_order_fn: Callable = polygonal_reading_order, 33 | model: Union[List[vgsl.TorchVGSLModel], vgsl.TorchVGSLModel] = None, 34 | device: str = 'cpu', 35 | raise_on_error: bool = False, 36 | autocast: bool = False, 37 | regions: Optional[Dict[str, List[List[int]]]] = None, 38 | ignore_lignes: bool = False) -> Segmentation: 39 | r""" 40 | Segments a page into text lines using the baseline segmenter. 41 | 42 | Segments a page into text lines and returns the polyline formed by each 43 | baseline and their estimated environment. 44 | 45 | Args: 46 | im: Input image. The mode can generally be anything but it is possible 47 | to supply a binarized-input-only model which requires accordingly 48 | treated images. 49 | text_direction: Passed-through value for serialization.serialize. 50 | mask: A bi-level mask image of the same size as `im` where 0-valued 51 | regions are ignored for segmentation purposes. Disables column 52 | detection. 53 | reading_order_fn: Function to determine the reading order. Has to 54 | accept a list of tuples (baselines, polygon) and a 55 | text direction (`lr` or `rl`). 56 | model: One or more TorchVGSLModel containing a segmentation model. If 57 | none is given a default model will be loaded. 58 | device: The target device to run the neural network on. 59 | raise_on_error: Raises error instead of logging them when they are 60 | not-blocking 61 | autocast: Runs the model with automatic mixed precision 62 | 63 | Returns: 64 | A :class:`kraken.containers.Segmentation` class containing reading 65 | order sorted baselines (polylines) and their respective polygonal 66 | boundaries as :class:`kraken.containers.BaselineLine` records. The 67 | last and first point of each boundary polygon are connected. 68 | 69 | Raises: 70 | KrakenInvalidModelException: if the given model is not a valid 71 | segmentation model. 72 | KrakenInputException: if the mask is not bitonal or does not match the 73 | image size. 74 | 75 | Notes: 76 | Multi-model operation is most useful for combining one or more region 77 | detection models and one text line model. Detected lines from all 78 | models are simply combined without any merging or duplicate detection 79 | so the chance of the same line appearing multiple times in the output 80 | are high. In addition, neural reading order determination is disabled 81 | when more than one model outputs lines. 82 | """ 83 | # Unlike Kraken base implementation, we only accept Model and List of Models 84 | if isinstance(model, vgsl.TorchVGSLModel): 85 | model = [model] 86 | 87 | for nn in model: 88 | if nn.model_type != 'segmentation': 89 | raise KrakenInvalidModelException(f'Invalid model type {nn.model_type} for {nn}') 90 | if 'class_mapping' not in nn.user_metadata: 91 | raise KrakenInvalidModelException(f'Segmentation model {nn} does not contain valid class mapping') 92 | 93 | if ignore_lignes: 94 | return {'text_direction': text_direction, 95 | 'type': 'baselines', 96 | 'lines': [], 97 | 'regions': regions, 98 | 'script_detection': False} 99 | 100 | im_str = get_im_str(im) 101 | logger.info(f'Segmenting {im_str}') 102 | 103 | lines = [] 104 | order = None 105 | regions = region_to_objects(regions) 106 | multi_lines = False 107 | # flag to indicate that multiple models produced line output -> disable 108 | # neural reading order 109 | for net in model: 110 | if 'topline' in net.user_metadata: 111 | loc = {None: 'center', 112 | True: 'top', 113 | False: 'bottom'}[net.user_metadata['topline']] 114 | logger.debug(f'Baseline location: {loc}') 115 | 116 | rets = compute_segmentation_map(im, mask, net, device, autocast=autocast) 117 | 118 | # We can't clear the heatmap of regions because it would mess up 119 | # print(rets) 120 | if "regions" in rets: 121 | del rets["regions"] 122 | 123 | # flatten regions for line ordering/fetch bounding regions 124 | line_regs = [] 125 | suppl_obj = [] 126 | for cls, regs in regions.items(): 127 | line_regs.extend(regs) 128 | if rets['bounding_regions'] is not None and cls in rets['bounding_regions']: 129 | suppl_obj.extend(regs) 130 | 131 | # convert back to net scale 132 | suppl_obj = scale_regions([x.boundary for x in suppl_obj], 1/rets['scale']) 133 | line_regs = scale_regions([x.boundary for x in line_regs], 1/rets['scale']) 134 | 135 | _lines = vec_lines(**rets, 136 | regions=line_regs, 137 | text_direction=text_direction, 138 | suppl_obj=suppl_obj, 139 | topline=net.user_metadata['topline'] if 'topline' in net.user_metadata else False, 140 | raise_on_error=raise_on_error) 141 | 142 | if 'ro_model' in net.aux_layers: 143 | logger.info(f'Using reading order model found in segmentation model {net}.') 144 | _order = neural_reading_order(lines=_lines, 145 | regions=regions, 146 | text_direction=text_direction[-2:], 147 | model=net.aux_layers['ro_model'], 148 | im_size=im.size, 149 | class_mapping=net.user_metadata['ro_class_mapping']) 150 | else: 151 | _order = None 152 | 153 | if _lines and lines or multi_lines: 154 | multi_lines = True 155 | order = None 156 | logger.warning('Multiple models produced line output. This is ' 157 | 'likely unintended. Suppressing neural reading ' 158 | 'order.') 159 | else: 160 | order = _order 161 | 162 | lines.extend(_lines) 163 | 164 | # Rounding ! 165 | for reg_class in regions: 166 | for reg_obj in regions[reg_class]: 167 | reg_obj.boundary = list(map(lambda x: list(map(round, x)), reg_obj.boundary)) 168 | 169 | if len(rets['cls_map']['baselines']) > 1: 170 | script_detection = True 171 | else: 172 | script_detection = False 173 | 174 | # create objects and assign IDs 175 | blls = [] 176 | _shp_regs = {} 177 | for reg_type, rgs in regions.items(): 178 | for reg in rgs: 179 | _shp_regs[reg.id] = geom.Polygon(reg.boundary) 180 | 181 | # reorder lines 182 | logger.debug(f'Reordering baselines with main RO function {reading_order_fn}.') 183 | basic_lo = reading_order_fn(lines=lines, regions=_shp_regs.values(), text_direction=text_direction[-2:]) 184 | lines = [lines[idx] for idx in basic_lo] 185 | 186 | for line in lines: 187 | line_regs = [] 188 | for reg_id, reg in _shp_regs.items(): 189 | line_ls = geom.LineString(line['baseline']) 190 | if is_in_region(line_ls, reg): 191 | line_regs.append(reg_id) 192 | blls.append(BaselineLine(id=str(uuid.uuid4()), baseline=line['baseline'], boundary=line['boundary'], tags=line['tags'], regions=line_regs)) 193 | 194 | return Segmentation(text_direction=text_direction, 195 | imagename=getattr(im, 'filename', None), 196 | type='baselines', 197 | lines=blls, 198 | regions=regions, 199 | script_detection=script_detection, 200 | line_orders=[order] if order else []) 201 | 202 | 203 | 204 | -------------------------------------------------------------------------------- /tests/test_files/alto_dataset/Bruyere07.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | pixel 7 | 8 | Bruyere07.jpg 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 22 | 26 | 27 | 33 | 34 | 35 | 36 | 37 | 43 | 44 | 45 | 46 | 47 | 53 | 54 | 55 | 56 | 63 | 64 | 69 | 70 | 71 | 72 | 73 | 80 | 81 | 86 | 87 | 88 | 89 | 90 | 97 | 98 | 103 | 104 | 105 | 106 | 107 | 114 | 115 | 120 | 121 | 122 | 123 | 124 | 131 | 132 | 137 | 138 | 139 | 140 | 141 | 148 | 149 | 154 | 155 | 156 | 157 | 158 | 165 | 166 | 171 | 172 | 173 | 174 | 175 | 182 | 183 | 188 | 189 | 190 | 191 | 192 | 199 | 200 | 205 | 206 | 207 | 208 | 209 | 210 | 216 | 217 | 218 | 219 | 226 | 227 | 232 | 233 | 234 | 235 | 236 | 243 | 244 | 249 | 250 | 251 | 252 | 253 | 260 | 261 | 266 | 267 | 268 | 269 | 270 | 277 | 278 | 283 | 284 | 285 | 286 | 287 | 294 | 295 | 300 | 301 | 302 | 303 | 304 | 311 | 312 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | -------------------------------------------------------------------------------- /tests/test_files/alto_dataset/Bruyere09.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | pixel 7 | 8 | Bruyere09.jpg 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 22 | 26 | 27 | 33 | 34 | 35 | 36 | 43 | 44 | 49 | 50 | 51 | 52 | 53 | 60 | 61 | 66 | 67 | 68 | 69 | 70 | 77 | 78 | 83 | 84 | 85 | 86 | 87 | 94 | 95 | 100 | 101 | 102 | 103 | 104 | 111 | 112 | 117 | 118 | 119 | 120 | 121 | 128 | 129 | 134 | 135 | 136 | 137 | 138 | 145 | 146 | 151 | 152 | 153 | 154 | 155 | 162 | 163 | 168 | 169 | 170 | 171 | 172 | 179 | 180 | 185 | 186 | 187 | 188 | 189 | 196 | 197 | 202 | 203 | 204 | 205 | 206 | 213 | 214 | 219 | 220 | 221 | 222 | 223 | 230 | 231 | 236 | 237 | 238 | 239 | 240 | 247 | 248 | 253 | 254 | 255 | 256 | 257 | 264 | 265 | 270 | 271 | 272 | 273 | 274 | 281 | 282 | 287 | 288 | 289 | 290 | 291 | 292 | 298 | 299 | 300 | 301 | 308 | 309 | 314 | 315 | 316 | 317 | 318 | 319 | 325 | 326 | 327 | 328 | 329 | 335 | 336 | 337 | 338 | 345 | 346 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | -------------------------------------------------------------------------------- /yaltai/cli/yaltai.py: -------------------------------------------------------------------------------- 1 | """ This CLI provides tool to transform ALTO or PAGE to YOLOv5 Formats 2 | 3 | """ 4 | import glob 5 | import json 6 | import shutil 7 | import os 8 | import random 9 | import re 10 | import sys 11 | from typing import List, Optional, Union, Set, Dict 12 | from collections import Counter 13 | from pathlib import Path 14 | 15 | import numpy as np 16 | from tqdm import tqdm 17 | from PIL import Image 18 | import click 19 | import yaml 20 | import tabulate 21 | 22 | 23 | from kraken.kraken import message 24 | from kraken.lib.xml import XMLPage 25 | from yaltai.utils import AltoToYoloZone, parse_box_labels, read_labelmap, YoloV5Zone, read_manifest 26 | from mean_average_precision import MetricBuilder 27 | 28 | 29 | @click.group() 30 | def yaltai_cli(): 31 | """ `yaltai` commands provides conversion options """ 32 | 33 | 34 | @yaltai_cli.group("convert") 35 | def convert(): 36 | """ Converts formats to various other formats """ 37 | 38 | 39 | @convert.command("alto-to-yolo") 40 | @click.argument("input", type=click.Path(exists=True, dir_okay=False, file_okay=True), nargs=-1) 41 | @click.argument("output", type=click.Path(dir_okay=True, file_okay=False)) 42 | @click.option("--single-class", type=str, default=None, 43 | help="Map every class to a single one") 44 | @click.option("--ignore", type=str, default=None, multiple=True) 45 | @click.option("--manifest", type=click.Path(exists=True, dir_okay=False, file_okay=True), default=None, 46 | help="Path to a manifest file containing paths to ALTO-XML files [Use with shuffle].") 47 | @click.option("--train", type=click.Path(exists=True, dir_okay=False, file_okay=True), default=None, 48 | help="Path to a manifest file containing paths to ALTO-XML files for train only [Ignores shuffle].") 49 | @click.option("--val", type=click.Path(exists=True, dir_okay=False, file_okay=True), default=None, 50 | help="Path to a manifest file containing paths to ALTO-XML files for validation only [Ignores shuffle].") 51 | @click.option("--segmonto", type=click.Choice(["region", "subtype", "full"]), default=None, 52 | help="If you use Segmonto, helper to cut the class and merge them at different levels") 53 | # ToDo: Merge classes in single class 54 | @click.option("--shuffle", type=float, default=None, 55 | help="Split into train and val") 56 | @click.option("-l", "--labelmap", type=click.Path(exists=True, file_okay=True, dir_okay=False), 57 | help="Format for the score table", default=None, show_default=True) 58 | @click.option("--image/--no-image", type=bool, default=True, show_default=True, 59 | help="Copy images when converting ALTO to YOLOv5") 60 | @click.option("--line-as-region", type=str, multiple=True, 61 | help="Line-type that should be added for zone detection") 62 | def alto_to_yolo( 63 | input: Optional[List[click.Path]], 64 | output: click.Path, 65 | single_class: Optional[str], 66 | segmonto: Optional[str], 67 | shuffle: Optional[float], 68 | labelmap: Optional[str], 69 | image: bool, 70 | manifest: Optional[click.Path], 71 | train: Optional[click.Path], 72 | val: Optional[click.Path], 73 | line_as_region: Optional[List[str]], 74 | ignore: Optional[List[str]] 75 | ): 76 | 77 | """ Converts ALTO-XML files to YOLOv5 training files 78 | """ 79 | val_idx: Optional[int] = None 80 | input_paths: List[str] = [] 81 | 82 | if manifest: 83 | message("Using single manifest", fg="blue") 84 | input_paths = read_manifest(manifest) 85 | elif train and val: 86 | message("Using train and validation manifests", fg="blue") 87 | train = read_manifest(train) 88 | val = read_manifest(val) 89 | val_idx = len(train) 90 | input_paths = train + val 91 | else: 92 | message(f"Using list of inputs.", fg="blue") 93 | input_paths = list(map(str, input)) 94 | 95 | message(f"Found {len(input_paths)} to convert.", fg="blue") 96 | 97 | if val: 98 | message(f"{len(val)} image for validation.", fg='green') 99 | elif shuffle: 100 | random.shuffle(input_paths) 101 | val_idx = int(len(input_paths) * shuffle) 102 | message(f"{val_idx+1}/{len(input_paths)} image for validation.", fg='green') 103 | 104 | if shuffle or train: 105 | message(f"Shuffling data with a ratio of {shuffle} for validation.", fg='green') 106 | os.makedirs(f"{output}/train/labels", exist_ok=True) 107 | os.makedirs(f"{output}/val/labels", exist_ok=True) 108 | if image: 109 | os.makedirs(f"{output}/train/images", exist_ok=True) 110 | os.makedirs(f"{output}/val/images", exist_ok=True) 111 | else: 112 | os.makedirs(f"{output}/labels", exist_ok=True) 113 | if image: 114 | os.makedirs(f"{output}/images", exist_ok=True) 115 | 116 | def map_zones(zone_type: str) -> str: 117 | if segmonto: 118 | if segmonto == "full": 119 | return zone_type 120 | elif segmonto == "region": 121 | return re.search(r"([^:#]+)", zone_type).group() 122 | elif segmonto == "subtype": 123 | return re.search(r"([^#]+)", zone_type).group() 124 | elif single_class: 125 | return single_class 126 | return zone_type 127 | 128 | line_as_region: Set[str] = set(line_as_region or []) 129 | 130 | Zones: List[str] = [] 131 | if labelmap: 132 | Zones = read_labelmap(labelmap) 133 | 134 | ZoneCounter = Counter() 135 | 136 | # Count Zones 137 | for idx, file in tqdm(enumerate(input_paths)): 138 | parsed = XMLPage(file) 139 | image_path: Path = parsed.imagename 140 | # We record each region identifier and map the region if required 141 | regions = parsed.regions 142 | for region in regions: 143 | if map_zones(region) not in Zones and not map_zones(region) in ignore and not region in ignore: 144 | Zones.append(map_zones(region)) 145 | 146 | processed_lines: List[Dict] = [] 147 | 148 | if line_as_region: # ToDo: Adapt to new system 149 | for _, line_obj in parsed.lines.items(): 150 | if line_obj.tags.get("type") in line_as_region: 151 | line_type = line_obj.tags["type"] 152 | if line_type not in Zones: 153 | Zones.append(line_type) 154 | processed_lines.append(line_obj) 155 | 156 | # Retrieve image 157 | image_file = Image.open(image_path) 158 | width, height = image_file.width, image_file.height 159 | image_file.close() 160 | 161 | local_file: List[AltoToYoloZone] = [] 162 | for region, examples in regions.items(): 163 | mapped = map_zones(region) 164 | if region in ignore or mapped in ignore: 165 | continue 166 | region_id = Zones.index(mapped) 167 | for region_obj in examples: 168 | if region_obj.boundary: 169 | local_file.append( 170 | AltoToYoloZone( 171 | BOX=region_obj.boundary, 172 | PAGE_WIDTH=width, 173 | PAGE_HEIGHT=height, 174 | tag=region_id 175 | ) 176 | ) 177 | ZoneCounter[Zones[region_id]] += 1 178 | 179 | # This is only triggered if we have region_as_lines 180 | for line in processed_lines: 181 | if not line.boundary: 182 | continue 183 | region_id = Zones.index(line.tags["type"]) 184 | if Zones[region_id] in ignore: 185 | continue 186 | local_file.append( 187 | AltoToYoloZone( 188 | BOX=line.boundary, 189 | PAGE_WIDTH=width, 190 | PAGE_HEIGHT=height, 191 | tag=region_id 192 | ) 193 | ) 194 | ZoneCounter[Zones[region_id]] += 1 195 | 196 | path = output 197 | if shuffle: 198 | path = f"{output}/train" 199 | if idx <= val_idx: 200 | path = f"{output}/val" 201 | 202 | src_img = image_path 203 | ext = src_img.suffix[1:] # Suffix keeps the dot, we remove it 204 | simplified_name = src_img.stem 205 | 206 | if image: 207 | if ext.lower() not in {"jpg", "jpeg"}: 208 | # open image in png format 209 | img_png = Image.open(src_img) 210 | 211 | if img_png.mode == "RGBA": # Handle RGBA 212 | img_png = img_png.convert('RGB') 213 | 214 | # The image object is used to save the image in jpg format 215 | img_png.save(f"{path}/images/{simplified_name}.jpg") 216 | img_png.close() 217 | else: 218 | shutil.copy(src_img, f"{path}/images/{simplified_name}.jpg") 219 | 220 | with open(f"{path}/labels/{simplified_name}.txt", "w") as f: 221 | f.write("\n".join([loc.yoloV5() for loc in local_file if loc.yoloV5()])) 222 | 223 | message(f"{len(input_paths)} ground truth XML files converted.", fg='green') 224 | 225 | for zone in ZoneCounter: 226 | if ZoneCounter[zone] == 0 and not labelmap: 227 | Zones.pop(Zones.index(zone)) 228 | print(f"Zone {zone} removed from zones") 229 | for zone in ignore: 230 | if zone in Zones: 231 | Zones.pop(Zones.index(zone)) 232 | print(f"Zone {zone} removed from zones") 233 | 234 | with open(f"{output}/config.yml", "w") as f: 235 | data = { 236 | "train": os.path.abspath(output), 237 | "val": os.path.abspath(output), 238 | "nc": len(Zones), 239 | "names": Zones 240 | } 241 | if shuffle: 242 | data.update({ 243 | "train": f"{os.path.abspath(output)}/train/images", 244 | "val": f"{os.path.abspath(output)}/val/images" 245 | }) 246 | 247 | yaml.dump( 248 | data=data, 249 | stream=f, 250 | sort_keys=False 251 | ) 252 | 253 | with open(f"{output}/labelmap.txt", "w") as f: 254 | f.write("\n".join(Zones)) 255 | 256 | message(f"Configuration available at {output}/config.yml.", fg='green') 257 | message(f"Label Map available at {output}/labelmap.txt.", fg='green') 258 | 259 | message(f"Regions count:", fg='blue') 260 | for zone, cnt in ZoneCounter.items(): 261 | message(f"\t- {cnt:05} {zone}", fg='blue') 262 | 263 | 264 | @yaltai_cli.command("scores") 265 | @click.argument("gt-directory", type=click.Path(exists=True, dir_okay=True, file_okay=False)) 266 | @click.argument("pred-directory", type=click.Path(dir_okay=True, file_okay=False, exists=True)) 267 | @click.option("-t", "--threshold", type=float, help="IoU Threshold", default=.5, show_default=True) 268 | @click.option("-f", "--format", type=click.Choice(["markdown", "latex"]), 269 | help="Format for the score table", default="markdown", show_default=True) 270 | @click.option("-l", "--labelmap", type=click.Path(exists=True, file_okay=True, dir_okay=False), 271 | help="Labelmap to print nicely the information", default=None, show_default=True) 272 | @click.option("-j", "--save-json", type=click.File(mode="w"), 273 | help="JSON File to save information", default=None, show_default=True) 274 | def get_scores(gt_directory, pred_directory, threshold, format, labelmap, save_json): 275 | gt_directory = os.path.join(gt_directory, "*.txt") 276 | pred_directory = os.path.join(pred_directory, "*.txt") 277 | 278 | ground_truth, gt_arrays = parse_box_labels(sorted(glob.glob(gt_directory))) 279 | pred, pred_arrays = parse_box_labels(sorted(glob.glob(pred_directory)), gt=False) 280 | 281 | classes = np.unique( 282 | np.concatenate(( 283 | np.array([row for arr in gt_arrays for row in arr])[:, 4], 284 | np.array([row for arr in pred_arrays for row in arr])[:, 4] 285 | )) 286 | ).tolist() 287 | 288 | def reclass_classes(array_list: List[np.array]) -> None: 289 | for array in array_list: 290 | for row_idx in range(array.shape[0]): 291 | array[row_idx, 4] = classes.index(array[row_idx, 4].astype(int)) 292 | 293 | reclass_classes(gt_arrays) 294 | reclass_classes(pred_arrays) 295 | 296 | builder = MetricBuilder.build_evaluation_metric("map_2d", async_mode=False, num_classes=len(classes)) 297 | for pred_array, gt_array in zip(pred_arrays, gt_arrays): 298 | builder.add(pred_array, gt_array) 299 | 300 | metric = builder.value(iou_thresholds=threshold) 301 | print(f"Global mAP: {metric['mAP']}") 302 | 303 | if labelmap: 304 | labelmap = read_labelmap(labelmap) 305 | else: 306 | labelmap = list(range(max(classes) + 1)) 307 | 308 | table = [["Class", "AP", "Precision", "Recall", "Support"]] 309 | for cls_idx, cls_orig_idx in enumerate(classes): 310 | data = metric[0.5][cls_idx] 311 | ap, precision, recall, support = data["ap"], data["precision"].mean(), data["recall"].mean(), \ 312 | data["precision"].shape[0] 313 | table.append([labelmap[cls_orig_idx], ap, precision, recall, support]) 314 | 315 | print(tabulate.tabulate(table, tablefmt=format, floatfmt=".3f", headers="firstrow")) 316 | 317 | if save_json is not None: 318 | json.dump({ 319 | "mAP": float(metric["mAP"]), 320 | "classes": { 321 | row[0]: { 322 | "AP": float(row[1]), 323 | "Precision": float(row[2]), 324 | "Recall": float(row[2]), 325 | "Support": float(row[3]) 326 | } 327 | for row in table[1:] 328 | } 329 | }, save_json) 330 | 331 | 332 | @convert.command("yolo-to-alto") 333 | @click.argument("input", type=click.Path(exists=True, dir_okay=False, file_okay=True), nargs=-1) 334 | @click.option("-l", "--labelmap", type=click.Path(exists=True, file_okay=True, dir_okay=False), 335 | help="Labels", default=None, show_default=True) 336 | def yolo_to_alto(input, labelmap): 337 | """ Converts YOLOv5.txt files to ALTO files """ 338 | if not labelmap: 339 | message("No labelmap given, --labelmap is required for ALTO conversion", fg="red") 340 | sys.exit(0) 341 | 342 | labelmap = read_labelmap(labelmap) 343 | 344 | OtherTags = "\n".join([ 345 | f'' 346 | for idx, zone in enumerate(labelmap) 347 | ]) 348 | 349 | with open(os.path.join(os.path.dirname(__file__), "../template.xml")) as f: 350 | TEMPLATE = f.read() 351 | 352 | for file in input: 353 | xml_name = file[:-4] + ".xml" 354 | img_file_name = os.path.basename(file[:-4]) + ".jpg" 355 | zones = [] 356 | if os.path.exists(os.path.join(os.path.dirname(file), "..", "images", img_file_name)): 357 | img_name = os.path.join(os.path.dirname(file), "..", "images", img_file_name) 358 | img_for_xml_name = f"../images/{img_file_name}" 359 | elif os.path.exists(os.path.join(os.path.dirname(file), "../..", "images", img_file_name)): 360 | img_name = os.path.join(os.path.dirname(file), "../..", "images", img_file_name) 361 | img_for_xml_name = os.path.join("../..", img_file_name) 362 | else: 363 | message(f"Can't find the image for {img_file_name}") 364 | sys.exit(0) 365 | 366 | image = Image.open(img_name) 367 | img_width, img_height = image.size 368 | image.close() 369 | 370 | with open(file) as f: 371 | for line_idx, line in enumerate(f): 372 | z = YoloV5Zone.from_txt(*line.strip().split()[:5]) 373 | 374 | x0, y0, x1, y1 = z.xyxy 375 | x0, x1 = img_width*x0, img_width*x1 376 | y0, y1 = img_height*y0, img_height*y1 377 | x0, x1, y0, y1 = [int(z) for z in [x0, x1, y0, y1]] 378 | width = x1 - x0 379 | height = y1 - y0 380 | 381 | zones.append(f""" 382 | 386 | 387 | 388 | 389 | """) 390 | 391 | with open(xml_name, "w") as f: 392 | f.write( 393 | TEMPLATE.replace("%Filename%", img_for_xml_name) 394 | .replace("%Width%", str(img_width)) 395 | .replace("%Height%", str(img_height)) 396 | .replace("%Tags%", OtherTags) 397 | .replace("%Textblocks%", "".join(zones)) 398 | ) 399 | 400 | 401 | from yaltai.cli.krakn import kcli 402 | 403 | yaltai_cli.add_command(kcli, "kraken") 404 | 405 | 406 | if __name__ == "__main__": 407 | yaltai_cli() 408 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------