fasdfsa commited on
Commit
9b05458
·
1 Parent(s): a2398dc

add 古籍 ocr

Browse files
ocr/.vscode/launch.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+ {
5
+ "name": "Python Debugger: Current File",
6
+ "type": "debugpy",
7
+ "request": "launch",
8
+ "program": "${file}",
9
+ "console": "integratedTerminal"
10
+ }
11
+ ]
12
+ }
ocr/0375.jpg ADDED

Git LFS Details

  • SHA256: e7c49eb4cf1066647067ac255c38c4799f0157147d5bb9a221569772620e7225
  • Pointer size: 131 Bytes
  • Size of remote file: 901 kB
ocr/0375.json ADDED
The diff for this file is too large to render. See raw diff
 
ocr/kandianguji_ocr.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import base64
3
+ import json
4
+ import requests
5
+
6
+ API_URL = "https://ocr.kandianguji.com/ocr_api"
7
+ TOKEN = "8be3d75a-8a66-4b5d-9047-d4895e4c8000"
8
+ EMAIL = "13788325535"
9
+
10
+ def get_kandianguji_ocr_result(image_path: str, **kwargs):
11
+ """
12
+ 调用看典古籍OCR API识别图像。
13
+
14
+ 参数:
15
+ image_path (str): 需要识别的图像文件路径。
16
+ **kwargs: 其他可选API参数 (如 version='v2', det_layout=True 等)。
17
+ 详细参数请参考 readme.txt 文档。
18
+
19
+ 返回:
20
+ dict: API响应的JSON数据。
21
+ """
22
+ if not os.path.exists(image_path):
23
+ raise FileNotFoundError(f"指定的图像文件不存在: {image_path}")
24
+
25
+ # 1. 将图像转换为base64编码
26
+ with open(image_path, "rb") as img_file:
27
+ image_base64 = base64.b64encode(img_file.read()).decode('utf-8')
28
+
29
+ # 2. 构建基础请求数据
30
+ payload = {
31
+ "token": TOKEN,
32
+ "email": EMAIL,
33
+ "image": image_base64
34
+ }
35
+
36
+ # 3. 合并可选参数
37
+ # 根据 readme.txt 提取的默认可选参数
38
+ default_options = {
39
+ "char_ocr": True,
40
+ "det_mode": "sp",
41
+ "det_layout": True,
42
+ "image_size": 0,
43
+ "return_position": True,
44
+ "return_choices": True,
45
+ "version": "v2",
46
+ "only_plain_text": False,
47
+ "return_layout": True,
48
+ "auto_insert_space": False,
49
+ "hp_line_words_angel": "left2right",
50
+ "sp_line_words_angel": "top2bottom"
51
+ }
52
+
53
+ # 更新默认参数(如果 kwargs 中提供了新的值)
54
+ for key, value in default_options.items():
55
+ payload[key] = kwargs.get(key, value)
56
+
57
+ # 4. 发送POST请求
58
+ headers = {
59
+ "Content-Type": "application/json"
60
+ }
61
+
62
+ try:
63
+ response = requests.post(API_URL, json=payload, headers=headers)
64
+ # 检查响应状态码
65
+ response.raise_for_status()
66
+ return response.json()
67
+ except requests.exceptions.RequestException as e:
68
+ print(f"API请求失败: {e}")
69
+ # 如果有响应体,打印出来以便调试
70
+ if 'response' in locals() and response is not None:
71
+ print(f"响应内容: {response.text}")
72
+ return None
73
+
74
+ if __name__ == "__main__":
75
+ # 测试用例 (请替换为真实的古籍图片路径)
76
+ sample_image = "0375.jpg"
77
+
78
+ print("看典古籍OCR API 接口测试")
79
+ print("-" * 30)
80
+
81
+ if os.path.exists(sample_image):
82
+ print(f"正在识别图片: {sample_image}")
83
+ # 调用示例,可按需传入 v2 版本的特有参数
84
+ result = get_kandianguji_ocr_result(
85
+ sample_image,
86
+ version="v2",
87
+ det_layout=True
88
+ )
89
+
90
+ if result:
91
+ print("\n识别结果:")
92
+ s = json.dumps(result, indent=2, ensure_ascii=False)
93
+ with open("out.json", 'w', encoding='utf-8') as f:
94
+ f.write(s)
95
+ print(s)
96
+ else:
97
+ print(f"提示: 请在当前目录下放置一张名为 '{sample_image}' 的图片用于测试。")
98
+ print("或者修改 sample_image 变量的值指向有效的图片路径。")
ocr/readme.txt ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ https://ocr.kandianguji.com/ocr_api
2
+
3
+ token:8be3d75a-8a66-4b5d-9047-d4895e4c8000
4
+
5
+ email:13788325535
6
+
7
+ 请求参数:
8
+
9
+ token:您所申请的API Token,点此申请 必传
10
+
11
+ email:申请API Token的账号,看典古籍网站的注册账号(参数名为email,手机号注册的传手机号) 必传
12
+
13
+ image:需要识别的古籍图像,base64编码后的字符串类型,您可以在此处转换您的图像为base64编码 必传
14
+
15
+ char_ocr:是否进行单字符检测识别,不检测文本行,只检测图像上的字符,文本行顺序可能会出错;布尔类型;默认值:False
16
+
17
+ det_mode:文字内容排版样式,目前有三种可选:auto(自动识别)、sp(竖向排版)、hp(横向排版);字符串类型,默认值:auto
18
+
19
+ image_size:识别前图像尺寸调整,图像越小识别速度越快,0为不调整,设置指定值将按照设置对图像最长边进行等比例调整;整数类型,默认值:0
20
+
21
+ return_position:是否返回文本行坐标信息和字符坐标信息;布尔类型,默认值:False
22
+
23
+ return_choices:是否返回每个字符的其它候选字;布尔类型,默认值:False
24
+
25
+ version:指定识别算法版本;字符串类型,可选:default(v1标准版本)、beta(古籍语序优化版本)、v2(最新版本),默认值:default
26
+
27
+ det_layout(v2):是否开启版面识别(对图像上的内容进行判断是否是正文、页眉页脚/版心等),对分栏式、多栏式文档识别效果较好;布尔类型(开启True、关闭False),默认值:False
28
+
29
+ only_plain_text(v2):是否只识别返回正文内容,仅当版面识别开启时生效,不识别页眉/页脚/版心等;布尔类型(开启True、关闭False),默认值:False
30
+
31
+ return_layout(v2):是否返回版面信息,仅当版面识别开启时生效;布尔类型(开启True、关闭False),默认值:False
32
+
33
+ auto_insert_space(v2):按照字符间距自动插入空格;布尔类型(开启True、关闭False),默认值:False
34
+
35
+ hp_line_words_angel(v2):指定横排句子文字排序方向;字符串类型(从左到右left2right、从右到左right2left),默认值:left2right
36
+
37
+ sp_line_words_angel(v2):指定竖排句子文字排序方向;字符串类型(从上到下top2bottom、从下到上bottom2top),默认值:top2bottom
38
+
39
+ 请求方式:
40
+
41
+ POST请求,请求体可以为Form Data或JSON两种方式均可接受
ocr/requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ requests