예제 #1
0
def ocr_page_image(doc_path, page_num, lang):
    """
    image = jpg, jpeg, png
    """
    logger.debug("OCR image (jpeg, jpg, png) document")

    page_url = PagePath(
        document_path=doc_path,
        page_num=page_num,
        step=Step(1),
        # jpeg, jpg, png are 1 page documents
        page_count=1)
    # resize and eventually convert (png -> jpg)
    resize_img(page_url, media_root=settings.MEDIA_ROOT)
    extract_txt(page_url, lang=lang, media_root=settings.MEDIA_ROOT)

    # First quickly generate preview images
    for step in Steps():
        page_url.step = step
        resize_img(page_url, media_root=settings.MEDIA_ROOT)
    # reset page's step
    page_url.step = Step(1)
    # Now OCR each image
    for step in Steps():
        if not step.is_thumbnail:
            extract_hocr(page_url, lang=lang, media_root=settings.MEDIA_ROOT)

    return page_url
예제 #2
0
def ocr_page_pdf(doc_path, page_num, lang):
    """
    doc_path is an mglib.path.DocumentPath instance
    """
    logger.debug("OCR PDF document")

    page_count = get_pagecount(default_storage.abspath(doc_path.url()))

    if page_num <= page_count:
        # first quickly generate preview images
        page_url = PagePath(document_path=doc_path,
                            page_num=page_num,
                            step=Step(1),
                            page_count=page_count)

        for step in Steps():
            page_url.step = step
            extract_img(page_url, media_root=settings.MEDIA_ROOT)

    if page_num <= page_count:
        page_url = PagePath(document_path=doc_path,
                            page_num=page_num,
                            step=Step(1),
                            page_count=page_count)
        extract_txt(page_url, lang=lang, media_root=settings.MEDIA_ROOT)

        for step in Steps():
            page_url.step = step
            if not step.is_thumbnail:
                extract_hocr(page_url,
                             lang=lang,
                             media_root=settings.MEDIA_ROOT)

    return page_url
예제 #3
0
def ocr_page_pdf(doc_path, page_num, lang, **kwargs):
    """
    doc_path is an mglib.path.DocumentPath instance

    On success returns ``mglib.path.PagePath`` instance.
    """
    logger.debug("OCR PDF document")

    file_name = kwargs.pop('file_name', None)

    if not file_name:
        file_name = doc_path.file_name

    page_count = get_pagecount(default_storage.abspath(doc_path.url()))

    if page_num <= page_count:
        # first quickly generate preview images
        page_path = PagePath(document_path=doc_path,
                             page_num=page_num,
                             step=Step(1),
                             page_count=page_count)
        for step in Steps():
            page_path.step = step
            extract_img(page_path, media_root=settings.MEDIA_ROOT)

    notify_pre_page_ocr(page_path,
                        page_num=page_num,
                        lang=lang,
                        file_name=doc_path.file_name,
                        **kwargs)

    if page_num <= page_count:
        page_path = PagePath(document_path=doc_path,
                             page_num=page_num,
                             step=Step(1),
                             page_count=page_count)
        extract_txt(page_path, lang=lang, media_root=settings.MEDIA_ROOT)
        notify_txt_ready(page_path,
                         page_num=page_num,
                         lang=lang,
                         file_name=file_name,
                         **kwargs)

        for step in Steps():
            page_path.step = step
            if not step.is_thumbnail:
                extract_hocr(page_path,
                             lang=lang,
                             media_root=settings.MEDIA_ROOT)
                notify_hocr_ready(
                    page_path,
                    page_num=page_num,
                    lang=lang,
                    # step as integer number
                    step=step.current,
                    file_name=file_name,
                    **kwargs)

    return page_path
예제 #4
0
def ocr_page_image(doc_path, page_num, lang, **kwargs):
    """
    image = jpg, jpeg, png

    On success returns ``mglib.path.PagePath`` instance.
    """
    logger.debug("OCR image (jpeg, jpg, png) document")

    page_path = PagePath(
        document_path=doc_path,
        page_num=page_num,
        step=Step(1),
        # jpeg, jpg, png are 1 page documents
        page_count=1)
    notify_pre_page_ocr(page_path,
                        page_num=page_num,
                        lang=lang,
                        file_name=doc_path.file_name,
                        **kwargs)
    # resize and eventually convert (png -> jpg)
    resize_img(page_path, media_root=settings.MEDIA_ROOT)
    extract_txt(page_path, lang=lang, media_root=settings.MEDIA_ROOT)
    notify_txt_ready(page_path,
                     page_num=page_num,
                     lang=lang,
                     file_name=doc_path.file_name,
                     **kwargs)

    # First quickly generate preview images
    for step in Steps():
        page_path.step = step
        resize_img(page_path, media_root=settings.MEDIA_ROOT)
    # reset page's step
    page_path.step = Step(1)
    # Now OCR each image
    for step in Steps():
        if not step.is_thumbnail:
            extract_hocr(page_path, lang=lang, media_root=settings.MEDIA_ROOT)
            notify_hocr_ready(
                page_path,
                page_num=page_num,
                lang=lang,
                # step as integer number
                step=step.current,
                file_name=doc_path.file_name,
                **kwargs)

    return page_path
예제 #5
0
    def paste_pages(self,
                    dest_doc_path,
                    data_list,
                    dest_doc_is_new=False,
                    after_page_number=False,
                    before_page_number=False):
        """
        Pastes pages in the document pointed by dest_doc_path
        from src_doc_path. Both dest and src are instances of
        mglib.path.DocumentPath
        """
        next_ver_dp = DocumentPath.copy_from(dest_doc_path,
                                             version=dest_doc_path.version + 1)
        self.make_sure_path_exists(self.abspath(next_ver_dp))

        pdftk.paste_pages(src=self.abspath(dest_doc_path),
                          dst=self.abspath(next_ver_dp),
                          data_list=data_list,
                          dst_doc_is_new=dest_doc_is_new,
                          after_page_number=after_page_number,
                          before_page_number=before_page_number)

        if not dest_doc_is_new:
            # migrate document's own pages from previous
            # version (this differs from pasting into newly
            # created docs)
            pcount = self.get_pagecount(dest_doc_path)
            data_list.insert(0, {
                'doc_path': dest_doc_path,
                'page_nums': list(range(1, pcount + 1))
            })

        dest_page_num = 1
        dest_page_count = sum([len(item['page_nums']) for item in data_list])
        for item in data_list:
            src_path = item['doc_path']
            for page_num in item['page_nums']:
                for step in Steps():
                    src_page_path = PagePath(
                        document_path=src_path,
                        page_num=int(page_num),
                        step=step,
                        page_count=self.get_pagecount(src_path))
                    dst_page_path = PagePath(document_path=next_ver_dp,
                                             page_num=dest_page_num,
                                             step=step,
                                             page_count=dest_page_count)
                    logger.debug(f"src={src_page_path}  dst={dst_page_path}")
                    self.copy_page(src_page_path=src_page_path,
                                   dst_page_path=dst_page_path)
                dest_page_num += 1

        return dest_doc_path.version + 1
예제 #6
0
    def reorder_pages(self, doc_path, new_order):
        """
        Reorders pages in the document pointed by doc_path.
        doc_path is an instance of mglib.path.DocumentPath

        In case of success returns document's new version.

        new_order is a list of following format:

            [
                {'page_num': 2, page_order: 1},
                {'page_num': 1, page_order: 2},
                {'page_num': 3, page_order: 3},
                {'page_num': 4, page_order: 4},
            ]
        Example above means that in current document of 4 pages,
        first page was swapped with second one.
        page_num    = older page order
        page_order  = current page order
        So in human language, each hash is read:
            <page_num> now should be <page_order>
        """
        src_doc_path = doc_path
        dst_doc_path = DocumentPath.copy_from(src_doc_path,
                                              version=doc_path.version + 1)
        self.make_sure_path_exists(self.abspath(dst_doc_path))

        stapler.reorder_pages(src=self.abspath(src_doc_path),
                              dst=self.abspath(dst_doc_path),
                              new_order=new_order)

        page_count = self.get_pagecount(doc_path)

        if len(new_order) > page_count:
            logger.error(
                f"deleted_pages({new_order}) > page_count({page_count})")
            return

        for item in new_order:
            for step in Steps():
                src_page_path = PagePath(document_path=src_doc_path,
                                         page_num=int(item['page_num']),
                                         step=step,
                                         page_count=len(new_order))
                dst_page_path = PagePath(document_path=dst_doc_path,
                                         page_num=int(item['page_order']),
                                         step=step,
                                         page_count=len(new_order))
                self.copy_page(src_page_path=src_page_path,
                               dst_page_path=dst_page_path)

        return doc_path.version + 1
예제 #7
0
    def delete_pages(self, doc_path, page_numbers, skip_migration=False):
        """
        Delets pages in the document pointed by doc_path.
        doc_path is an instance of mglib.path.DocumentPath

        In case of success returns document's new version.
        """

        if not isinstance(page_numbers, list):
            logger.error("Expecting list argument")
            return False

        src_doc_path = doc_path
        dst_doc_path = DocumentPath.copy_from(src_doc_path,
                                              version=doc_path.version + 1)
        self.make_sure_path_exists(self.abspath(dst_doc_path))
        stapler.delete_pages(self.abspath(src_doc_path),
                             self.abspath(dst_doc_path), page_numbers)

        if skip_migration:
            return doc_path.version + 1

        page_count = self.get_pagecount(doc_path)

        if len(page_numbers) > page_count:
            logger.error(
                f"deleted_pages({page_numbers}) > page_count({page_count})")
            return

        assigns = get_assigns_after_delete(total_pages=page_count,
                                           deleted_pages=page_numbers)
        for a in assigns:
            for step in Steps():
                src_page_path = PagePath(document_path=src_doc_path,
                                         page_num=a[1],
                                         step=step,
                                         page_count=page_count)
                dst_page_path = PagePath(document_path=dst_doc_path,
                                         page_num=a[0],
                                         step=step,
                                         page_count=page_count -
                                         len(page_numbers))
                self.copy_page(src_page_path=src_page_path,
                               dst_page_path=dst_page_path)

        return doc_path.version + 1
예제 #8
0
def upload_to(page_path, **kwargs):
    """
    Will upload results to remote storage if such storage
    is supported.

    Default storage class ``mglib.storage.FileSystemStorage``
    ``download`` and ``upload`` methods are empty.

    Method is useful for cloud production (where storage class
    will be replaced to support remote storage e.g. S3, SFTP)
    """
    default_storage.upload(page_path.txt_url(), **kwargs)

    for step in Steps():
        if not step.is_thumbnail:

            page_path.step = step
            default_storage.upload(page_path.hocr_url(), **kwargs)
            default_storage.upload(page_path.img_url(), **kwargs)
예제 #9
0
def ocr_page_pdf(doc_path, page_num, lang):
    """
    doc_path is an mglib.path.DocumentPath instance
    """
    page_count = get_pagecount(default_storage.abspath(doc_path.url()))
    if page_num <= page_count:
        page_url = PagePath(document_path=doc_path,
                            page_num=page_num,
                            step=Step(1),
                            page_count=page_count)
        extract_img(page_url, media_root=settings.MEDIA_ROOT)
        extract_txt(page_url, lang=lang, media_root=settings.MEDIA_ROOT)

        for step in Steps():
            page_url.step = step
            extract_img(page_url, media_root=settings.MEDIA_ROOT)
            # tesseract unterhalt-1.jpg page-1 -l deu hocr
            if not step.is_thumbnail:
                extract_hocr(page_url,
                             lang=lang,
                             media_root=settings.MEDIA_ROOT)

    return page_url