Skip to content
Snippets Groups Projects
Commit cc229a5a authored by Anton Akhmerov's avatar Anton Akhmerov Committed by Thomas Roos
Browse files

fix another bug in position calculation

parent 3e65f172
No related branches found
No related tags found
No related merge requests found
...@@ -217,9 +217,6 @@ def process_page(output_dir, image_data, exam_config): ...@@ -217,9 +217,6 @@ def process_page(output_dir, image_data, exam_config):
def extract_qr(image_data): def extract_qr(image_data):
"""Extract a QR code from a PIL Image.""" """Extract a QR code from a PIL Image."""
grayscale = np.asarray(image_data.convert(mode='L')) grayscale = np.asarray(image_data.convert(mode='L'))
# Apply portrait orientation.
if grayscale.shape[0] < grayscale.shape[1]:
grayscale = grayscale.T
# Empirically we observed that the most important parameter # Empirically we observed that the most important parameter
# for zbar to successfully read a qr code is the resolution # for zbar to successfully read a qr code is the resolution
...@@ -229,7 +226,8 @@ def extract_qr(image_data): ...@@ -229,7 +226,8 @@ def extract_qr(image_data):
for dirx, diry, factor in itertools.product([1, -1], [1, -1], [8, 5, 4, 3]): for dirx, diry, factor in itertools.product([1, -1], [1, -1], [8, 5, 4, 3]):
scaled = grayscale[::factor * dirx, ::factor * diry] scaled = grayscale[::factor * dirx, ::factor * diry]
scanner = zbar.Scanner() scanner = zbar.Scanner()
results = scanner.scan(scaled) # Filter only QR codes to eliminate some false positives.
results = [i for i in scanner.scan(scaled) if i.type == 'QR-Code']
if len(results) > 1: if len(results) > 1:
raise RuntimeError("Found > 1 QR code on the page.") raise RuntimeError("Found > 1 QR code on the page.")
if results: if results:
...@@ -240,8 +238,6 @@ def extract_qr(image_data): ...@@ -240,8 +238,6 @@ def extract_qr(image_data):
return return
coords = np.array(results[0].position) coords = np.array(results[0].position)
# zbar doesn't respect array ordering! # zbar doesn't respect array ordering!
if not np.isfortran(scaled):
coords = coords[:, ::-1]
coords *= [factor * dirx, factor * diry] coords *= [factor * dirx, factor * diry]
coords %= grayscale.shape coords %= grayscale.shape
return ExtractedQR(version, name, int(page), int(copy), coords) return ExtractedQR(version, name, int(page), int(copy), coords)
...@@ -258,19 +254,18 @@ def guess_dpi(image_array): ...@@ -258,19 +254,18 @@ def guess_dpi(image_array):
def rotate_and_shift(image_data, extracted_qr, qr_coords): def rotate_and_shift(image_data, extracted_qr, qr_coords):
"""Roll the image such that QR occupies coords specified by the template.""" """Roll the image such that QR occupies coords specified by the template."""
page, position = extracted_qr.page, extracted_qr.coords page, position = extracted_qr.page, extracted_qr.coords
y, x = np.mean(position, axis=0)
image = np.array(image_data) image = np.array(image_data)
if image.shape[0] < image.shape[1]: if image.shape[0] < image.shape[1]:
image = np.copy(np.transpose( image = np.transpose(image, [1, 0] + [2] * (len(image.shape) > 2))
image, ((1, 0) if len(image.shape) == 2 else (1, 0, 2)) x, y = y, x
))
dpi = guess_dpi(image) dpi = guess_dpi(image)
h, w, *_ = image.shape h, w, *_ = image.shape
qr_widget = qr_coords[qr_coords.page == page] qr_widget = qr_coords[qr_coords.page == page]
box = dpi * qr_widget[['top', 'bottom', 'left', 'right']].values[0] box = dpi * qr_widget[['top', 'bottom', 'left', 'right']].values[0]
y0, x0 = h - np.mean(box[:2]), np.mean(box[2:]) y0, x0 = h - np.mean(box[:2]), np.mean(box[2:])
y, x = np.mean(position, axis=0)
if (x > w / 2) != (x0 > w / 2): if (x > w / 2) != (x0 > w / 2):
image = image[:, ::-1] image = image[:, ::-1]
x = w - x x = w - x
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment