mathpix_ recognition_V1.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2021/6/30 0030 17:01
  3. # @Author : LF
  4. # @FileName: mathpix_ recognition_V1.py
  5. # @Software: PyCharm
  6. import requests
  7. import base64
  8. import cv2
  9. import time
  10. # url = 'http://10.19.1.11:7080/segment/formula/' # 线上服务器地址
  11. url = 'http://192.168.1.208:8001/segment/formula/' # 本地测试服务器地址
  12. def mathpix_recognition_with_img_path(img_path, img_only_flag, latex_img):
  13. """
  14. :param img_path: 图片路径 服务器静态地址'http://************.png'
  15. :return: result 包括参数:"image_url": img_path,输入图片的名称 ; "texts": 识别结果; "is_success": 是否识别成功 --- 0:识别错误 1:识别正确 99:接口调用错误
  16. """
  17. data = {'img_url': img_path, 'img_only': img_only_flag, 'latex_img': latex_img}
  18. result = requests.post(url=url, data=data).json()
  19. if result['is_success'] == 1:
  20. txt_result = result['texts'] # 识别的最终结果
  21. return txt_result
  22. def get_image_base64(img_path):
  23. with open(img_path, "rb") as f:
  24. base64_byte = base64.b64encode(f.read())
  25. return base64_byte.decode('utf-8')
  26. def mathpix_recognition_with_base64Img(img_path):
  27. """
  28. :param img_path: 图片路径 'img_url':base64img
  29. :return: result 包括参数:"image_url": img_path,输入图片的名称 ; "texts": 识别结果; "is_success": 是否识别成功 --- 0:识别错误 1:识别正确 99:接口调用错误
  30. """
  31. data = {'img_url': get_image_base64(img_path)}
  32. result = requests.post(url=url, data=data).json()
  33. if result['is_success'] == 1:
  34. txt_result = result['texts'] # 识别的最终结果
  35. return txt_result
  36. res = mathpix_recognition_with_base64Img(r'Z:\datas\formula_online\png_test_32/0ad997808e.png')
  37. print(res)
  38. # '''梅阳阳调用方式'''
  39. # import requests
  40. # res = requests.post('http://192.168.1.208:8001/segment/formula/', data={
  41. # 'img_only': 1, 'latex_img': 1
  42. # 'img_url': 'url'}).json()