ocr_baidu.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding:utf-8 -*-
  2. import os, sys, time
  3. from aip import AipOcr
  4. import cchardet as chardet
  5. from ssat_sdk.utils import LoggingUtil
  6. from ssat_sdk.config.baidu_config import BaiduConfig
  7. class OCRBaidu():
  8. '''
  9. 识别语言类型,默认为CHN_ENG。可选值包括:
  10. - CHN_ENG#中英文混合;
  11. - ENG#英文;
  12. - POR#葡萄牙语;
  13. - FRE#法语;
  14. - GER#德语;
  15. - ITA#意大利语;
  16. - SPA#西班牙语;
  17. - RUS#俄语;
  18. - JAP#日语;
  19. - KOR#韩语
  20. '''
  21. def __init__(self):
  22. baiduCFG = BaiduConfig()
  23. APP_ID = baiduCFG.getAppID()
  24. API_KEY = baiduCFG.getAPIKey()
  25. SECRET_KEY = baiduCFG.getSecretKey()
  26. print "APP_ID,API_KEY,SECRET_KEY:",APP_ID,API_KEY,SECRET_KEY
  27. self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
  28. """ 读取图片 """
  29. def get_file_content(self, filePath):
  30. try:
  31. with open(filePath, 'rb') as fp:
  32. img = fp.read()
  33. fp.close()
  34. return img
  35. except Exception,e:
  36. LoggingUtil.printLog("OCR", u"百度OCR读取图片失败,Err:" + unicode(e))
  37. return None
  38. '''
  39. :param language:
  40. - CHN_ENG#中英文混合;
  41. - ENG#英文;
  42. - POR#葡萄牙语;
  43. - FRE#法语;
  44. - GER#德语;
  45. - ITA#意大利语;
  46. - SPA#西班牙语;
  47. - RUS#俄语;
  48. - JAP#日语;
  49. - KOR#韩语
  50. '''
  51. def basicGeneral(self, pic_path, language):
  52. """ 读取图片 """
  53. image = self.get_file_content(pic_path)
  54. if image is None:
  55. return None
  56. """ 如果有可选参数 """
  57. options = {}
  58. options["language_type"] = language
  59. """ 带参数调用通用文字识别, 图片参数为本地图片 """
  60. """
  61. {u'log_id': 3857306686703806895L,
  62. u'direction': 0,
  63. u'words_result_num': 1,
  64. u'words_result':
  65. [
  66. {
  67. u'words': u'\u58f0\u97f3',
  68. u'probability': {u'variance': 0.0,
  69. u'average': 0.99964, u'min': 0.99952}
  70. }
  71. ],
  72. u'language': -1}
  73. """
  74. try:
  75. print "普通精度"
  76. result = self.client.basicGeneral(image, options)
  77. except Exception,e:
  78. LoggingUtil.printLog("OCR",u"百度普通OCR连接失败,Err:"+ unicode(e))
  79. return None
  80. try:
  81. str_words = self.genOCRStrList(result["words_result"])
  82. except Exception,e:
  83. LoggingUtil.printLog("OCR",u"百度普通OCR识别失败,Err:"+ unicode(e))
  84. return []
  85. return str_words
  86. def basicAccurate(self, pic_path, language="CHN_ENG"):
  87. baiduCFG = BaiduConfig()
  88. retCount = baiduCFG.getRetCount()
  89. # if retCount <= 0:
  90. # return self.basicGeneral(pic_path, language)
  91. """ 读取图片 """
  92. image = self.get_file_content(pic_path)
  93. if image is None:
  94. return None
  95. """ 如果有可选参数 """
  96. options = {}
  97. # options["detect_direction"] = "true"
  98. # options["probability"] = "true"
  99. """ 带参数调用通用文字识别, 图片参数为本地图片 """
  100. """
  101. {u'log_id': 3857306686703806895L,
  102. u'direction': 0,
  103. u'words_result_num': 1,
  104. u'words_result':
  105. [
  106. {
  107. u'words': u'\u58f0\u97f3',
  108. u'probability': {u'variance': 0.0,
  109. u'average': 0.99964, u'min': 0.99952}
  110. }
  111. ],
  112. u'language': -1}
  113. """
  114. try:
  115. print "高精度"
  116. result = self.client.basicAccurate(image, options)
  117. except Exception,e:
  118. LoggingUtil.printLog("OCR",u"百度高精度OCR连接失败,Err:" + unicode(e))
  119. return None
  120. try:
  121. str_words = self.genOCRStrList(result["words_result"])
  122. except Exception,e:
  123. LoggingUtil.printLog("OCR",u"百度高精度OCR识别失败,Err:" + unicode(e) + unicode(result))
  124. return []
  125. finally:
  126. baiduCFG = BaiduConfig()
  127. baiduCFG.subRetCount(1)
  128. return str_words
  129. def genOCRStrList(self, words_result):
  130. strList = []
  131. for word in words_result:
  132. strList.append(word["words"].encode("utf-8"))
  133. return strList
  134. if __name__ == "__main__":
  135. pic_path = r"D:\ocr_err\mi_1.png"
  136. # pic_path = r"D:\ocr_err\mi_3.png"
  137. # pic_path = r"D:\ocr_err\mi_5.png"
  138. ocr = OCRBaidu()
  139. # for word in ocr.basicGeneral(pic_path,"CHN_ENG"):
  140. # print "word:", word, chardet.detect(word)
  141. for word in ocr.basicAccurate(pic_path):
  142. print "word:", word, chardet.detect(word)