123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- """
- -------------------------------------------------
- # @File :line_composition
- # @Date :2022/7/15
- # @Author :inshine
- -------------------------------------------------
- """
- import cv2
- import numpy as np
- import base64
- import requests
- import re
- import my_config
- from uuid import uuid1
- from shapely.geometry import Polygon
- URL_API_PREFIX = "ai_cv_hwr"
- # PyTorch_REST_API_URL = "http://192.168.1.209:7015/{0}/{1}"
- # PyTorch_REST_API_URL = "http://10.19.1.43:7015/{0}/{1}"
- # PyTorch_REST_API_URL = "http://api.cv.zxhx.com/{0}/{1}"
- def get_image_base64(image):
- """
- 将各来源的图片转为base64编码,先默认为本地图片路径
- :param image:
- :return:
- """
- # print("image:", str(image))
- if re.search("^https?:", str(image)): # 远程图片
- filebyte = requests.get(image).content
- elif re.search("^[A-H]:", str(image)): # 本地图片
- local_img = image
- filebyte = open(local_img, 'rb').read()
- else: # 二进制文件流
- filebyte = image.read()
- # encoded = base64.b64encode(open(local_img, 'rb').read())
- base64_data = base64.b64encode(filebyte).rstrip().decode('utf-8')
- return base64_data
- def generate_uuid():
- return str(uuid1()).replace('-', '')
- def get_image_url_base64(img_url):
- response = requests.get(img_url)
- base64_byte = base64.encodebytes(response.content)
- return base64_byte.decode('utf-8')
- def calc_polygon_center(points):
- polygon = Polygon(points)
- point = polygon.centroid
- return point.x, point.y
- def recognition_line(img_base64, img_url, img_source, line_coordinate, rec_method=0):
- """
- 英译汉中文识别接口
- :param img_base64: 图片的base64编码
- :param img_url: 图片url地址
- :param img_source: 产品种类:英译汉词汇、英译汉短语
- :param line_coordinate: 是否返回坐标:OPEN/CLOSE
- :param rec_method: -1 -- baidu, 0 -- random 50%, 1 -- AI-CV, default is 0
- :return:
- """
- img_matrix = cv2.imdecode(np.frombuffer(base64.b64decode(img_base64), np.uint8), cv2.IMREAD_COLOR)
- img_shape = img_matrix.shape
- img_width, img_height = img_shape[1], img_shape[0]
- poly = np.array([[0, 0], [img_width, 0], [img_width, img_height], [0, img_height]])
- cx, cy = calc_polygon_center(poly)
- det_index_list = [[f"{cy}_{cx}"]]
- det_polygon_dict = {f"{cy}_{cx}": {"Polygon": poly.tolist(), "Confidence": 1.0}}
- det_matrix_dict = {f"{cy}_{cx}": img_base64}
- request_data = {
- "RequestID": generate_uuid(),
- "DetectionResult": {'ImageWidth': img_width, "ImageHeight": img_height,
- "IndexList": det_index_list, "PolygonDict": det_polygon_dict,
- "DetMatrixDict": det_matrix_dict},
- "ImageURL": img_url,
- "ImageSource": img_source,
- "LineCoordinate": line_coordinate,
- "RecMethod": rec_method,
- "AccessToken": "75ad010a0c353d1f9173db20077bb715"
- }
- print(my_config.PyTorch_REST_API_URL.format(URL_API_PREFIX, "recognitionChineseLine"))
- result = requests.post(my_config.PyTorch_REST_API_URL.format(URL_API_PREFIX, "recognitionChineseLine"),
- headers={'Content-Type': "application/json;charset=utf-8"},
- json=request_data)
- result = result.json()
- print(result)
- if result["errCode"] == "00":
- return result["data"]["message"]
- else:
- return None
- if __name__ == '__main__':
- import time
- image_url = "http://zxhx-w-1302712961.cos.ap-chengdu.myqcloud.com/PP/CScut/601/980294985259646976/900195828017442864/36.jpg"
- t1 = time.time()
- image_base64 = get_image_url_base64(image_url)
- print(time.time() - t1)
- # data = recognition_line(image_base64, image_url, "英译汉词汇", "CLOSE")
|