OcrUtils.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Author : liuyuqi
  5. @Contact : liuyuqi.gov@msn.cn
  6. @Time : 2020/02/17 22:13:38
  7. @Version : 1.0
  8. @License : Copyright © 2017-2020 liuyuqi. All Rights Reserved.
  9. @Desc : ocr 文字识别
  10. '''
  11. import base64
  12. import json,os,sys,re,requests
  13. import user_agent
  14. class OcrUtils():
  15. def __init__(self):
  16. pass
  17. def ocrImg(self, imgData, ocrType):
  18. if ocrType == 0:
  19. return self.bdGeneralOcr(imgData)
  20. elif ocrType == 1:
  21. return self.bdAccurateOcr(imgData)
  22. elif ocrType == 1:
  23. return self.sogouMobileOcr(imgData)
  24. else:
  25. return self.sogouWebOcr(imgData)
  26. def bdGeneralOcr(self, imgData):
  27. return self.bdBaseOcr(imgData, "general_location")
  28. def sogouMobileOcr(self, imgData):
  29. pass
  30. def bdAccurateOcr(self, imgData):
  31. self.bdBaseOcr(imgData, "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate")
  32. data={}
  33. res=requests.post(url=url,data=data,headers=user_agent.getheaders()).content.decode("utf8")
  34. def sogouWebOcr(self, imgData):
  35. url = "https://deepi.sogou.com/api/sogouService"
  36. referer = "https://deepi.sogou.com/?from=picsearch&tdsourcetag=s_pctim_aiomsg"
  37. imageData = base64.encode(imgData)
  38. def bdBaseOcr(self, imgData, param):
  39. urlArr = ["http://ai.baidu.com/tech/ocr/general", "http://ai.baidu.com/index/seccode?action=show"]
  40. if __name__ == '__main__':
  41. test=OcrUtils()
  42. print(test.sogouWebOcr())