我们参加了本次大学生创新创业服务外包大赛,在项目中大量使用到了合合信息所提供的api进行相关功能实现,所以在这里写一篇博客分享一下我们在项目的实际推进中关于TextIn.com API使用心得
我们的产品是一款面向公司管理的REP微信小程序,由于需要覆盖大部分的企业办公需求,我们使用到了大量的API,进行功能实现,这里列举四个使用的比较多的API功能进行讲解和展示
一、通用文字识别
首先是最常用的通用文字识别功能,即识别图片上的的文字并进行输出,实现代码如下:
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):
self._app_id = '******************************'
self._secret_code = '********************************'
self._img_path = img_path
def recognize(self):
url = 'https://api.textin.com/ai/service/v2/recognize'
head = {}
texts = []
try:
image = get_file_content(self._img_path)
head['x-ti-app-id'] = self._app_id
head['x-ti-secret-code'] = self._secret_code
response = requests.post(url, data=image, headers=head)
results = json.loads(response.text)
for line in results['result']['lines']:
texts.append(line['text'])
return texts
except Exception as e:
return str(e)
if __name__ == "__main__":
response = CommonOcr(r'img.png')
print(response.recognize())
实现效果如下:
二、车牌号识别
在公司管理中,公司的汽车管理是企业业务的常见组成部分,所以我们在小程序中加入公车管理的功能,其中汽车的登记任务就是通过车牌识别的api进行实现的,实现代码如下:
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):
# 请登录后前往 "工作台-账号设置-开发者信息" 查看 x-ti-app-id
# 示例代码中 x-ti-app-id 非真实数据
self._app_id = '****************************'
# 请登录后前往 "工作台-账号设置-开发者信息" 查看 x-ti-secret-code
# 示例代码中 x-ti-secret-code 非真实数据
self._secret_code = '*******************************'
self._img_path = img_path
def recognize(self):
# 车牌号识别
url = 'https://api.textin.com/robot/v1.0/api/plate_number'
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 e
if __name__ == "__main__":
response = CommonOcr(r'img_1.png')
print(response.recognize())
实现效果如下:
三、票据识别
企业业务流程中,票据识别是一个很重要的事务,票据识别的效率很大程度上会影响到整体报销流程的效率,所以一个精确高效的票据识别功能是不可或缺的。我们的实现代码如下:
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):
self._app_id = '**********************************'
self._secret_code = '***********************************'
self._img_path = img_path
def recognize(self):
url = 'https://api.textin.com/robot/v1.0/api/bills_crop'
head = {}
result_formatted = []
try:
image = get_file_content(self._img_path)
head['x-ti-app-id'] = self._app_id
head['x-ti-secret-code'] = self._secret_code
response = requests.post(url, data=image, headers=head)
results = json.loads(response.text)
if "result" in results and "object_list" in results["result"]:
for item in results["result"]["object_list"]:
if "item_list" in item:
for field in item["item_list"]:
result_formatted.append(field["key"] + ": " + field["value"])
result_formatted.append("") # Adds an empty line
return "\n".join(result_formatted)
except Exception as e:
return str(e)
if __name__ == "__main__":
response = CommonOcr(r'img_2.png')
print(response.recognize())
实现效果如下:
四、名片识别
名片是员工管理的重要依据,我们的小程序也通过名片识别实现了公司员工的登记和管理,名片识别代码如下:
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):
# 请登录后前往 "工作台-账号设置-开发者信息" 查看 x-ti-app-id
# 示例代码中 x-ti-app-id 非真实数据
self._app_id = '***************************'
# 请登录后前往 "工作台-账号设置-开发者信息" 查看 x-ti-secret-code
# 示例代码中 x-ti-secret-code 非真实数据
self._secret_code = '***************************'
self._img_path = img_path
def recognize(self):
# 名片识别
url = 'https://api.textin.com/robot/v1.0/api/business_card'
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 e
if __name__ == "__main__":
response = CommonOcr(r'img_3.png')
print(response.recognize())
实现效果如下:
除上面的api外,合合信息还有很多丰富的面向办公需求的api端口,并且有免费额度,推荐大家进行使用。