img2latex.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import base64
  2. import json
  3. import random
  4. import requests
  5. import configs
  6. # import cv2
  7. import numpy as np
  8. def np2base64(img_arr):
  9. """
  10. :param img_arr:
  11. :return:
  12. """
  13. retval, buffer = cv2.imencode('.jpg', img_arr)
  14. base64_str = base64.b64encode(buffer)
  15. base64_str = base64_str.decode()
  16. return base64_str
  17. def microsoft_ocr_one(img_base64):
  18. """
  19. :param img_base64:
  20. :return:
  21. """
  22. data = {"data": img_base64,
  23. "inputForm": "Image",
  24. "clientInfo": {"app": "Math",
  25. "platform": "ios",
  26. "configuration": "Unknown",
  27. "version": "1.8.0",
  28. "mkt": "zh-cn"},
  29. }
  30. url = "https://www.bing.com/cameraexp/api/v1/getlatex"
  31. headers = {"User-Agent": "Math/1 CFNetwork/1121.2.2 Darwin/19.3.0",
  32. # $(CFBundleName)/$(CFBundleVersion) CFNetwork/1121.2.2 Darwin/19.2.0
  33. "Content-Type": "application/json",
  34. "Connection": "keep-alive",
  35. "Accept": "application/json",
  36. "Content-Length": "888888",
  37. "Accept-Language": "zh-cn",
  38. "Accept-Encoding": "gzip, deflate, br"
  39. }
  40. resp = requests.post(url=url, headers=headers, data=json.dumps(data, ensure_ascii=True), timeout=3)
  41. ocrres = ""
  42. if resp.status_code == 200:
  43. ocrres = json.loads(resp.text)["latex"]
  44. print(resp.text)
  45. return ocrres
  46. def formula_reg(img_base64):
  47. """
  48. :param img_base64:
  49. :return: result 包括参数:
  50. "image_url": img_path,输入图片的名称 ;
  51. "texts": 识别结果;
  52. "is_success": 是否识别成功 --- 0:识别错误 1:识别正确 99:接口调用错误
  53. """
  54. flag = random.randint(1, 100)
  55. if flag <= 90:
  56. # mathpix charged api
  57. print("---mathpix---")
  58. txt_res = requests.post(url=configs.mathpix_ip,
  59. data={"img_url": img_base64}, timeout=3).json()["texts"]
  60. txt_res = txt_res.replace("<latex>", "").replace("</latex>", "")
  61. else: # 11-15 p=1/3 free api
  62. txt_res = microsoft_ocr_one(img_base64)
  63. return txt_res
  64. def get_ocr_latex(img_path):
  65. """
  66. 根据公式图片调取接口(mathpix,微软),获取latex
  67. img_path:图片路径
  68. :return:
  69. """
  70. try:
  71. #
  72. img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
  73. rr = cv2.split(img)
  74. # print(img.shape)
  75. # img = img[:, :, :3]
  76. base64_img = np2base64(rr[3])
  77. res_ltx = formula_reg(base64_img)
  78. except:
  79. res_ltx = ""
  80. return res_ltx
  81. def get_ocrlatex_by_url(img_url):
  82. """
  83. 根据公式图片调取接口(mathpix,微软),获取latex
  84. img_path:图片路径
  85. :return:
  86. """
  87. try:
  88. img = requests.get(img_url).content
  89. im = np.frombuffer(img, np.uint8)
  90. img = cv2.imdecode(im, cv2.IMREAD_UNCHANGED)
  91. rr = cv2.split(img)
  92. # print(img.shape)
  93. # img = img[:, :, :3]
  94. base64_img = np2base64(rr[3])
  95. res_ltx = formula_reg(base64_img)
  96. print(res_ltx)
  97. except:
  98. res_ltx = ""
  99. return res_ltx
  100. if __name__ == "__main__":
  101. import time
  102. # t1 = time.time()
  103. # hyy = "http://192.168.1.140:8888/ser_static/1848/files/image337.png"
  104. # res = get_ocrlatex_by_url(hyy)
  105. # print(res)
  106. # print(time.time()-t1)
  107. # F:\zwj\word_folder\6673\word\media\image14.png # wmf生成的4通道
  108. # F:\zwj\word_folder\6210\word\media\image47.png # 原始4通道图片
  109. # im = requests.get(hyy).content
  110. # img1 = np.frombuffer(im, np.uint8)
  111. # img = cv2.imread(r"F:\zwj\word_folder\6673\word\media\image14.png", cv2.IMREAD_UNCHANGED)
  112. # img = cv2.imdecode(img1, cv2.IMREAD_UNCHANGED)
  113. # rr = cv2.split(img)
  114. # print(img.shape)
  115. # img = img[:, :, :3]
  116. # import numpy as np
  117. # # print(np.argwhere((0 < img) & (img <255)))
  118. # cv2.imshow("hh", img)
  119. # cv2.waitKey(0)
  120. # self.raw = cv2.imdecode(data, cv2.IMREAD_COLOR)
  121. # img = cv2.resize(rr[3], (0, 0), fx=0.5, fy=0.3)
  122. # cv2.imshow("hh", img)
  123. # cv2.waitKey(0)
  124. # base64_img = np2base64(rr[3])
  125. # # res = microsoft_ocr_one(base64_img)
  126. # res = formula_reg(base64_img)
  127. # print(res)