detect_er_chars.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/python
  2. import sys
  3. import os
  4. import cv2 as cv
  5. import numpy as np
  6. print('\ndetect_er_chars.py')
  7. print(' A simple demo script using the Extremal Region Filter algorithm described in:')
  8. print(' Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012\n')
  9. if (len(sys.argv) < 2):
  10. print(' (ERROR) You must call this script with an argument (path_to_image_to_be_processed)\n')
  11. quit()
  12. pathname = os.path.dirname(sys.argv[0])
  13. img = cv.imread(str(sys.argv[1]))
  14. gray = cv.imread(str(sys.argv[1]),0)
  15. erc1 = cv.text.loadClassifierNM1(pathname+'/trained_classifierNM1.xml')
  16. er1 = cv.text.createERFilterNM1(erc1)
  17. erc2 = cv.text.loadClassifierNM2(pathname+'/trained_classifierNM2.xml')
  18. er2 = cv.text.createERFilterNM2(erc2)
  19. regions = cv.text.detectRegions(gray,er1,er2)
  20. #Visualization
  21. rects = [cv.boundingRect(p.reshape(-1, 1, 2)) for p in regions]
  22. for rect in rects:
  23. cv.rectangle(img, rect[0:2], (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 0), 2)
  24. for rect in rects:
  25. cv.rectangle(img, rect[0:2], (rect[0]+rect[2],rect[1]+rect[3]), (255, 255, 255), 1)
  26. cv.imshow("Text detection result", img)
  27. cv.waitKey(0)