ocr.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from . import preprocess
  2. from . import sheetocr
  3. import time
  4. import os
  5. import cv2
  6. # sheetpath = r'C:\Users\Administrator\Desktop\sheet' # 预处理前的试卷目录
  7. # testpath = r'C:\Users\Administrator\Desktop\test' # 预处理后的试卷目录
  8. # resultpath = r'C:\Users\Administrator\Desktop\result' # 结果生成目录
  9. # Parameter Sets
  10. langs = {'ce': '-l chi_sim+eng', 'ec': '-l eng+chi_sim', 'c': '-l chi_sim', 'e': '-l eng', 'eq': '-l eng+equ'} # 语言选项
  11. psms = {'block': '--psm 6', 'default': '--psm 3'} # , '_line': ' --psm 7'} # Page segmentation modes
  12. # oems = {'legacy': '--oem 0', 'lstm': '--oem 1', 'lstm+legacy': '--oem 2'} # OCR Engine modes
  13. langs_py = {'ce': 'chi_sim+eng', 'ec': 'eng+chi_sim', 'c': 'chi_sim', 'e': 'eng', 'eq': 'eng+equ'} # 语言选项
  14. psms_py = {'block': '--psm 6', 'default': '--psm 3'} # , '_line': ' --psm 7'} # Page segmentation modes
  15. scales = (0, 0.5, 2)
  16. dilates = (0, 1, 3, 5)
  17. blurs = (0, 1, 3, 5, 7)
  18. # 用默认最佳参数处理图片, 返回文本
  19. def ocr_py(picture, lang='ce', psm='block', scale=0, dilate=1, blur=5):
  20. image = preprocess.preprocess(picture, scale=scale, dilate=dilate, blur=blur)
  21. words = sheetocr.sheetocr_py(image, lang=langs_py[lang], psm=psms_py[psm])
  22. return words
  23. # 用默认最佳参数处理图片, 返回文本文件
  24. def ocr(picture, output, lang='ce', psm='block', scale=0, dilate=1, blur=5):
  25. image = preprocess.preprocess(picture, scale=scale, dilate=dilate, blur=blur)
  26. cv2.imwrite('tmp_pic', image)
  27. sheetocr.sheetocr('tmp_pic', output, lang=langs[lang], psm=psms[psm])
  28. os.remove('tmp_pic')
  29. # 测试最佳参数
  30. def test_parameters(picture_path, output=0):
  31. start = time.time()
  32. for root, dirs, files in os.walk(picture_path):
  33. for file in files:
  34. picture = os.path.join(root, file)
  35. if output == 0: # 屏幕显示
  36. for s in scales:
  37. for d in dilates:
  38. for b in blurs:
  39. print('Parameters:' + 's' + str(s) + 'd' + str(d) + 'b' + str(b) + '\n')
  40. words = ocr_py(picture, scale=s, dilate=d, blur=b)
  41. print(words)
  42. else: # 输出到路径为output的文件中
  43. for s in scales:
  44. for d in dilates:
  45. for b in blurs:
  46. save = file + 's' + str(s) + 'd' + str(d) + 'b' + str(b)
  47. save = os.path.join(output, save)
  48. with open(save, 'r', encoding='UTF-8') as f:
  49. words = ocr_py(picture, scale=s, dilate=d, blur=b)
  50. f.write(words)
  51. end = time.time()
  52. print('running time:', end - start, 's')
  53. # test_parameters(sheetpath)
  54. # print('OCR done!\n')