Repository navigation
Expand file tree
/
Copy pathTextDetection_ocr_Cover_ai.py
More file actions
1546 lines (1243 loc) · 54.6 KB
/
Copy pathTextDetection_ocr_Cover_ai.py
File metadata and controls
1546 lines (1243 loc) · 54.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import keyboard
import pyautogui
from PIL import Image, ImageDraw
import time
import os
import numpy as np
import pyperclip
import base64
import requests
import json
import io
from paddleocr import TextDetection
import google.generativeai as genai
from dotenv import load_dotenv
from concurrent.futures import ThreadPoolExecutor, as_completed
import threading
from openai import OpenAI
load_dotenv()
# 读取 Luna 程序路径
LUNA_PATH = os.getenv('LUNA_PATH', '')
# 读取 OCR Server 路径
OCR_SERVER_PATH = os.getenv('OCR_SERVER_PATH', '')
model = TextDetection(model_name="PP-OCRv5_server_det")
save_debug_images = False
# 状态栏高度,默认71px(请修改为23px,或者你的状态机高度。这里用作位置校准和背景图片裁切)
STATUS_BAR_HEIGHT = 71
# 是否发送裁剪后的截图到网页
SEND_CROPPED_SCREENSHOT = True
# OCR AI修正模式:auto(自动模式)或 manual(手动模式)
# auto: 全图识别和边框识别都自动进行AI修正
# manual: 需要用户手动点击按钮触发AI修正
OCR_CORRECTION_MODE = os.getenv('OCR_CORRECTION_MODE', 'auto').lower()
# 是否开启全局AI修正(已废弃,使用 OCR_CORRECTION_MODE 替代)
# 保留此变量用于向后兼容
ENABLE_AI_CORRECTION = False
# 开启全局AI修正后,激活的最小文本长度
AI_MIN_TEXT_LENGTH = 10
# 是否开启智能AI修正,修正距离鼠标最新的SMART_AI_SELECTION_COUNT个文本框
# 在 auto 模式下生效
SMART_AI_SELECTION_MODE = True
# 智能AI修正模式下,选择的最近的文本框数量
SMART_AI_SELECTION_COUNT = 5
# AI相关配置
# 配置Gemini API密钥
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
# OCR超时时间
OCR_TIMEOUT = int(os.getenv('OCR_TIMEOUT', 10))
# AI超时时间
AI_TIMEOUT = 14
# AI校验prompt
AI_CORRECTION_PROMPT = """下面收到的文本是用户通过OCR获取的德语句子,请按照一下要求,进行验证和清理:
1. ocr可能会丢失öü,或将ß识别成B,请将错误的德语单词纠正;
2. 格式的换行,请保持换行符,比如第一行是用户名,第二行是用户的推文正文
3. 句子的换行,请不要换行,比如第二行是推文正文,虽然ocr视觉上是多行的,但是你识别后就不用换行,
4. 可能含有不属于句子内的干扰单词、符号、网名等不是德语单词的拉丁单词,请你删除之后返回完整的德语句子。
5. 记得只返回清理后的句子,不许说其他废话
原始文本:{text}"""
# AI提供商,可选值:gemini, openai, responses_api
# AI 服务提供商,支持 gemini / openai / responses_api,默认优先使用 gemini
AI_PROVIDER = os.getenv('AI_PROVIDER', 'responses_api')
# OpenAI 接口的基础 URL,默认官方地址,可替换为第三方代理
OPENAI_BASE_URL = os.getenv('OPENAI_BASE_URL', 'https://api.openai.com/v1')
# OpenAI API 密钥,用于调用 GPT 系列模型
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
# 指定使用的 OpenAI 模型,默认 gpt-4o-mini 以平衡速度与质量
OPENAI_MODEL = os.getenv('OPENAI_MODEL', 'gpt-4o-mini')
# 生成文本时的采样温度,值越小结果越确定,默认 0.7 保持适度多样性
OPENAI_TEMPERATURE = float(os.getenv('OPENAI_TEMPERATURE', '0.7'))
# Responses API 配置(OpenAI 新的 API 格式,端点为 /v1/responses)
# Responses API 基础 URL
RESPONSES_API_BASE_URL = os.getenv('RESPONSES_API_BASE_URL', 'https://api.openai.com/v1')
# Responses API 密钥
RESPONSES_API_KEY = os.getenv('RESPONSES_API_KEY')
# Responses API 使用的模型
RESPONSES_API_MODEL = os.getenv('RESPONSES_API_MODEL', 'gpt-4o-mini')
# Responses API 温度
RESPONSES_API_TEMPERATURE = float(os.getenv('RESPONSES_API_TEMPERATURE', '0.7'))
ai_correction_count = 0
ai_correction_completed = 0
ai_correction_lock = threading.Lock()
def image_to_base64(image_path_or_pil):
if isinstance(image_path_or_pil, str):
with open(image_path_or_pil, 'rb') as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
else:
img_byte_arr = io.BytesIO()
image_path_or_pil.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
return base64.b64encode(img_byte_arr).decode('utf-8')
def call_luna_ocr_api(image_path_or_pil):
api_url = 'http://127.0.0.1:2333/api/ocr'
try:
base64_image = image_to_base64(image_path_or_pil)
payload = {
'image': base64_image
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(api_url, data=json.dumps(payload), headers=headers, timeout=OCR_TIMEOUT)
if response.status_code == 200:
return response.json()
else:
print(f'API Error: Status Code {response.status_code}')
return None
except requests.exceptions.Timeout:
print(f'API Timeout: 请求超时({OCR_TIMEOUT}秒)')
return None
except requests.exceptions.RequestException as e:
print(f'Request Error: {e}')
return None
except Exception as e:
print(f'Error: {e}')
return None
def init_gemini():
"""
初始化Gemini AI模型
返回:
Gemini模型实例或None(如果初始化失败)
"""
if not ENABLE_AI_CORRECTION and not SMART_AI_SELECTION_MODE:
return None
try:
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel('gemini-2.5-flash')
print("Gemini AI模型初始化成功")
return model
except Exception as e:
print(f"Gemini AI初始化失败: {e}")
return None
def init_openai():
"""
初始化OpenAI客户端
返回:
OpenAI客户端实例或None(如果初始化失败)
"""
if not ENABLE_AI_CORRECTION and not SMART_AI_SELECTION_MODE:
return None
try:
client = OpenAI(
api_key=OPENAI_API_KEY,
base_url=OPENAI_BASE_URL
)
print(f"OpenAI客户端初始化成功 (base_url: {OPENAI_BASE_URL}, model: {OPENAI_MODEL}, temperature: {OPENAI_TEMPERATURE})")
return client
except Exception as e:
print(f"OpenAI初始化失败: {e}")
return None
def init_responses_api():
"""
初始化 Responses API 客户端配置
Responses API 是 OpenAI 新的 API 格式,端点为 /v1/responses
与传统的 chat.completions 不同,它使用 input 字段直接传递文本
返回:
dict: 包含配置信息的字典,或None(如果初始化失败)
"""
if not ENABLE_AI_CORRECTION and not SMART_AI_SELECTION_MODE:
return None
if not RESPONSES_API_KEY:
print("Responses API 初始化失败: 未设置 RESPONSES_API_KEY")
return None
try:
config = {
'base_url': RESPONSES_API_BASE_URL,
'api_key': RESPONSES_API_KEY,
'model': RESPONSES_API_MODEL,
'temperature': RESPONSES_API_TEMPERATURE
}
print(f"Responses API 初始化成功 (base_url: {RESPONSES_API_BASE_URL}, model: {RESPONSES_API_MODEL}, temperature: {RESPONSES_API_TEMPERATURE})")
return config
except Exception as e:
print(f"Responses API 初始化失败: {e}")
return None
def correct_text_with_responses_api(text, responses_config, prompt=None, force_correction=False):
"""
使用 Responses API 校验和修正OCR识别的文本
Responses API 格式说明:
- 端点: {base_url}/responses
- 请求体: {"model": "...", "input": "直接文本", "temperature": ...}
- 响应体: {"output": [{"type": "message", "content": [{"text": "..."}]}]}
参数:
text: OCR识别的原始文本
responses_config: Responses API 配置字典
prompt: 自定义prompt,如果为None则使用默认的AI_CORRECTION_PROMPT
force_correction: 是否强制进行AI纠正(用于智能AI选择模式)
返回:
校正后的文本,如果校验失败则返回原始文本
"""
if not force_correction and (not ENABLE_AI_CORRECTION or not responses_config):
return text
if len(text.strip()) < AI_MIN_TEXT_LENGTH:
return text
if not text.strip():
return text
try:
if prompt is None:
prompt = AI_CORRECTION_PROMPT.format(text=text)
elif '{text}' in prompt:
prompt = prompt.format(text=text)
# 构建请求 URL
api_url = f"{responses_config['base_url'].rstrip('/')}/responses"
# 构建请求体
payload = {
'model': responses_config['model'],
'input': prompt,
'temperature': responses_config['temperature'],
'stream': False
}
headers = {
'Content-Type': 'application/json',
'Authorization': f"Bearer {responses_config['api_key']}"
}
print(f"发送给 Responses API 的 Prompt: {prompt}")
print(f"请求 URL: {api_url}")
response = requests.post(
api_url,
data=json.dumps(payload),
headers=headers,
timeout=AI_TIMEOUT
)
if response.status_code != 200:
print(f"Responses API 错误: Status Code {response.status_code}")
print(f"响应内容: {response.text}")
return text
result = response.json()
print(f"Responses API 原始响应: {json.dumps(result, indent=2, ensure_ascii=False)}")
# 解析响应: output 数组中找 type="message" 的项,然后取 content[0].text
corrected_text = None
if 'output' in result and isinstance(result['output'], list):
for output_item in result['output']:
if output_item.get('type') == 'message':
content = output_item.get('content', [])
if content and len(content) > 0:
corrected_text = content[0].get('text', '')
break
print(f"Responses API 返回的文本: '{corrected_text}'")
if corrected_text and corrected_text != text:
print(f"Responses API 校验: '{text}' -> '{corrected_text}'")
return corrected_text
else:
print(f"Responses API 校验: 文本无需修正 '{text}'")
return text
except requests.exceptions.Timeout:
print(f"Responses API 超时({AI_TIMEOUT}秒)")
return text
except requests.exceptions.RequestException as e:
print(f"Responses API 请求错误: {e}")
return text
except Exception as e:
print(f"Responses API 校验失败: {e}")
return text
def correct_text_with_openai(text, openai_client, prompt=None, force_correction=False):
"""
使用OpenAI校验和修正OCR识别的文本
参数:
text: OCR识别的原始文本
openai_client: OpenAI客户端实例
prompt: 自定义prompt,如果为None则使用默认的AI_CORRECTION_PROMPT
force_correction: 是否强制进行AI纠正(用于智能AI选择模式)
返回:
校正后的文本,如果校验失败则返回原始文本
"""
if not force_correction and (not ENABLE_AI_CORRECTION or not openai_client):
return text
if len(text.strip()) < AI_MIN_TEXT_LENGTH:
return text
if not text.strip():
return text
try:
if prompt is None:
prompt = AI_CORRECTION_PROMPT.format(text=text)
elif '{text}' in prompt:
prompt = prompt.format(text=text)
print(f"发送给OpenAI的Prompt: {prompt}")
response = openai_client.chat.completions.create(
model=OPENAI_MODEL,
messages=[
{"role": "user", "content": prompt}
],
temperature=OPENAI_TEMPERATURE,
timeout=AI_TIMEOUT
)
print(f"OpenAI原始响应: {response}")
if response and response.choices and len(response.choices) > 0:
corrected_text = response.choices[0].message.content
print(f"OpenAI返回的文本: '{corrected_text}'")
if corrected_text and corrected_text != text:
print(f"OpenAI校验: '{text}' -> '{corrected_text}'")
return corrected_text
else:
print(f"OpenAI校验: 文本无需修正 '{text}'")
return text
else:
print(f"OpenAI校验: 未返回有效结果")
return text
except Exception as e:
print(f"OpenAI校验失败: {e}")
return text
gemini_model = None
openai_client = None
responses_api_config = None
# AI 提供商初始化逻辑
print("\n" + "=" * 80)
print("AI 提供商初始化")
print(f" - AI_PROVIDER 环境变量值: '{AI_PROVIDER}'")
print(f" - RESPONSES_API_KEY 是否设置: {RESPONSES_API_KEY is not None and len(RESPONSES_API_KEY) > 0}")
print(f" - OPENAI_API_KEY 是否设置: {OPENAI_API_KEY is not None and len(OPENAI_API_KEY) > 0}")
print(f" - GEMINI_API_KEY 是否设置: {GEMINI_API_KEY is not None and len(GEMINI_API_KEY) > 0}")
print("=" * 80 + "\n")
if AI_PROVIDER.lower() == 'responses_api':
# 优先使用 Responses API
print(f"[AI初始化] 尝试初始化 Responses API...")
responses_api_config = init_responses_api()
if responses_api_config:
ai_model = responses_api_config
print(f"[AI初始化] ✓ 已选择 Responses API 作为AI提供商")
else:
print("[AI初始化] ✗ Responses API 初始化失败,尝试使用 OpenAI...")
openai_client = init_openai()
if openai_client:
ai_model = openai_client
print(f"[AI初始化] ✓ 已回退到 OpenAI 作为AI提供商")
else:
print("[AI初始化] ✗ OpenAI初始化失败,尝试使用Gemini...")
gemini_model = init_gemini()
ai_model = gemini_model
if gemini_model:
print(f"[AI初始化] ✓ 已回退到 Gemini 作为AI提供商")
elif AI_PROVIDER.lower() == 'openai':
print(f"[AI初始化] 尝试初始化 OpenAI...")
openai_client = init_openai()
if openai_client:
ai_model = openai_client
print(f"[AI初始化] ✓ 已选择OpenAI作为AI提供商")
else:
print("[AI初始化] ✗ OpenAI初始化失败,尝试使用Gemini...")
gemini_model = init_gemini()
ai_model = gemini_model
if gemini_model:
print(f"[AI初始化] ✓ 已回退到 Gemini 作为AI提供商")
else:
print(f"[AI初始化] 尝试初始化 Gemini...")
gemini_model = init_gemini()
if gemini_model:
ai_model = gemini_model
print(f"[AI初始化] ✓ 已选择Gemini作为AI提供商")
else:
print("[AI初始化] ✗ Gemini初始化失败,尝试使用OpenAI...")
openai_client = init_openai()
ai_model = openai_client
if openai_client:
print(f"[AI初始化] ✓ 已回退到 OpenAI 作为AI提供商")
print("\n" + "=" * 80)
print(f"AI 提供商最终选择结果:")
print(f" - AI_PROVIDER: {AI_PROVIDER}")
print(f" - responses_api_config: {responses_api_config is not None}")
print(f" - openai_client: {openai_client is not None}")
print(f" - gemini_model: {gemini_model is not None}")
print("=" * 80 + "\n")
def detect_columns(items, x_thresh=10):
"""
基于 x 坐标聚类检测列
参数:
items: 文本块列表,每个包含 box [x1, y1, x2, y2]
x_thresh: 列聚类阈值,单位像素
返回:
列列表,每列包含该列的所有文本块
"""
items_sorted = sorted(items, key=lambda x: x["box"][0])
columns = []
for it in items_sorted:
placed = False
for col in columns:
if abs(it["box"][0] - col["x1_mean"]) < x_thresh:
col["items"].append(it)
col["x1_mean"] = np.mean([p["box"][0] for p in col["items"]])
placed = True
break
if not placed:
columns.append({"x1_mean": it["box"][0], "items": [it]})
columns.sort(key=lambda x: x["x1_mean"])
for col in columns:
col["items"].sort(key=lambda x: x["box"][1])
print("\n=== 列检测结果 ===")
for i, col in enumerate(columns):
print(f"\n第 {i+1} 列:")
print(f" 平均 x1: {col['x1_mean']:.2f}")
print(f" 文本块数量: {len(col['items'])}")
for item in col["items"]:
print(f" - 坐标: x1={item['box'][0]:.1f}, y1={item['box'][1]:.1f}, x2={item['box'][2]:.1f}, y2={item['box'][3]:.1f}")
return columns
def sort_ocr_results(boxes, y_thresh=15, x_thresh=50):
"""
boxes: np.ndarray shape [N, 4] => [x_min, y_min, x_max, y_max]
y_thresh: 行分组阈值,单位像素,适当调大调小
x_thresh: 列分组阈值,单位像素,用于检测多列布局
"""
items = []
for i in range(len(boxes)):
box = boxes[i]
x1, y1, x2, y2 = box.tolist()
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
items.append({
"box": [x1, y1, x2, y2],
"cx": cx,
"cy": cy,
"y1": y1
})
columns = detect_columns(items, x_thresh=x_thresh)
all_merged_paragraphs = []
for col in columns:
col_items = col["items"]
col_items.sort(key=lambda x: x["y1"])
lines = []
for it in col_items:
placed = False
for line in lines:
if abs(it["cy"] - line["cy_mean"]) < y_thresh:
line["items"].append(it)
line["cy_mean"] = np.mean([p["cy"] for p in line["items"]])
placed = True
break
if not placed:
lines.append({"cy_mean": it["cy"], "items": [it]})
for line in lines:
line["items"].sort(key=lambda x: x["cx"])
lines.sort(key=lambda x: x["cy_mean"])
col_sorted_items = []
for line in lines:
col_sorted_items.extend(line["items"])
col_merged_paragraphs = merge_paragraphs(col_sorted_items, gap_coefficient=1.2)
all_merged_paragraphs.extend(col_merged_paragraphs)
return all_merged_paragraphs
def merge_paragraphs(sorted_items, gap_coefficient=1.2, height_consistency_check=True, horizontal_alignment_check=True):
"""
智能段落合并算法,基于相对几何关系和统计特征
参数:
sorted_items: 已排序的OCR结果列表(按阅读顺序)
gap_coefficient: 间距系数,默认1.2,表示间距小于平均行高的1.2倍时合并
height_consistency_check: 是否启用字体高度一致性检查
horizontal_alignment_check: 是否启用水平对齐检查
返回:
合并后的段落列表,每个段落包含:
- box: 合并后的外接矩形 [x1, y1, x2, y2]
- children: 构成该段落的原始文本块列表
"""
if not sorted_items:
return []
for item in sorted_items:
x1, y1, x2, y2 = item["box"]
item["height"] = y2 - y1
item["width"] = x2 - x1
item["center_x"] = (x1 + x2) / 2
item["center_y"] = (y1 + y2) / 2
results = []
first_item = sorted_items[0]
current_block = {
"box": first_item["box"].copy(),
"children": [first_item],
"height": first_item["height"],
"width": first_item["width"]
}
for i in range(1, len(sorted_items)):
item = sorted_items[i]
current_y2 = current_block["box"][3]
item_y1 = item["box"][1]
gap = item_y1 - current_y2
avg_height = (current_block["height"] + item["height"]) / 2
should_merge = False
if gap < avg_height * gap_coefficient:
horizontal_overlap = True
if horizontal_alignment_check:
current_x1, _, current_x2, _ = current_block["box"]
item_x1, _, item_x2, _ = item["box"]
overlap = max(0, min(current_x2, item_x2) - max(current_x1, item_x1))
min_width = min(current_block["width"], item["width"])
if min_width > 0 and overlap / min_width < 0.2:
horizontal_overlap = False
height_consistent = True
if height_consistency_check:
height_diff = abs(current_block["height"] - item["height"])
max_height = max(current_block["height"], item["height"])
if max_height > 0 and height_diff / max_height > 0.5:
height_consistent = False
if horizontal_overlap and height_consistent:
should_merge = True
if should_merge:
current_box = current_block["box"]
item_box = item["box"]
new_x1 = min(current_box[0], item_box[0])
new_y1 = min(current_box[1], item_box[1])
new_x2 = max(current_box[2], item_box[2])
new_y2 = max(current_box[3], item_box[3])
current_block["box"] = [new_x1, new_y1, new_x2, new_y2]
current_block["children"].append(item)
current_block["height"] = max(current_block["height"], item["height"])
current_block["width"] = new_x2 - new_x1
else:
results.append(current_block)
current_block = {
"box": item["box"].copy(),
"children": [item],
"height": item["height"],
"width": item["width"]
}
results.append(current_block)
return results
def polys_to_boxes(dt_polys):
"""
将多边形坐标转换为矩形框坐标
参数:
dt_polys: 多边形坐标数组,形状为 [N, 4, 2]
返回:
boxes: 矩形框坐标数组,形状为 [N, 4],每个为 [x1, y1, x2, y2]
"""
boxes = []
for poly in dt_polys:
x_coords = poly[:, 0]
y_coords = poly[:, 1]
x1 = int(x_coords.min())
y1 = int(y_coords.min())
x2 = int(x_coords.max())
y2 = int(y_coords.max())
boxes.append([x1, y1, x2, y2])
return np.array(boxes)
def draw_merged_paragraphs(image_path, merged_paragraphs, output_path):
"""
在图片上绘制合并后的段落方框
参数:
image_path: 原始图片路径
merged_paragraphs: 合并后的段落列表
output_path: 输出图片路径
"""
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
colors = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(255, 0, 255), # 洋红色
(0, 255, 255), # 青色
(255, 128, 0), # 橙色
(128, 0, 255), # 紫色
(0, 128, 255), # 天蓝色
(255, 0, 128), # 粉红色
]
for i, para in enumerate(merged_paragraphs):
x1, y1, x2, y2 = para["box"]
color = colors[i % len(colors)]
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
print(f"段落 {i+1}:")
print(f" 坐标: ({x1:.1f}, {y1:.1f}, {x2:.1f}, {y2:.1f})")
print(f" 颜色: {color}")
image.save(output_path)
print(f"\n合并后的段落图片已保存到: {output_path}")
def draw_ocr_text_on_merged_image(image_path, merged_paragraphs, output_path):
"""
在合并后的段落图片上绘制OCR识别的文本(红色字体)
参数:
image_path: 原始图片路径
merged_paragraphs: 合并后的段落列表(包含OCR识别的文本)
output_path: 输出图片路径
"""
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
colors = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(255, 0, 255), # 洋红色
(0, 255, 255), # 青色
(255, 128, 0), # 橙色
(128, 0, 255), # 紫色
(0, 128, 255), # 天蓝色
(255, 0, 128), # 粉红色
]
print("\n" + "=" * 80)
print("在合并后的段落图片上绘制OCR文本...")
print("=" * 80)
for i, para in enumerate(merged_paragraphs):
x1, y1, x2, y2 = para["box"]
color = colors[i % len(colors)]
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
if 'text' in para and para['text']:
text = para['text']
try:
from PIL import ImageFont
font_size = max(12, int((y2 - y1) * 0.8))
try:
font = ImageFont.truetype("arial.ttf", font_size)
except:
font = ImageFont.load_default()
text_bbox = draw.textbbox((x1, y1), text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
box_width = x2 - x1
box_height = y2 - y1
if text_width > box_width:
font_size = int(font_size * (box_width / text_width))
try:
font = ImageFont.truetype("arial.ttf", font_size)
except:
font = ImageFont.load_default()
text_x = x1 + 5
text_y = y1 + (box_height - text_height) // 2
draw.text((text_x, text_y), text, fill=(255, 0, 0), font=font)
print(f"段落 {i+1}: 绘制文本 '{text}'")
except Exception as e:
print(f"段落 {i+1}: 绘制文本失败 - {e}")
draw.text((x1 + 5, y1 + 5), para['text'], fill=(255, 0, 0))
else:
print(f"段落 {i+1}: 无OCR文本")
image.save(output_path)
print(f"\n带OCR文本的合并段落图片已保存到: {output_path}")
def send_ocr_to_server(merged_paragraphs):
"""
将OCR数据发送到Node.js服务器
参数:
merged_paragraphs: 合并后的段落列表(包含OCR识别的文本)
"""
text_data = []
for para in merged_paragraphs:
x1, y1, x2, y2 = para["box"]
adjusted_y = y1 - STATUS_BAR_HEIGHT
text = para.get('text', '')
children = para.get('children', [])
is_merged = len(children) > 1
merged_lines = len(children)
text_data.append({
'x': x1,
'y': adjusted_y,
'width': x2 - x1,
'height': y2 - y1,
'text': text,
'is_merged': is_merged,
'merged_lines': merged_lines
})
api_url = 'http://127.0.0.1:2334/api/update-ocr'
try:
print(f'[DEBUG] 准备发送OCR数据到: {api_url}')
print(f'[DEBUG] 数据条数: {len(text_data)}')
print(f'[DEBUG] 请求方法: POST')
response = requests.post(api_url, json=text_data, headers={'Content-Type': 'application/json'})
print(f'[DEBUG] 响应状态码: {response.status_code}')
print(f'[DEBUG] 响应内容: {response.text[:200] if response.text else "无内容"}')
if response.status_code == 200:
result = response.json()
print(f"OCR数据已成功发送到服务器,共 {len(text_data)} 个文本区域")
return True
else:
print(f"发送OCR数据失败: HTTP {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"发送OCR数据时出错: {e}")
return False
def send_ai_correction_to_server(para):
"""
将AI纠错后的单个段落数据发送到Node.js服务器
参数:
para: 包含AI纠错文本的段落字典
"""
x1, y1, x2, y2 = para["box"]
adjusted_y = y1 - STATUS_BAR_HEIGHT
text = para.get('corrected_text', para.get('text', ''))
original_text = para.get('text', '')
x1, y1, x2, y2 = para["box"]
para_info = f"段落[({int(x1)},{int(y1)})]"
children = para.get('children', [])
is_merged = len(children) > 1
merged_lines = len(children)
text_data = {
'x': x1,
'y': adjusted_y,
'width': x2 - x1,
'height': y2 - y1,
'text': text,
'is_corrected': True,
'is_merged': is_merged,
'merged_lines': merged_lines
}
api_url = 'http://127.0.0.1:2334/api/update-ocr-correction'
try:
response = requests.post(api_url, json=text_data, headers={'Content-Type': 'application/json'})
if response.status_code == 200:
result = response.json()
if text != original_text:
print(f"{para_info} 发送AI纠错数据: '{original_text}' -> '{text}'")
else:
print(f"{para_info} 发送AI纠错数据: '{text}' (无修正)")
return True
else:
print(f"{para_info} 发送AI纠错数据失败: HTTP {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"{para_info} 发送AI纠错数据时出错: {e}")
return False
def send_cropped_screenshot_to_server(screenshot):
"""
将裁剪后的截图发送到Node.js服务器作为背景图片
参数:
screenshot: PIL Image 对象(全屏截图)
"""
if not SEND_CROPPED_SCREENSHOT:
return False
width, height = screenshot.size
cropped_screenshot = screenshot.crop((0, STATUS_BAR_HEIGHT, width, height))
try:
img_byte_arr = io.BytesIO()
cropped_screenshot.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
base64_image = base64.b64encode(img_byte_arr).decode('utf-8')
payload = {
'image': base64_image
}
headers = {
'Content-Type': 'application/json'
}
api_url = 'http://127.0.0.1:2334/api/update-screenshot'
print(f'[DEBUG] 准备发送截图到: {api_url}')
print(f'[DEBUG] 图片数据长度: {len(base64_image)}')
print(f'[DEBUG] 请求方法: POST')
response = requests.post(api_url, data=json.dumps(payload), headers=headers, timeout=10)
print(f'[DEBUG] 响应状态码: {response.status_code}')
print(f'[DEBUG] 响应内容: {response.text[:200] if response.text else "无内容"}')
if response.status_code == 200:
result = response.json()
print(f"裁剪后的截图已成功发送到服务器")
return True
else:
print(f"发送裁剪后的截图失败: HTTP {response.status_code}")
return False
except requests.exceptions.Timeout:
print(f'发送裁剪后的截图超时')
return False
except requests.exceptions.RequestException as e:
print(f'发送裁剪后的截图时出错: {e}')
return False
except Exception as e:
print(f'发送裁剪后的截图时发生错误: {e}')
return False
def recognize_full_image(image_path_or_pil):
"""
对完整图片进行 OCR 识别(不分割,直接识别整张图片)
参数:
image_path_or_pil: 图片路径或 PIL Image 对象
返回:
识别出的完整文本,失败返回空字符串
"""
print("\n" + "=" * 80)
print("开始对完整图片进行 OCR 识别...")
print("=" * 80)
try:
result = call_luna_ocr_api(image_path_or_pil)
if result and 'text' in result:
text = result['text']
print(f"完整图片 OCR 识别成功,文本长度: {len(text)}")
print(f"识别结果预览: {text[:200]}..." if len(text) > 200 else f"识别结果: {text}")
return text
else:
print("完整图片 OCR 识别失败: 未返回有效文本")
return ""
except Exception as e:
print(f"完整图片 OCR 识别异常: {e}")
return ""
def send_full_text_to_server(text, corrected_text=None):
"""
将完整文本发送到 Node.js 服务器
参数:
text: 原始 OCR 识别的完整文本
corrected_text: AI 矫正后的文本(可选)
返回:
是否发送成功
"""
if not text:
print("完整文本为空,跳过发送")
return False
api_url = 'http://127.0.0.1:2334/api/update-full-text'
payload = {
'text': text,
'corrected_text': corrected_text
}
try:
print(f'[DEBUG] 准备发送完整文本到: {api_url}')
print(f'[DEBUG] 文本长度: {len(text)}')
response = requests.post(
api_url,
json=payload,
headers={'Content-Type': 'application/json'},
timeout=10
)
print(f'[DEBUG] 响应状态码: {response.status_code}')
if response.status_code == 200:
result = response.json()
print(f"完整文本已成功发送到服务器")
return True
else:
print(f"发送完整文本失败: HTTP {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"发送完整文本时出错: {e}")