经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
TextIn.com API使用心得
来源:cnblogs  作者:王小美O(∩_∩)O  时间:2024/4/15 9:49:18  对本文有异议

我们参加了本次大学生创新创业服务外包大赛,在项目中大量使用到了合合信息所提供的api进行相关功能实现,所以在这里写一篇博客分享一下我们在项目的实际推进中关于TextIn.com API使用心得

我们的产品是一款面向公司管理的REP微信小程序,由于需要覆盖大部分的企业办公需求,我们使用到了大量的API,进行功能实现,这里列举四个使用的比较多的API功能进行讲解和展示

一、通用文字识别

首先是最常用的通用文字识别功能,即识别图片上的的文字并进行输出,实现代码如下:

  1. import requests
  2. import json
  3. def get_file_content(filePath):
  4. with open(filePath, 'rb') as fp:
  5. return fp.read()
  6. class CommonOcr(object):
  7. def __init__(self, img_path):
  8. self._app_id = '******************************'
  9. self._secret_code = '********************************'
  10. self._img_path = img_path
  11. def recognize(self):
  12. url = 'https://api.textin.com/ai/service/v2/recognize'
  13. head = {}
  14. texts = []
  15. try:
  16. image = get_file_content(self._img_path)
  17. head['x-ti-app-id'] = self._app_id
  18. head['x-ti-secret-code'] = self._secret_code
  19. response = requests.post(url, data=image, headers=head)
  20. results = json.loads(response.text)
  21. for line in results['result']['lines']:
  22. texts.append(line['text'])
  23. return texts
  24. except Exception as e:
  25. return str(e)
  26. if __name__ == "__main__":
  27. response = CommonOcr(r'img.png')
  28. print(response.recognize())

实现效果如下:

 二、车牌号识别

在公司管理中,公司的汽车管理是企业业务的常见组成部分,所以我们在小程序中加入公车管理的功能,其中汽车的登记任务就是通过车牌识别的api进行实现的,实现代码如下:

  1. import requests
  2. import json
  3. def get_file_content(filePath):
  4. with open(filePath, 'rb') as fp:
  5. return fp.read()
  6. class CommonOcr(object):
  7. def __init__(self, img_path):
  8. # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-app-id
  9. # 示例代码中 x-ti-app-id 非真实数据
  10. self._app_id = '****************************'
  11. # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-secret-code
  12. # 示例代码中 x-ti-secret-code 非真实数据
  13. self._secret_code = '*******************************'
  14. self._img_path = img_path
  15. def recognize(self):
  16. # 车牌号识别
  17. url = 'https://api.textin.com/robot/v1.0/api/plate_number'
  18. head = {}
  19. try:
  20. image = get_file_content(self._img_path)
  21. head['x-ti-app-id'] = self._app_id
  22. head['x-ti-secret-code'] = self._secret_code
  23. result = requests.post(url, data=image, headers=head)
  24. return result.text
  25. except Exception as e:
  26. return e
  27. if __name__ == "__main__":
  28. response = CommonOcr(r'img_1.png')
  29. print(response.recognize())

实现效果如下:

 

 三、票据识别

企业业务流程中,票据识别是一个很重要的事务,票据识别的效率很大程度上会影响到整体报销流程的效率,所以一个精确高效的票据识别功能是不可或缺的。我们的实现代码如下:

  1. import requests
  2. import json
  3. def get_file_content(filePath):
  4. with open(filePath, 'rb') as fp:
  5. return fp.read()
  6. class CommonOcr(object):
  7. def __init__(self, img_path):
  8. self._app_id = '**********************************'
  9. self._secret_code = '***********************************'
  10. self._img_path = img_path
  11. def recognize(self):
  12. url = 'https://api.textin.com/robot/v1.0/api/bills_crop'
  13. head = {}
  14. result_formatted = []
  15. try:
  16. image = get_file_content(self._img_path)
  17. head['x-ti-app-id'] = self._app_id
  18. head['x-ti-secret-code'] = self._secret_code
  19. response = requests.post(url, data=image, headers=head)
  20. results = json.loads(response.text)
  21. if "result" in results and "object_list" in results["result"]:
  22. for item in results["result"]["object_list"]:
  23. if "item_list" in item:
  24. for field in item["item_list"]:
  25. result_formatted.append(field["key"] + ": " + field["value"])
  26. result_formatted.append("") # Adds an empty line
  27. return "\n".join(result_formatted)
  28. except Exception as e:
  29. return str(e)
  30. if __name__ == "__main__":
  31. response = CommonOcr(r'img_2.png')
  32. print(response.recognize())

实现效果如下:

 

 四、名片识别

名片是员工管理的重要依据,我们的小程序也通过名片识别实现了公司员工的登记和管理,名片识别代码如下:

  1. import requests
  2. import json
  3. def get_file_content(filePath):
  4. with open(filePath, 'rb') as fp:
  5. return fp.read()
  6. class CommonOcr(object):
  7. def __init__(self, img_path):
  8. # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-app-id
  9. # 示例代码中 x-ti-app-id 非真实数据
  10. self._app_id = '***************************'
  11. # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-secret-code
  12. # 示例代码中 x-ti-secret-code 非真实数据
  13. self._secret_code = '***************************'
  14. self._img_path = img_path
  15. def recognize(self):
  16. # 名片识别
  17. url = 'https://api.textin.com/robot/v1.0/api/business_card'
  18. head = {}
  19. try:
  20. image = get_file_content(self._img_path)
  21. head['x-ti-app-id'] = self._app_id
  22. head['x-ti-secret-code'] = self._secret_code
  23. result = requests.post(url, data=image, headers=head)
  24. return result.text
  25. except Exception as e:
  26. return e
  27. if __name__ == "__main__":
  28. response = CommonOcr(r'img_3.png')
  29. print(response.recognize())

实现效果如下:

 

 除上面的api外,合合信息还有很多丰富的面向办公需求的api端口,并且有免费额度,推荐大家进行使用。

 

原文链接:https://www.cnblogs.com/wangxiaomeidegou/p/18133992

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号