Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore remap boxes #812

Merged
merged 6 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add functional test for complete prediction with trained predictor
  • Loading branch information
Rob192 committed Jan 25, 2022
commit a00136178f346409fcecc53e878507dc31e85b01
7 changes: 0 additions & 7 deletions doctr/utils/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,6 @@ def remap_boxes(
orig_height, orig_width = orig_shape
dest_height, dest_width = dest_shape
mboxes = loc_preds.copy()
# # remaps position of the box center for the destination image shape
# mboxes[:, 0] = ((loc_preds[:, 0] * orig_width) + (dest_width - orig_width) / 2) / dest_width
# mboxes[:, 1] = ((loc_preds[:, 1] * orig_height) + (dest_height - orig_height) / 2) / dest_height
# # remaps box dimension for the destination image shape
# mboxes[:, 2] = loc_preds[:, 2] * orig_width / dest_width
# mboxes[:, 3] = loc_preds[:, 3] * orig_height / dest_height

mboxes[:, :, 0] = ((loc_preds[:, :, 0] * orig_width) + (dest_width - orig_width) / 2) / dest_width
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand well the computation here: If we have relative coordinates [[x1, y1], [x2, y2], [x3, y3], ...] we just need to multiply by dest_width & dest_height to get the absolute points coordinates in dest_shape referential, or am I missing something ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @charlesmindee, no you need to take into account the padding that was introduced by rotate_image. This is the main point of this function remap_boxes that is here to accommodate these differences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added pictures for reference in #800

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks I see the point

mboxes[:, :, 1] = ((loc_preds[:, :, 1] * orig_height) + (dest_height - orig_height) / 2) / dest_height

Expand Down
20 changes: 3 additions & 17 deletions tests/common/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,6 @@ def mock_image(tmpdir_factory):
return image


@pytest.fixture(scope="session")
def mock_tilted_payslip(tmpdir_factory):
url = 'https://3.bp.blogspot.com/-Es0oHTCrVEk/UnYA-iW9rYI/AAAAAAAAAFI/hWExrXFbo9U/s1600/003.jpg'
file = BytesIO(requests.get(url).content)
folder = tmpdir_factory.mktemp("images")
fn = str(folder.join("mock_payslip.jpeg"))
with open(fn, 'wb') as f:
f.write(file.getbuffer())
image = reader.read_img_as_numpy(fn)
image = geometry.rotate_image(image, 30, expand=True)
return image


@pytest.fixture(scope="function")
def mock_bitmap(mock_image):
bitmap = np.squeeze(cv2.cvtColor(mock_image, cv2.COLOR_BGR2GRAY) / 255.)
Expand All @@ -107,7 +94,7 @@ def test_get_bitmap_angle(mock_bitmap):
assert abs(angle - 30.) < 1.


def test_estimate_orientation(mock_image):
def test_estimate_orientation(mock_image, mock_tilted_payslip):
assert estimate_orientation(mock_image * 0) == 0

angle = estimate_orientation(mock_image)
Expand All @@ -117,10 +104,9 @@ def test_estimate_orientation(mock_image):
angle_rotated = estimate_orientation(rotated)
assert abs(angle_rotated) < 1.


def test_estimate_orientation(mock_tilted_payslip):
mock_tilted_payslip = reader.read_img_as_numpy(mock_tilted_payslip)
assert (estimate_orientation(mock_tilted_payslip) - 30.) < 1.

rotated = geometry.rotate_image(mock_tilted_payslip, -30, expand=True)
angle_rotated = estimate_orientation(rotated)
assert abs(angle_rotated) < 1.
assert abs(angle_rotated) < 1.
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import tempfile
from io import BytesIO

import cv2
import fitz
import hdf5storage
import numpy as np
import pytest
import requests
import scipy.io as sio

from doctr.io import reader
from doctr.utils import geometry


@pytest.fixture(scope="session")
def mock_vocab():
Expand All @@ -35,6 +39,26 @@ def mock_pdf(tmpdir_factory):
return str(fn)


@pytest.fixture(scope="session")
def mock_payslip(tmpdir_factory):
url = 'https://3.bp.blogspot.com/-Es0oHTCrVEk/UnYA-iW9rYI/AAAAAAAAAFI/hWExrXFbo9U/s1600/003.jpg'
file = BytesIO(requests.get(url).content)
folder = tmpdir_factory.mktemp("data")
fn = str(folder.join("mock_payslip.jpeg"))
with open(fn, 'wb') as f:
f.write(file.getbuffer())
return fn


@pytest.fixture(scope="session")
def mock_tilted_payslip(mock_payslip, tmpdir_factory):
image = reader.read_img_as_numpy(mock_payslip)
image = geometry.rotate_image(image, 30, expand=True)
tmp_path = str(tmpdir_factory.mktemp("data").join("mock_tilted_payslip.jpg"))
cv2.imwrite(tmp_path, image)
return tmp_path


@pytest.fixture(scope="session")
def mock_text_box_stream():
url = 'https://www.pngitem.com/pimgs/m/357-3579845_love-neon-loveislove-word-text-typography-freetoedit-picsart.png'
Expand Down
32 changes: 32 additions & 0 deletions tests/tensorflow/test_models_zoo_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from doctr.io import Document, DocumentFile
from doctr.models import detection, recognition
from doctr.models.detection.predictor import DetectionPredictor
from doctr.models.detection.zoo import detection_predictor
from doctr.models.predictor import OCRPredictor
from doctr.models.preprocessor import PreProcessor
from doctr.models.recognition.predictor import RecognitionPredictor
from doctr.models.recognition.zoo import recognition_predictor


@pytest.mark.parametrize(
Expand Down Expand Up @@ -54,6 +56,36 @@ def test_ocrpredictor(mock_pdf, mock_vocab, assume_straight_pages, straighten_pa
_ = predictor([input_page])


def test_trained_ocr_predictor(mock_tilted_payslip):
doc = DocumentFile.from_images(mock_tilted_payslip)

det_predictor = detection_predictor('db_resnet50', pretrained=True, batch_size=2, assume_straight_pages=True)
reco_predictor = recognition_predictor('crnn_vgg16_bn', pretrained=True, batch_size=128)

predictor = OCRPredictor(
det_predictor,
reco_predictor,
assume_straight_pages=True,
straighten_pages=True,
)

out = predictor(doc)

assert out.pages[0].blocks[0].lines[0].words[0].value == 'Mr.'
geometry_mr = np.array([[0.08844472, 0.35763523],
[0.11625107, 0.34320644],
[0.12588427, 0.35771032],
[0.09807791, 0.37213911]])
assert np.allclose(np.array(out.pages[0].blocks[0].lines[0].words[0].geometry), geometry_mr)

assert out.pages[0].blocks[1].lines[0].words[-1].value == 'revised'
geometry_revised = np.array([[0.50422498, 0.19551784],
[0.55741975, 0.16791493],
[0.56705294, 0.18241881],
[0.51385817, 0.21002172]])
assert np.allclose(np.array(out.pages[0].blocks[1].lines[0].words[-1].geometry), geometry_revised)


@pytest.mark.parametrize(
"det_arch, reco_arch",
[
Expand Down