import base64 import requests import json import cv2 import numpy as np def opecv2base64(img): image = cv2.imencode('.jpg', img)[1] base64_data = str(base64.b64encode(image))[2:-1] return base64_data def mathpix_api(img): image = opecv2base64(img) image_uri = "data:image/jpg;base64," + image r = requests.post("https://api.mathpix.com/v3/latex", data=json.dumps({'src': image_uri, 'formats': ['latex_normal', 'latex_styled']}), headers={"app_id": "1092963746_qq_com", "app_key": "0c3b77b0c3720175e0ba", "Content-type": "application/json"}, timeout=7).json() res = r['latex_styled'] latex_confidence = r['latex_confidence'] # print(res) return res, latex_confidence if __name__ == '__main__': img_path0 = r'F:\save\img_withbgm\0003.png' img0 = np.asarray(cv2.imread(img_path0)) mathpix_api(img0)