示例#1
0
 def test_text_bold(self):
     finder = TextFinder()
     matches = finder.find(Text('Find the word'), Image('sentence_bold'))
     self.assertEqual(len(matches), 1)
     self.assertEqual(matches[0].x, 12)
     self.assertEqual(matches[0].y, 13)
     self.assertAlmostEqual(matches[0].width, 100, delta=5)
     self.assertAlmostEqual(matches[0].height, 10, delta=5)
示例#2
0
 def test_text_larger(self):
     finder = TextFinder()
     matches = finder.find(Text('Find the word'), Image('sentence_larger'))
     self.assertEqual(len(matches), 1)
     # TODO: location too far due to poor text detection
     #self.assertEqual(matches[0].x, 13)
     self.assertEqual(matches[0].y, 13)
     #self.assertAlmostEqual(matches[0].width, 100, delta=5)
     self.assertAlmostEqual(matches[0].height, 10, delta=5)
示例#3
0
 def test_text_basic(self):
     finder = TextFinder()
     matches = finder.find(Text('Find the word here'),
                           Image('sentence_sans'))
     self.assertEqual(len(matches), 1)
     # TODO: location too far due to poor text detection
     #self.assertEqual(matches[0].x, 11)
     self.assertEqual(matches[0].y, 12)
     self.assertAlmostEqual(matches[0].width, 115, delta=5)
     self.assertAlmostEqual(matches[0].height, 10, delta=5)
示例#4
0
    def test_text_nomatch(self):
        finder = TextFinder()
        finder.params["find"]["similarity"].value = 0.25
        i = 1

        for tdetect in finder.algorithms["text_detectors"]:
            # TODO: this is still not implemented
            if tdetect == "components":
                continue
            for ocr in finder.algorithms["text_recognizers"]:
                # TODO: this is still not implemented
                if ocr == "beamSearch":
                    continue
                # TODO: handle newer OpenCV bugs with some backends
                import cv2
                # TODO: deprecate OpenCV 3.X versions after time
                if cv2.__version__.startswith("3.") and tdetect == "east":
                    continue

                finder.configure_backend(tdetect, "tdetect")
                finder.configure_backend(ocr, "ocr")
                # also with customized synchronization to the configuration
                finder.synchronize_backend(tdetect, "tdetect")
                finder.synchronize_backend(ocr, "ocr")
                matches = finder.find(Text('Nothing'), Image('all_shapes'))

                # verify match accuracy
                self.assertEqual(len(matches), 0)

                # verify dumped files count and names
                dumps = self._verify_and_get_dumps(7, i)
                self._verify_dumped_images('Nothing', 'all_shapes', dumps,
                                           "text")
                hotmaps = sorted(self._get_matches_in('.*hotmap.*', dumps))
                self.assertEqual(len(hotmaps), 4)
                for j, hotmap in enumerate(hotmaps):
                    if j == 0:
                        self.assertIn('3hotmap', hotmap)
                    elif j == 1:
                        self.assertIn('3hotmap-1char', hotmap)
                    elif j == 2:
                        self.assertIn('3hotmap-2text', hotmap)
                    else:
                        self.assertIn('3hotmap-3ocr-%stext' % (j - 2), hotmap)
                    if j == 3 or j == 4:
                        # report achieved similarity in the end of the filename
                        self.assertRegex(hotmap, ".*-\d\.\d+.*")
                    self.assertTrue(
                        os.path.isfile(os.path.join(self.logpath, hotmap)))

                shutil.rmtree(self.logpath)
                i += 1
示例#5
0
    def test_invalid_backends(self):
        """Test that unsupported backends are detected when loading and saving."""
        # test on load
        stepsfile_contents = [
            "item_for_contour.png	some_contour_matchfile.match",
            "some_text_content	some_unknown_content.match"
        ]
        self.assertRaises(UnsupportedBackendError, self._build_chain,
                          os.linesep.join(stepsfile_contents))

        # test on save
        finder = Finder(False, False)
        finder.params["find"] = {"backend": "unknown"}
        chain = self._build_chain("")
        chain._steps.append(Text("", finder))
        self.assertRaises(UnsupportedBackendError, chain.save, "foobar")
示例#6
0
    def test_text_same(self):
        finder = TextFinder()
        finder.params["find"]["similarity"].value = 1.0
        i = 1

        for tdetect in finder.algorithms["text_detectors"]:
            # TODO: this is still not implemented
            if tdetect == "components":
                continue
            for ocr in finder.algorithms["text_recognizers"]:
                # TODO: this is still not implemented
                if ocr == "beamSearch":
                    continue
                # TODO: handle newer OpenCV bugs with some backends
                import cv2
                # TODO: OpenCV 4.2.0 Tesseract bindings output nothing
                if cv2.__version__ == "4.2.0" and ocr == "tesseract":
                    continue
                # TODO: deprecate OpenCV 3.X versions after time
                elif cv2.__version__.startswith("3.") and tdetect == "east":
                    continue

                # HMM misinterprets one char leading to 3/4 recognized chars
                # Tesseract still has similarity 1.0 though
                if ocr == "hmm":
                    finder.params["find"]["similarity"].value = 0.75
                    if tdetect == "east":
                        finder.params["find"]["similarity"].value = 0.4
                else:
                    finder.params["find"]["similarity"].value = 1.0

                finder.configure_backend(tdetect, "tdetect")
                finder.configure_backend(ocr, "ocr")
                # also with customized synchronization to the configuration
                finder.synchronize_backend(tdetect, "tdetect")
                finder.synchronize_backend(ocr, "ocr")
                matches = finder.find(Text('Text'), Image('all_shapes'))

                # verify match accuracy
                self.assertEqual(len(matches), 1)
                # the EAST network confuses the space among some squares with
                # text and thus still read the output but in a larger rectangle
                if tdetect != "east":
                    self.assertEqual(matches[0].x, 22)
                    self.assertEqual(matches[0].y, 83)
                    self.assertAlmostEqual(matches[0].width, 40, delta=3)
                    self.assertAlmostEqual(matches[0].height, 15, delta=3)

                # verify dumped files count and names
                dumps = self._verify_and_get_dumps(7, i)
                self._verify_dumped_images('Text', 'all_shapes', dumps, "text")
                hotmaps = sorted(self._get_matches_in('.*hotmap.*', dumps))
                self.assertEqual(len(hotmaps), 4)
                for j, hotmap in enumerate(hotmaps):
                    if j == 0:
                        self.assertIn('3hotmap', hotmap)
                    elif j == 1:
                        self.assertIn('3hotmap-1char', hotmap)
                    elif j == 2:
                        self.assertIn('3hotmap-2text', hotmap)
                    else:
                        self.assertIn('3hotmap-3ocr-%stext' % (j - 2), hotmap)
                    if j == 3 or j == 4:
                        # report achieved similarity in the end of the filename
                        self.assertRegex(hotmap, ".*-\d\.\d+.*")
                    self.assertTrue(
                        os.path.isfile(os.path.join(self.logpath, hotmap)))

                shutil.rmtree(self.logpath)
                i += 1