<commit_before>#!/usr/bin/env python
"""Saves cropped object images
"""

import argparse
import traceback

from pathlib import Path

# Import numpy here to prevent PyInstaller build from breaking
# TODO LH find a better solution
import numpy

import inselect
import inselect.lib.utils

from inselect.lib.document import InselectDocument
from inselect.lib.utils import debug_print


# TODO Recursive option
# TODO Ignore if existing crops dir; option to overwrite

def save_crops(dir, overwrite_existing):
    dir = Path(dir)
    for p in dir.glob('*' + InselectDocument.EXTENSION):
        try:
            debug_print('Loading [{0}]'.format(p))
            doc = InselectDocument.load(p)
            if not overwrite_existing and doc.crops_dir.is_dir():
                print('Crops dir [{0}] exists - skipping'.format(doc.crops_dir))
            else:
                print('Will save crops for [{0}] to [{1}]'.format(p, doc.crops_dir))

                debug_print('Loading full-resolution scanned image')
                doc.scanned.array

                debug_print('Saving crops')
                doc.save_crops()
        except Exception:
            print('Error saving crops from [{0}]'.format(p))
            traceback.print_exc()

<commit_msg>Fix save_crops<commit_after>#!/usr/bin/env python
"""Saves cropped object images
"""

import argparse
import traceback

from pathlib import Path

# Import numpy here to prevent PyInstaller build from breaking
# TODO LH find a better solution
import numpy

import inselect
import inselect.lib.utils

from inselect.lib.document import InselectDocument
from inselect.lib.document_export import DocumentExport
from inselect.lib.utils import debug_print


# TODO Recursive option
# TODO Ignore if existing crops dir; option to overwrite

def save_crops(dir, overwrite_existing):
    dir = Path(dir)
    export = DocumentExport()
    for p in dir.glob('*' + InselectDocument.EXTENSION):
        try:
            debug_print('Loading [{0}]'.format(p))
            doc = InselectDocument.load(p)
            if not overwrite_existing and doc.crops_dir.is_dir():
                print('Crops dir [{0}] exists - skipping'.format(doc.crops_dir))
            else:
                print('Will save crops for [{0}] to [{1}]'.format(p, doc.crops_dir))

                debug_print('Loading full-resolution scanned image')
                doc.scanned.array

                debug_print('Saving crops')
                export.save_crops(doc)
        except Exception:
            print('Error saving crops from [{0}]'.format(p))
            traceback.print_exc()


{'fraction_non_alphanumeric': 0.09565885516711486, 'fraction_numerical': 0.00384172109104879, 'mean_word_length': 2.7146932952924394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3374070', 'n_tokens_mistral': 751, 'n_tokens_neox': 713, 'n_words': 217}