· 7 years ago · Aug 12, 2018, 09:26 AM
1from aip import AipOcr
2from PyTencentAI.TencentAPI import *
3from PyTencentAI.TencentAPIMsg import *
4
5"""
6Autor: Wilson.Zhang
7Date: 2018/08/12
8Usage: 该文件调用了百度以åŠè…¾è®¯çš„OCRæœåŠ¡ï¼Œå¹¶å°†å¾—åˆ°çš„å†…å®¹ä»¥JSONå½¢å¼ï¼ˆæš‚定)返回给开å‘者
9"""
10
11def GetOCRResult(**args):
12 if args.get("APINAME") is "Baidu" :
13 #选择百度API的情况:
14 APP_ID = "11638437"
15 API_KEY = "iNQHH8yb3CVBEw5hSZu8wtBe"
16 SECRET_KEY = "Cb3w4VSjXzwHlGoVIQxbcDusSlqG554F"
17 client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
18 filecontent = args.get("FILECONTENT")
19 #读å–图片
20 if filecontent is None:
21 result = {"result": "failed! No input pic..."}
22 return result
23 else:
24 file = open(filecontent, "rb")
25 if file is None:
26 result = {"result": "failed! File content is wrong, please verify..."}
27 return result
28 bin = file.read()
29 #强制开å¯ä½ç½®æ£€æµ‹ï¼Œå› 为需è¦ä½ç½®ã€‚。。
30 options = {}
31 options["language_type"] = "CHN_ENG"
32 options["detect_direction"] = "true"
33 options["detect_language"] = "true"
34 options["probability"] = "true"
35
36 result = client.basicGeneral(bin, options)
37 result["result"] = "Success!"
38 return result
39 elif args.get("APINAME") is "Tencent":
40 #选择腾讯API的情况:
41 APP_ID = "2107641945"
42 APP_KEY = "ONCdozjSNiDo1sZj"
43 else:
44 result = {"result": "Oops, seems that we don't support this platform(we only support Baidu now....)"}
45 return result