File size: 10,405 Bytes
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5ea0f1
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5ea0f1
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5ea0f1
 
4433b8c
 
d5ea0f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4433b8c
 
 
 
 
 
d5ea0f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5ea0f1
 
 
 
 
 
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0bdc40
 
 
 
4433b8c
 
 
 
d5ea0f1
 
 
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
d5ea0f1
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5ea0f1
4433b8c
d5ea0f1
4433b8c
 
b0bdc40
 
 
 
 
4433b8c
 
 
 
 
 
 
 
 
 
 
 
 
d5ea0f1
 
4433b8c
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import json
import os
from pathlib import Path
import shutil
import warnings

from PIL import Image
from dawsonia import io
from dawsonia import digitize
from dawsonia.ml import ml
import gradio as gr
import numpy as np
from numpy.typing import NDArray
import pandas as pd
import pooch

from .visualizer_functions import Page, TableCell

# Max number of images a user can upload at once
MAX_IMAGES = int(os.environ.get("MAX_IMAGES", 5))

# Setup the cache directory to point to the directory where the example images
# are located. The images must lay in the cache directory because otherwise they
# have to be reuploaded when drag-and-dropped to the input image widget.
GRADIO_CACHE = os.getenv("GRADIO_CACHE_DIR", ".gradio_cache")
DATA_CACHE = os.path.join(GRADIO_CACHE, "data")
EXAMPLES_DIRECTORY = os.path.join(os.getcwd(), "examples")

# Example books
PIPELINES: dict[str, dict[str, str]] = {
    "bjuröklubb": dict(
        url="https://git.smhi.se/ai-for-obs/data/-/raw/688c04f13e8e946962792fe4b4e0ded98800b154/raw_zarr/BJUR%C3%96KLUBB/DAGBOK_Bjur%C3%B6klubb_Station_Jan-Dec_1928.zarr.zip",
        known_hash="sha256:6d87b7f79836ae6373cfab11260fe28787d93fe16199fefede6697ccd750f71a",
    ),
    "härnösand": dict(
        url="https://git.smhi.se/ai-for-obs/data/-/raw/688c04f13e8e946962792fe4b4e0ded98800b154/raw_zarr/H%C3%84RN%C3%96SAND/DAGBOK_H%C3%A4rn%C3%B6sand_Station_1934.zarr.zip",
        known_hash="sha256:a58fdb6521214d0bd569c9325ce78d696738de28ce6ec869cde0d46616b697f2",
    ),
}


def run_dawsonia(
    table_fmt_config_override,
    first_page,
    last_page,
    prob_thresh,
    book: io.Book,
    book_path,
    gallery,
    progress=gr.Progress(),
):
    if book is None:
        raise ValueError("You need to select / upload the pages to digitize")

    progress(0, desc="Dawsonia: starting")

    model_path = Path("data/models/dawsonia/2024-07-02")
    output_path = Path("output")
    output_path.mkdir(exist_ok=True)

    print("Dawsonia: digitizing", book)
    table_fmt = book.table_format

    final_output_path_book = output_path / book.station_name
    output_path_book = Path(book_path).parent / "output"
    output_path_book.mkdir(exist_ok=True, parents=True)
    (output_path_book / "probablities").mkdir(exist_ok=True)

    init_data: list[dict[str, NDArray]] = [
        {
            key: np.empty(len(table_fmt.rows), dtype="O")
            for key in table_fmt.columns[table_idx]
        }
        for table_idx in table_fmt.preproc.idx_tables_size_verify
    ]

    collection = []
    images = []

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", FutureWarning)
        for page_number in range(first_page, last_page):
            output_path_page = output_path_book / str(page_number)
            gr.Info(f"Digitizing {page_number = }")

            if (
                not (output_path_book / str(page_number))
                .with_suffix(".parquet")
                .exists()
            ):
                digitize.digitize_page_and_write_output(
                    book,
                    init_data,
                    page_number=page_number,
                    date_str=f"0000-page-{page_number}",
                    model_path=model_path,
                    model_predict=ml.model_predict,
                    prob_thresh=prob_thresh,
                    output_path_page=output_path_page,
                    output_text_fmt=False,
                    debug=False,
                )
            _synctree(output_path_book, final_output_path_book)

            progress_value = (page_number - first_page) / max(1, last_page - first_page)

    # if final_output_path_book.exists():
    #     shutil.rmtree(final_output_path_book)

    # shutil.copytree(output_path_book, final_output_path_book)
    for page_number, im_from_gallery in zip(range(first_page, last_page), gallery):
        if results := read_page(
            final_output_path_book,
            str(page_number),
            prob_thresh,
            progress,
            1.0,
            table_fmt.preproc.idx_tables_size_verify,
        ):  # , im_from_gallery[0])
            page, im = results
            collection.append(page)
            images.append(im)
            yield collection, gr.skip()
        else:
            gr.Info(f"No tables detected in {page_number = }")

    gr.Info("Pages were succesfully digitized ✨")

    # yield collection, images
    yield collection, gr.skip()


def _synctree(source_dir, dest_dir):
    source_dir = Path(source_dir)
    dest_dir = Path(dest_dir)
    if not dest_dir.exists():
        dest_dir.mkdir(parents=True)

    for root, _, files in os.walk(source_dir):
        root = Path(root)
        relative_root = root.relative_to(source_dir)

        # Create subdirectories in the destination directory
        dest_subdir_path = dest_dir / relative_root
        if not dest_subdir_path.exists():
            dest_subdir_path.mkdir(parents=True, exist_ok=True)

        for file_ in files:
            source_file_path = root / file_
            dest_file_path = dest_subdir_path / file_

            # Copy only if the file does not already exist or is newer
            if (
                not dest_file_path.exists()
                or (source_file_path.stat().st_mtime - dest_file_path.stat().st_mtime) > 0
            ):
                shutil.copy2(source_file_path, dest_file_path)


def read_page(
    output_path_book: Path,
    prefix: str,
    prob_thresh: float,
    progress,
    progress_value,
    idx_tables_size_verify: list[int],
    im_path_from_gallery: str = "",
):
    stats = digitize.Statistics.from_json(
        (output_path_book / "statistics" / prefix).with_suffix(".json")
    )
    print(stats)
    progress(progress_value, desc=f"Dawsonia: {stats!s:.50}")
    if stats.tables_detected > 0:
        values_df = pd.read_parquet((output_path_book / prefix).with_suffix(".parquet"))
        prob_df = pd.read_parquet(
            (output_path_book / "probablities" / prefix).with_suffix(".parquet")
        )
        table_meta = json.loads(
            (output_path_book / "table_meta" / prefix).with_suffix(".json").read_text()
        )
        with Image.open(
            image_path := (output_path_book / "pages" / prefix).with_suffix(".webp")
        ) as im:
            width = im.width
            height = im.height

        values_array = values_df.values.flatten()
        prob_array = prob_df.values.flatten()
        # FIXME: hardcoded to get upto 2 tables. Use idx_tables_size_verify and reconstruct bbox_array
        try:
            bbox_array = np.hstack(table_meta["table_positions"][:2]).reshape(-1, 4)
        except ValueError:
            bbox_array = np.reshape(table_meta["table_positions"][0], (-1, 4))

        cells = [
            make_cell(value, bbox)
            for value, prob, bbox in zip(values_array, prob_array, bbox_array)
            if prob > prob_thresh
        ]

        return Page(width, height, cells, im_path_from_gallery or str(image_path)), im


def make_cell(value: str, bbox: NDArray[np.int64]):
    y, x, h, w = bbox
    xmin, ymin = x - w // 2, y - h // 2
    xmax, ymax = x + w // 2, y + h // 2
    polygon = (xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax), (xmin, ymin)
    return TableCell(polygon, text_x=x - w // 4, text_y=y, text=value)


def all_example_images() -> list[str]:
    """
    Get paths to all example images.
    """
    examples = [
        os.path.join(EXAMPLES_DIRECTORY, f"{pipeline}.png") for pipeline in PIPELINES
    ]
    return examples


def get_selected_example_image(
    first_page, last_page, event: gr.SelectData
) -> tuple[list[Image.Image], io.Book, str, str, str] | None:
    """
    Get the name of the pipeline that corresponds to the selected image.
    """
    orig_name = event.value["image"]["orig_name"]
    # for name, details in PIPELINES.items():
    orig_path = Path(orig_name)
    name = orig_path.name
    for suffix in orig_path.suffixes[::-1]:
        name = name.removesuffix(suffix)

    station_tf = Path("table_formats", name).with_suffix(".toml")

    if (last_page - first_page) > MAX_IMAGES:
        error = f"Maximum images you can digitize is set to: {MAX_IMAGES}"
        gr.Warning(error)
        raise ValueError(error)

    if name in PIPELINES:
        book_path = pooch.retrieve(**PIPELINES[name], path=DATA_CACHE)
        first, last, book = io.read_book(book_path)
        book._name = name
        book.size_cell = [1.0, 1.0, 1.0, 1.0]
        return (
            [book.read_image(pg) for pg in range(first_page, last_page)],
            book,
            book_path,
            station_tf.name,
            station_tf.read_text(),
        )


def move_uploaded_file(uploaded, table_fmt_filename):
    current_directory = Path(uploaded).parent

    # Define the target directory where you want to save the uploaded files
    target_directory = current_directory / table_fmt_filename.removesuffix(".toml")
    os.makedirs(target_directory, exist_ok=True)

    # Move the uploaded file to the target directory
    true_path = Path(target_directory / Path(uploaded).name)
    # if true_path.exists():
    #     true_path.unlink()

    shutil.copy2(uploaded, true_path)
    print(f"Copy created", true_path)
    return str(true_path)


def get_uploaded_image(
    first_page: int, last_page: int, table_fmt_filename: str, filename: str
) -> tuple[list[NDArray], io.Book, str, str] | None:

    orig_path = Path(filename)
    name = orig_path.name
    for suffix in orig_path.suffixes[::-1]:
        name = name.removesuffix(suffix)

    station_tf = Path("table_formats", table_fmt_filename)
    if not station_tf.exists():
        station_tf = Path("table_formats", "bjuröklubb.toml")

    first, last, book = io.read_book(Path(filename))
    book._name = name
    book.size_cell = [1.0, 1.0, 1.0, 1.0]
    return (
        [book.read_page(pg) for pg in range(first_page, last_page)],
        book,
        filename,
        station_tf.read_text(),
    )


def overwrite_table_format_file(book: io.Book, book_path, table_fmt: str):
    name = book.station_name
    table_fmt_dir = Path("table_formats")
    (table_fmt_dir / name).with_suffix(".toml").write_text(table_fmt)
    book.table_format = io.read_specific_table_format(table_fmt_dir, Path(book_path))
    gr.Info(f"Overwritten table format file for {name}")
    return book