zbparse/flask_app/general/table_ocr.py

35 lines
1.1 KiB
Python
Raw Normal View History

2024-12-05 16:53:11 +08:00
import os
import requests
import json
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
class CommonOcr(object):
def __init__(self, img_path=None):
self._app_id = os.getenv("TEXTIN_APP_ID")
self._secret_code = os.getenv("TEXTIN_APP_KEY")
self.img_path = img_path # 将 img_path 存为实例变量
def recognize(self):
if not self.img_path:
return "No image path provided"
url = 'https://api.textin.com/ai/service/v2/recognize/table/multipage'
head = {}
try:
image = get_file_content(self.img_path)
head['x-ti-app-id'] = self._app_id
head['x-ti-secret-code'] = self._secret_code
result = requests.post(url, data=image, headers=head)
return result.text
except Exception as e:
return str(e)
if __name__ == "__main__":
png_path = "test.png"
ocr = CommonOcr(img_path=png_path) # 将路径传入构造函数
response = ocr.recognize() # 调用 recognize 方法
print(response)