import base64 import json import random import requests import configs # import cv2 import numpy as np def np2base64(img_arr): """ :param img_arr: :return: """ retval, buffer = cv2.imencode('.jpg', img_arr) base64_str = base64.b64encode(buffer) base64_str = base64_str.decode() return base64_str def microsoft_ocr_one(img_base64): """ :param img_base64: :return: """ data = {"data": img_base64, "inputForm": "Image", "clientInfo": {"app": "Math", "platform": "ios", "configuration": "Unknown", "version": "1.8.0", "mkt": "zh-cn"}, } url = "https://www.bing.com/cameraexp/api/v1/getlatex" headers = {"User-Agent": "Math/1 CFNetwork/1121.2.2 Darwin/19.3.0", # $(CFBundleName)/$(CFBundleVersion) CFNetwork/1121.2.2 Darwin/19.2.0 "Content-Type": "application/json", "Connection": "keep-alive", "Accept": "application/json", "Content-Length": "888888", "Accept-Language": "zh-cn", "Accept-Encoding": "gzip, deflate, br" } resp = requests.post(url=url, headers=headers, data=json.dumps(data, ensure_ascii=True), timeout=3) ocrres = "" if resp.status_code == 200: ocrres = json.loads(resp.text)["latex"] print(resp.text) return ocrres def formula_reg(img_base64): """ :param img_base64: :return: result 包括参数: "image_url": img_path,输入图片的名称 ; "texts": 识别结果; "is_success": 是否识别成功 --- 0:识别错误 1:识别正确 99:接口调用错误 """ flag = random.randint(1, 100) if flag <= 90: # mathpix charged api print("---mathpix---") txt_res = requests.post(url=configs.mathpix_ip, data={"img_url": img_base64}, timeout=3).json()["texts"] txt_res = txt_res.replace("", "").replace("", "") else: # 11-15 p=1/3 free api txt_res = microsoft_ocr_one(img_base64) return txt_res def get_ocr_latex(img_path): """ 根据公式图片调取接口(mathpix,微软),获取latex img_path:图片路径 :return: """ try: # img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED) rr = cv2.split(img) # print(img.shape) # img = img[:, :, :3] base64_img = np2base64(rr[3]) res_ltx = formula_reg(base64_img) except: res_ltx = "" return res_ltx def get_ocrlatex_by_url(img_url): """ 根据公式图片调取接口(mathpix,微软),获取latex img_path:图片路径 :return: """ try: img = requests.get(img_url).content im = np.frombuffer(img, np.uint8) img = cv2.imdecode(im, cv2.IMREAD_UNCHANGED) rr = cv2.split(img) # print(img.shape) # img = img[:, :, :3] base64_img = np2base64(rr[3]) res_ltx = formula_reg(base64_img) print(res_ltx) except: res_ltx = "" return res_ltx if __name__ == "__main__": import time # t1 = time.time() # hyy = "http://192.168.1.140:8888/ser_static/1848/files/image337.png" # res = get_ocrlatex_by_url(hyy) # print(res) # print(time.time()-t1) # F:\zwj\word_folder\6673\word\media\image14.png # wmf生成的4通道 # F:\zwj\word_folder\6210\word\media\image47.png # 原始4通道图片 # im = requests.get(hyy).content # img1 = np.frombuffer(im, np.uint8) # img = cv2.imread(r"F:\zwj\word_folder\6673\word\media\image14.png", cv2.IMREAD_UNCHANGED) # img = cv2.imdecode(img1, cv2.IMREAD_UNCHANGED) # rr = cv2.split(img) # print(img.shape) # img = img[:, :, :3] # import numpy as np # # print(np.argwhere((0 < img) & (img <255))) # cv2.imshow("hh", img) # cv2.waitKey(0) # self.raw = cv2.imdecode(data, cv2.IMREAD_COLOR) # img = cv2.resize(rr[3], (0, 0), fx=0.5, fy=0.3) # cv2.imshow("hh", img) # cv2.waitKey(0) # base64_img = np2base64(rr[3]) # # res = microsoft_ocr_one(base64_img) # res = formula_reg(base64_img) # print(res)