test_tesseract_ocr.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. import infra.basetest
  3. class TestTesseractOcr(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_FREETYPE=y
  7. BR2_PACKAGE_GHOSTSCRIPT_FONTS=y
  8. BR2_PACKAGE_GRAPHICSMAGICK=y
  9. BR2_PACKAGE_TESSERACT_OCR=y
  10. BR2_PACKAGE_TESSERACT_OCR_LANG_ENG=y
  11. BR2_TARGET_ROOTFS_CPIO=y
  12. # BR2_TARGET_ROOTFS_TAR is not set
  13. """
  14. def test_run(self):
  15. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  16. self.emulator.boot(arch="armv5",
  17. kernel="builtin",
  18. options=["-initrd", cpio_file])
  19. self.emulator.login()
  20. msg = "Hello from Buildroot runtime test."
  21. img_file = "image.pgm"
  22. txt_basename = "text"
  23. txt_file = f"{txt_basename}.txt"
  24. # Check the program execute.
  25. self.assertRunOk("tesseract --version")
  26. # Generate an image file including a text message.
  27. cmd = f"gm convert -pointsize 16 label:'{msg}' {img_file}"
  28. self.assertRunOk(cmd)
  29. # Perform the character recognition.
  30. cmd = f"tesseract {img_file} {txt_basename}"
  31. self.assertRunOk(cmd, timeout=30)
  32. # Check the decoded text matches the original message.
  33. cmd = f"grep -F '{msg}' {txt_file}"
  34. self.assertRunOk(cmd)