def test_ground(self): imgf = ImageFile(TEST_FILE) self.assertEqual(imgf.is_grounded, True) imgf.set_ground(imgf.ground.segments, imgf.ground.classes, write_file=False) self.assertEqual(imgf.is_grounded, True) imgf.remove_ground(remove_file=False) self.assertEqual(imgf.is_grounded, False)
class TestGrounding(unittest.TestCase): def setUp(self): self.img = ImageFile('digits1') self.img.remove_ground() self.assertFalse(self.img.is_grounded()) self.segments = ContourSegmenter().process(self.img.image) def test_textgrounder(self): grounder = TextGrounder() characters = "0" * len(self.segments) grounder.ground(self.img, self.segments, characters) self.assertTrue(self.img.is_grounded()) self.assertEquals(reconstruct_chars(self.img.ground.classes), characters) def test_textgrounder_wrong_len(self): grounder = TextGrounder() characters = "0" * len(self.segments) with self.assertRaises(ValueError): grounder.ground(self.img, self.segments, characters[:-4]) self.assertFalse(self.img.is_grounded()) def test_usergrounder(self): ESC_KEY = 27 ZERO_KEY = 48 keys = [ZERO_KEY] * len(self.segments) + [ESC_KEY] mock_generator = iter(keys) def mock_input(*args): return next(mock_generator) grounder = UserGrounder() with mock.patch('cv2.waitKey', mock_input): with mock.patch('cv2.imshow'): grounder.ground(self.img, self.segments) self.assertTrue(self.img.is_grounded()) self.assertEquals(reconstruct_chars(self.img.ground.classes), "0" * len(self.segments)) def test_terminal_grounder(self): terminal = TerminalGrounder() characters = "0" * len(self.segments) mock_input_gen = iter(characters) def mock_input(prompt): return next(mock_input_gen) with mock.patch('__builtin__.raw_input', mock_input): terminal.ground(self.img, self.segments) self.assertTrue(self.img.is_grounded()) self.assertEquals(reconstruct_chars(self.img.ground.classes), "0" * len(self.segments))
def test_ocr_digits(self): # get data from images img1 = ImageFile('digits1') img2 = ImageFile('digits2') ground_truth = img2.ground.classes img2.remove_ground() # create OCR segmenter = ContourSegmenter() extractor = SimpleFeatureExtractor() classifier = KNNClassifier() ocr = OCR(segmenter, extractor, classifier) # train and test ocr.train(img1) chars, classes, _ = ocr.ocr(img2, show_steps=False) self.assertEqual(list(classes), list(ground_truth)) self.assertEqual(chars, reconstruct_chars(ground_truth))