Skip to content

Commit

Permalink
Update to v0.6 samples, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sz3 committed Feb 18, 2024
1 parent e27511d commit f6a3522
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cimbar/cimbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ def _decode_cell(ct, img, x, y, drift):
def _preprocess_for_decode(img):
''' This might need to be conditional based on source image size.'''
img = cv2.cvtColor(numpy.array(img), cv2.COLOR_RGB2BGR)
#kernel = numpy.array([[-1.0,-1.0,-1.0], [-1.0,8.5,-1.0], [-1.0,-1.0,-1.0]])
#img = cv2.filter2D(img, -1, kernel)
kernel = numpy.array([[-1.0,-1.0,-1.0], [-1.0,8.5,-1.0], [-1.0,-1.0,-1.0]])
img = cv2.filter2D(img, -1, kernel)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = Image.fromarray(img)
return img
Expand Down
2 changes: 1 addition & 1 deletion samples
8 changes: 4 additions & 4 deletions tests/test_cimb_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def test_decode_dark(self):
self.assertEqual(decoded, 5)
self.assertEqual(error, 0)

color = cimb.decode_color(img)
self.assertEqual(color, 0 << 4)
color = cimb.decode_color(img, 0)
self.assertEqual(color, 1)

img2 = Image.open(path.join(CIMBAR_ROOT, 'tests', 'sample', '25.png'))
img2 = img2.convert('RGB')
decoded, error = cimb.decode_symbol(img2)
self.assertEqual(decoded, 5)
self.assertEqual(error, 0)

color = cimb.decode_color(img2)
self.assertEqual(color, 1 << 4)
color = cimb.decode_color(img2, 0)
self.assertEqual(color, 2)
27 changes: 21 additions & 6 deletions tests/test_cimbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from os import path
from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import patch

import cv2
import numpy

from cimbar.cimbar import encode, decode, bits_per_op
from cimbar.encode.rss import reed_solomon_stream
from cimbar.grader import evaluate as evaluate_grader
from cimbar.grader import evaluate_split, evaluate_interleaved


CIMBAR_ROOT = path.abspath(path.join(path.dirname(path.realpath(__file__)), '..'))
Expand Down Expand Up @@ -78,7 +79,7 @@ def validate_output(self, out_path):
self.assertEqual(contents, self._src_data()[:7500])

def validate_grader(self, out_path, target):
num_bits = evaluate_grader(self.decode_clean, out_path, bits_per_op(), True)
num_bits = evaluate_split(self.decode_clean, out_path, bits_per_op(), 2)
self.assertLess(num_bits, target)

def test_decode_simple(self):
Expand Down Expand Up @@ -112,7 +113,8 @@ def test_decode_perspective_rotate(self):
decode([skewed_image], out_no_ecc, dark=True, ecc=0, force_preprocess=True)
self.validate_grader(out_no_ecc, 4000)

def test_decode_sample(self):
@patch('cimbar.cimbar.use_split_mode', lambda: False)
def test_decode_sample_mode4c(self):
clean_image = 'samples/6bit/4color_ecc30_0.png'
warped_image = 'samples/6bit/4_30_802.jpg'

Expand All @@ -122,7 +124,20 @@ def test_decode_sample(self):
warped_bits = self._temp_path('outfile_warped.txt')
decode([warped_image], warped_bits, dark=True, ecc=0, force_preprocess=True, auto_dewarp=False)

num_bits = evaluate_grader(clean_bits, warped_bits, bits_per_op(), True)
num_bits = evaluate_interleaved(clean_bits, warped_bits, bits_per_op())
self.assertLess(num_bits, 350)

def test_decode_sample_modeb(self):
clean_image = 'samples/b/tr_0.png'
warped_image = 'samples/b/ex2434.jpg'

clean_bits = self._temp_path('outfile_clean.txt')
decode([clean_image], clean_bits, dark=True, ecc=0, auto_dewarp=False)

warped_bits = self._temp_path('outfile_warped.txt')
decode([warped_image], warped_bits, dark=True, ecc=0, force_preprocess=True, auto_dewarp=False)

num_bits = evaluate_split(clean_bits, warped_bits, bits_per_op(), 2)
self.assertLess(num_bits, 350)


Expand All @@ -145,10 +160,10 @@ def test_roundtrip(self):
encode(self.src_file, dst_image, dark=True, fountain=True)

out_path = path.join(self.temp_dir.name, 'out.txt')
decode([dst_image], out_path, dark=True, deskew=False, auto_dewarp=False, fountain=True)
decode([dst_image], out_path, dark=True, deskew=False, auto_dewarp=False, fountain=True, color_correct=3)

with open(out_path, 'rb') as f:
contents = f.read()
with open(self.src_file, 'rb') as f:
expected = f.read()
self.assertEquals(contents, expected)
self.assertEqual(contents, expected)
2 changes: 1 addition & 1 deletion tests/test_fountain.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_encode(self):
data = b'0123456789' * 100
inbuff = BytesIO(data)

fes = fountain_encoder_stream(inbuff, 400)
fes = fountain_encoder_stream(inbuff, 400, encode_id=0)
r = fes.read(400)

self.assertEqual(b'\x00\x00\x03\xe8\x00\x00' + data[:394], r)
Expand Down

0 comments on commit f6a3522

Please sign in to comment.