main_line_e2cc.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. -------------------------------------------------
  3. # @File :line_composition
  4. # @Date :2022/7/15
  5. # @Author :inshine
  6. -------------------------------------------------
  7. """
  8. import cv2
  9. import numpy as np
  10. import base64
  11. import requests
  12. import re
  13. import my_config
  14. from uuid import uuid1
  15. from shapely.geometry import Polygon
  16. URL_API_PREFIX = "ai_cv_hwr"
  17. # PyTorch_REST_API_URL = "http://192.168.1.209:7015/{0}/{1}"
  18. # PyTorch_REST_API_URL = "http://10.19.1.43:7015/{0}/{1}"
  19. # PyTorch_REST_API_URL = "http://api.cv.zxhx.com/{0}/{1}"
  20. def get_image_base64(image):
  21. """
  22. 将各来源的图片转为base64编码,先默认为本地图片路径
  23. :param image:
  24. :return:
  25. """
  26. # print("image:", str(image))
  27. if re.search("^https?:", str(image)): # 远程图片
  28. filebyte = requests.get(image).content
  29. elif re.search("^[A-H]:", str(image)): # 本地图片
  30. local_img = image
  31. filebyte = open(local_img, 'rb').read()
  32. else: # 二进制文件流
  33. filebyte = image.read()
  34. # encoded = base64.b64encode(open(local_img, 'rb').read())
  35. base64_data = base64.b64encode(filebyte).rstrip().decode('utf-8')
  36. return base64_data
  37. def generate_uuid():
  38. return str(uuid1()).replace('-', '')
  39. def get_image_url_base64(img_url):
  40. response = requests.get(img_url)
  41. base64_byte = base64.encodebytes(response.content)
  42. return base64_byte.decode('utf-8')
  43. def calc_polygon_center(points):
  44. polygon = Polygon(points)
  45. point = polygon.centroid
  46. return point.x, point.y
  47. def recognition_line(img_base64, img_url, img_source, line_coordinate, rec_method=0):
  48. """
  49. 英译汉中文识别接口
  50. :param img_base64: 图片的base64编码
  51. :param img_url: 图片url地址
  52. :param img_source: 产品种类:英译汉词汇、英译汉短语
  53. :param line_coordinate: 是否返回坐标:OPEN/CLOSE
  54. :param rec_method: -1 -- baidu, 0 -- random 50%, 1 -- AI-CV, default is 0
  55. :return:
  56. """
  57. img_matrix = cv2.imdecode(np.frombuffer(base64.b64decode(img_base64), np.uint8), cv2.IMREAD_COLOR)
  58. img_shape = img_matrix.shape
  59. img_width, img_height = img_shape[1], img_shape[0]
  60. poly = np.array([[0, 0], [img_width, 0], [img_width, img_height], [0, img_height]])
  61. cx, cy = calc_polygon_center(poly)
  62. det_index_list = [[f"{cy}_{cx}"]]
  63. det_polygon_dict = {f"{cy}_{cx}": {"Polygon": poly.tolist(), "Confidence": 1.0}}
  64. det_matrix_dict = {f"{cy}_{cx}": img_base64}
  65. request_data = {
  66. "RequestID": generate_uuid(),
  67. "DetectionResult": {'ImageWidth': img_width, "ImageHeight": img_height,
  68. "IndexList": det_index_list, "PolygonDict": det_polygon_dict,
  69. "DetMatrixDict": det_matrix_dict},
  70. "ImageURL": img_url,
  71. "ImageSource": img_source,
  72. "LineCoordinate": line_coordinate,
  73. "RecMethod": rec_method,
  74. "AccessToken": "75ad010a0c353d1f9173db20077bb715"
  75. }
  76. print(my_config.PyTorch_REST_API_URL.format(URL_API_PREFIX, "recognitionChineseLine"))
  77. result = requests.post(my_config.PyTorch_REST_API_URL.format(URL_API_PREFIX, "recognitionChineseLine"),
  78. headers={'Content-Type': "application/json;charset=utf-8"},
  79. json=request_data)
  80. result = result.json()
  81. print(result)
  82. if result["errCode"] == "00":
  83. return result["data"]["message"]
  84. else:
  85. return None
  86. if __name__ == '__main__':
  87. import time
  88. image_url = "http://zxhx-w-1302712961.cos.ap-chengdu.myqcloud.com/PP/CScut/601/980294985259646976/900195828017442864/36.jpg"
  89. t1 = time.time()
  90. image_base64 = get_image_url_base64(image_url)
  91. print(time.time() - t1)
  92. # data = recognition_line(image_base64, image_url, "英译汉词汇", "CLOSE")