-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr.py
More file actions
982 lines (839 loc) · 29 KB
/
ocr.py
File metadata and controls
982 lines (839 loc) · 29 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
#!/usr/bin/env python3
"""
Live OCR → clean → ncurses → code-detect + autoformat → final cleanup → pyperclip.copy(...) → HARD EXIT
Notes:
- uses ONLY pyperclip for clipboard (no xclip/xsel/wl-copy/pbcopy subprocess junk)
- if pyperclip is missing → exit(2) with install hint
- stops on "===============save results:==============="
- drops torch.Size(...), image:/other: progress lines, "BASE: ..." etc.
- if curses is available and stdout is a TTY → shows stream in ncurses
- after finished → copies CLEANED text to clipboard → os._exit(0)
"""
from __future__ import annotations
import argparse
import io
import os
import sys
import re
import json
import shutil
import subprocess # still used for nothing heavy now, but keep
import platform
from typing import Iterable, Optional, Tuple, List
# ---------------------------------------------------------------------
# required deps
# ---------------------------------------------------------------------
missing = []
try:
import requests
except Exception:
missing.append("requests")
try:
from PIL import Image, ImageTk
except Exception:
missing.append("Pillow")
try:
import mss
except Exception:
missing.append("mss")
try:
import tkinter as tk
except Exception:
missing.append("tkinter (usually preinstalled with Python)")
try:
import curses
except Exception:
curses = None # fallback
# MUST HAVE: pyperclip
try:
import pyperclip
HAS_PYPERCLIP = True
except Exception:
HAS_PYPERCLIP = False
# optional
try:
from langdetect import detect as ld_detect
HAS_LANGDETECT = True
except Exception:
HAS_LANGDETECT = False
try:
from pygments import lex
from pygments.lexers import guess_lexer
from pygments.token import Token
from pygments.util import ClassNotFound
HAS_PYGMENTS = True
except Exception:
HAS_PYGMENTS = False
try:
import black
HAS_BLACK = True
except Exception:
HAS_BLACK = False
try:
import autopep8
HAS_AUTOPEP8 = True
except Exception:
HAS_AUTOPEP8 = False
if missing:
print(
"Missing deps:\n - " + "\n - ".join(missing)
+ "\nInstall with:\n python3 -m pip install "
+ " ".join([m for m in missing if not m.startswith("tkinter")]),
file=sys.stderr,
)
sys.exit(2)
if not HAS_PYPERCLIP:
print("This script now uses pyperclip for clipboard. Install it with:", file=sys.stderr)
print(" python3 -m pip install pyperclip", file=sys.stderr)
sys.exit(2)
# ---------------------------------------------------------------------
# consts
# ---------------------------------------------------------------------
DEFAULT_URL = os.environ.get("OCR_URL", "http://localhost:8000")
TIMEOUT = 3600
TAB_WIDTH = 4
ANSI_RE = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
HARD_MARKER = "===============save results:==============="
# tags
OPEN_TAGS = ("<|det|>", "<|ref|>")
CLOSE_TAGS = {
"<|det|>": "<|/det|>",
"<|ref|>": "<|/ref|>",
}
MAX_OPEN_LEN = max(len(t) for t in OPEN_TAGS)
# noise to kill
NOISE_LINE_PREFIXES = (
"image:",
"other:",
"video:",
"audio:",
"base:",
"patches:",
"====================",
"===============save",
"===============",
)
NOISE_SUBSTRINGS = (
"torch.size(",
" it/s]",
"it/s]",
"progress:",
)
# ---------------------------------------------------------------------
# final cleanup
# ---------------------------------------------------------------------
def final_cleanup(text: str) -> str:
"""remove streamer/mode/torch noise & everything after hard marker"""
if not text:
return ""
lower = text.lower()
cut_pos = lower.find(HARD_MARKER.lower())
if cut_pos != -1:
text = text[:cut_pos]
lines = text.splitlines()
out: List[str] = []
drop_prefixes = (
"====================",
"===============save",
"base:",
"no patches",
"image:",
"other:",
"video:",
"audio:",
"patches:",
)
progress_re = re.compile(r".*\b\d{1,3}%\|#+\|.*")
for ln in lines:
s = ln.strip()
if not s:
continue
sl = s.lower()
if any(sl.startswith(p) for p in drop_prefixes):
continue
if "torch.size(" in sl:
continue
if progress_re.match(s):
continue
out.append(ln)
# dedupe consecutive
deduped: List[str] = []
last = None
for ln in out:
if ln == last:
continue
deduped.append(ln)
last = ln
return "\n".join(deduped).strip()
# ---------------------------------------------------------------------
# clipboard via pyperclip
# ---------------------------------------------------------------------
def copy_to_clipboard(text: str) -> bool:
if not text:
return False
try:
pyperclip.copy(text)
return True
except Exception as e:
# best effort — do not block, do not explode
sys.stderr.write(f"[warn] pyperclip copy failed: {e}\n")
return False
# ---------------------------------------------------------------------
# collector
# ---------------------------------------------------------------------
class ClipBuffer:
def __init__(self):
self.parts: List[str] = []
def write(self, chunk: str):
if chunk:
self.parts.append(chunk)
def get_text(self) -> str:
return "".join(self.parts)
def wrap(self, iterable: Iterable[str]) -> Iterable[str]:
for part in iterable:
self.write(part)
yield part
# ---------------------------------------------------------------------
# tag stripper
# ---------------------------------------------------------------------
def strip_tags_stream(chunks: Iterable[str]) -> Iterable[str]:
pending = ""
in_tag = False
current_close = ""
for chunk in chunks:
if not chunk:
continue
chunk = ANSI_RE.sub("", chunk)
for ch in chunk:
pending += ch
if not in_tag:
opened = False
for ot in OPEN_TAGS:
if pending.endswith(ot):
before = pending[:-len(ot)]
if before:
yield before
in_tag = True
current_close = CLOSE_TAGS[ot]
pending = ""
opened = True
break
if opened:
continue
if len(pending) > MAX_OPEN_LEN:
# flush oldest
yield pending[0]
pending = pending[1:]
else:
if pending.endswith(current_close):
pending = ""
in_tag = False
current_close = ""
else:
if len(pending) > len(current_close) * 2:
pending = pending[-len(current_close):]
if not in_tag and pending:
yield pending
# ---------------------------------------------------------------------
# lang detector
# ---------------------------------------------------------------------
class NaturalLang:
HU_DIAC = set("áéíóöőúüű")
def __init__(self):
self.fallback = {"hu": 0, "en": 0, "de": 0}
self.total = 0
self.current = "unk"
self.buf: List[str] = []
def feed(self, tok: str):
self.total += 1
self.buf.append(tok)
if len(self.buf) > 200:
self.buf = self.buf[-200:]
if not HAS_LANGDETECT:
t = tok.lower()
if any(c in t for c in self.HU_DIAC):
self.fallback["hu"] += 2
if t in ("the", "and", "is", "are", "to", "of"):
self.fallback["en"] += 2
if t in ("und", "die", "der", "das", "ist"):
self.fallback["de"] += 2
if self.total % 20 == 0:
lang, sc = max(self.fallback.items(), key=lambda x: x[1])
self.current = lang if sc > 0 else "unk"
def get(self) -> str:
if HAS_LANGDETECT and self.buf and self.total % 25 == 0:
try:
text = " ".join(self.buf)
lang = ld_detect(text)
self.current = lang
except Exception:
pass
return self.current or "unk"
# ---------------------------------------------------------------------
# code formatting helpers
# ---------------------------------------------------------------------
def has_clang_format() -> bool:
return shutil.which("clang-format") is not None
def run_clang_format(code: str) -> Optional[str]:
if not has_clang_format():
return None
try:
p = subprocess.run(
["clang-format"],
input=code.encode("utf-8"),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)
if p.returncode == 0:
return p.stdout.decode("utf-8", "replace")
except Exception:
return None
return None
def format_python(code: str) -> str:
if HAS_BLACK:
try:
return black.format_str(code, mode=black.FileMode())
except Exception:
pass
if HAS_AUTOPEP8:
try:
return autopep8.fix_code(code)
except Exception:
pass
return code.expandtabs(TAB_WIDTH)
def format_json_text(code: str) -> str:
try:
obj = json.loads(code)
return json.dumps(obj, indent=4, ensure_ascii=False)
except Exception:
return code.expandtabs(TAB_WIDTH)
def format_cpp(code: str) -> str:
cf = run_clang_format(code)
if cf is not None:
return cf
return code.expandtabs(TAB_WIDTH)
def guess_code_lang(code: str) -> str:
if HAS_PYGMENTS:
try:
lexr = guess_lexer(code)
name = lexr.name.lower()
if "python" in name:
return "code:python"
if "json" in name:
return "code:json"
if "c++" in name or "cpp" in name or "c " in name or name == "c":
return "code:cpp"
if "bash" in name or "shell" in name:
return "code:sh"
if "java" in name:
return "code:java"
return "code:" + name.replace(" ", "_")
except ClassNotFound:
return "code:generic"
except Exception:
return "code:generic"
return "code:generic"
def format_code(code: str, lang_hint: str) -> str:
if lang_hint == "code:generic":
lang_hint = guess_code_lang(code)
if lang_hint == "code:python":
return format_python(code)
if lang_hint == "code:json":
return format_json_text(code)
if lang_hint in ("code:cpp", "code:java"):
return format_cpp(code)
return code.expandtabs(TAB_WIDTH)
# ---------------------------------------------------------------------
# live code block
# ---------------------------------------------------------------------
class LiveCodeBlock:
def __init__(self, sink: "CursesSink"):
self.sink = sink
self.active = False
self.text_parts: List[str] = []
self.last_lines: List[str] = []
self.lang_hint = "code:generic"
def start(self):
self.active = True
self.text_parts.clear()
self.last_lines = []
self.lang_hint = "code:generic"
self.sink.toggle_code()
def feed(self, tok: str):
if not self.active:
return
self.text_parts.append(tok)
raw = "".join(self.text_parts).replace("\r", "\n")
if self.lang_hint == "code:generic" and len(raw) > 12:
self.lang_hint = guess_code_lang(raw)
self.sink.set_code_label(self.lang_hint)
formatted = format_code(raw, self.lang_hint)
lines = formatted.splitlines()
old_n = len(self.last_lines)
new_n = len(lines)
if new_n > old_n:
for ln in lines[old_n:]:
self.sink.add_newline()
self.sink.add_code_line(ln)
self.last_lines = lines
def finish(self):
if not self.active:
return
self.active = False
self.text_parts.clear()
self.last_lines = []
self.sink.toggle_code()
self.sink.add_newline()
# ---------------------------------------------------------------------
# curses sink
# ---------------------------------------------------------------------
class CursesSink:
def __init__(self, stdscr, natdet: NaturalLang):
self.stdscr = stdscr
self.max_y, self.max_x = self.stdscr.getmaxyx()
self.pad = curses.newpad(20000, self.max_x)
self.line = 0
self.col = 0
self.scroll_top = 0
self.in_code_block = False
self.colors_ok = False
self.natdet = natdet
self.code_label = "unk"
self._init_colors()
def _init_colors(self):
if not curses.has_colors():
return
try:
curses.start_color()
curses.use_default_colors()
except curses.error:
pass
def sp(i, fg, bg=-1):
try:
curses.init_pair(i, fg, bg)
return True
except curses.error:
try:
curses.init_pair(i, fg, 0)
return True
except curses.error:
return False
ok1 = sp(1, curses.COLOR_BLUE)
ok2 = sp(2, curses.COLOR_CYAN)
ok3 = sp(3, curses.COLOR_BLACK)
ok4 = sp(4, curses.COLOR_MAGENTA)
ok5 = sp(5, curses.COLOR_YELLOW)
ok6 = sp(6, curses.COLOR_GREEN)
ok7 = sp(7, curses.COLOR_RED)
ok8 = sp(8, curses.COLOR_GREEN)
ok9 = sp(9, curses.COLOR_YELLOW)
ok10 = sp(10, curses.COLOR_BLUE)
self.colors_ok = any([ok1, ok2, ok3, ok4, ok5, ok6, ok7, ok8, ok9, ok10])
def set_code_label(self, lbl: str):
self.code_label = lbl
def draw_title(self):
nat = self.natdet.get()
label = self.code_label if self.in_code_block else nat
title = " OCR stream — q=quit "
tail = f" lang: {label or 'unk'} "
try:
if self.colors_ok:
self.stdscr.attron(curses.color_pair(4) | curses.A_BOLD)
self.stdscr.addstr(0, 0, title[: self.max_x - 1])
if self.colors_ok:
self.stdscr.attroff(curses.color_pair(4) | curses.A_BOLD)
if self.colors_ok:
if self.in_code_block or (label and label.startswith("code:")):
self.stdscr.attron(curses.color_pair(6) | curses.A_BOLD)
else:
self.stdscr.attron(curses.color_pair(5) | curses.A_BOLD)
self.stdscr.addstr(0, len(title), tail[: self.max_x - len(title) - 1])
if self.colors_ok:
if self.in_code_block or (label and label.startswith("code:")):
self.stdscr.attroff(curses.color_pair(6) | curses.A_BOLD)
else:
self.stdscr.attroff(curses.color_pair(5) | curses.A_BOLD)
except curses.error:
pass
self.stdscr.refresh()
def _newline(self):
self.line += 1
self.col = 0
if self.line >= self.max_y - 1:
self.scroll_top = self.line - (self.max_y - 2)
def _refresh(self):
try:
self.pad.refresh(self.scroll_top, 0, 1, 0, self.max_y - 1, self.max_x - 1)
except curses.error:
pass
self.draw_title()
def toggle_code(self):
self.in_code_block = not self.in_code_block
def add_space(self):
if self.col + 1 >= self.max_x - 1:
self._newline()
else:
try:
self.pad.addstr(self.line, self.col, " ")
except curses.error:
pass
self.col += 1
self._refresh()
def add_newline(self):
self._newline()
self._refresh()
def add_word(self, word: str, style: str | None = None):
word = word.expandtabs(TAB_WIDTH)
if len(word) > self.max_x - 1:
chunks = [word[i:i + self.max_x - 1] for i in range(0, len(word), self.max_x - 1)]
else:
chunks = [word]
for idx, chunk in enumerate(chunks):
if self.col + len(chunk) >= self.max_x - 1:
self._newline()
attr = curses.A_NORMAL
if self.colors_ok:
if style == "heading":
attr = curses.color_pair(1) | curses.A_BOLD
elif style == "list":
attr = curses.color_pair(2)
elif style == "code":
attr = curses.color_pair(3)
elif style == "heading":
attr = curses.A_BOLD
try:
self.pad.addstr(self.line, self.col, chunk, attr)
except curses.error:
pass
self.col += len(chunk)
if idx < len(chunks) - 1:
self._newline()
self._refresh()
def add_code_line(self, line: str):
line = line.expandtabs(TAB_WIDTH)
if not HAS_PYGMENTS or not self.colors_ok:
self.add_word(line, style="code")
return
try:
tokens = list(lex(line, guess_lexer(line)))
except Exception:
self.add_word(line, style="code")
return
if self.col != 0:
self._newline()
for tok_type, tok_val in tokens:
if tok_type in Token.Keyword:
attr = curses.color_pair(7) | curses.A_BOLD
elif tok_type in Token.String:
attr = curses.color_pair(8)
elif tok_type in Token.Number:
attr = curses.color_pair(9)
elif tok_type in Token.Comment:
attr = curses.color_pair(10)
else:
attr = curses.color_pair(3)
for sub in tok_val.splitlines(True):
if sub == "\n":
self._newline()
continue
if self.col + len(sub) >= self.max_x - 1:
self._newline()
try:
self.pad.addstr(self.line, self.col, sub, attr)
except curses.error:
pass
self.col += len(sub)
self._refresh()
# ---------------------------------------------------------------------
# ncurses loop
# ---------------------------------------------------------------------
def emit_words_ncurses(stdscr, chunks: Iterable[str]) -> None:
nat = NaturalLang()
sink = CursesSink(stdscr, nat)
code_live = LiveCodeBlock(sink)
buf = ""
at_line_start = True
skip_line = False
last_was_newline = False
prev_word_lower = ""
current_line_style = None
stdscr.nodelay(True)
for chunk in chunks:
if not chunk:
continue
chunk = chunk.expandtabs(TAB_WIDTH)
buf += chunk
i = 0
L = len(buf)
while i < L:
try:
key = stdscr.getch()
if key in (ord("q"), ord("Q")):
return
except Exception:
pass
ch = buf[i]
if ch.isspace():
j = i
has_nl = False
while j < L and buf[j].isspace():
if buf[j] == "\n":
has_nl = True
j += 1
if has_nl:
if code_live.active:
code_live.feed("\n")
else:
if not skip_line and not last_was_newline:
sink.add_newline()
at_line_start = True
skip_line = False
last_was_newline = True
prev_word_lower = ""
current_line_style = None
else:
if code_live.active:
code_live.feed(" ")
else:
if not skip_line and not at_line_start:
sink.add_space()
at_line_start = False
last_was_newline = False
i = j
continue
j = i
while j < L and not buf[j].isspace():
j += 1
word = buf[i:j]
lower = word.lower().lstrip()
# HARD STOP
if "save results" in lower or lower == HARD_MARKER.lower():
return
if prev_word_lower == "save" and lower.startswith("result"):
return
# code fence
if lower == "```":
if code_live.active:
code_live.finish()
else:
code_live.start()
nat.feed(lower)
at_line_start = False
last_was_newline = False
prev_word_lower = lower
i = j
continue
# line-start noise
if at_line_start and not code_live.active:
tl = lower.lstrip("#").lstrip()
for pfx in NOISE_LINE_PREFIXES:
if tl.startswith(pfx):
skip_line = True
break
# inline noise
if not skip_line and not code_live.active:
if any(s in lower for s in NOISE_SUBSTRINGS):
skip_line = True
nat.feed(lower)
if code_live.active:
code_live.feed(word)
else:
if at_line_start and not skip_line and not sink.in_code_block:
stripped = word.lstrip()
if stripped.startswith("#"):
current_line_style = "heading"
elif stripped in ("-", "*", "+") or re.match(r"\d+\.$", stripped):
current_line_style = "list"
else:
current_line_style = None
if not skip_line:
if current_line_style == "heading":
sink.add_word(word, style="heading")
elif current_line_style == "list":
sink.add_word(word, style="list")
else:
sink.add_word(word)
at_line_start = False
last_was_newline = False
prev_word_lower = lower
i = j
buf = buf[i:]
# ---------------------------------------------------------------------
# HTTP streaming (pyperclip at end + HARD EXIT)
# ---------------------------------------------------------------------
def stream_infer(url: str, img_name: str, img_bytes: bytes, use_curses: bool = True) -> int:
endpoint = url.rstrip("/") + "/infer"
files = {"file": (img_name, io.BytesIO(img_bytes), "image/png")}
headers = {"Expect": ""}
collector = ClipBuffer()
resp = None
try:
resp = requests.post(endpoint, files=files, headers=headers, stream=True, timeout=TIMEOUT)
if resp.status_code != 200:
sys.stderr.write(f"/infer failed: HTTP {resp.status_code}\n{resp.text}\n")
if resp is not None:
resp.close()
return 1
def chunk_iter():
for c in resp.iter_content(chunk_size=4096, decode_unicode=True):
if not c:
continue
c = c.replace("\r", "\n")
yield c
if HARD_MARKER.lower() in c.lower():
break
tagless = strip_tags_stream(chunk_iter())
mirrored = collector.wrap(tagless)
if use_curses and curses is not None and sys.stdout.isatty():
curses.wrapper(emit_words_ncurses, mirrored)
else:
for part in mirrored:
sys.stdout.write(part)
sys.stdout.flush()
if HARD_MARKER.lower() in part.lower():
break
except KeyboardInterrupt:
text = collector.get_text()
clean = final_cleanup(text)
copy_to_clipboard(clean)
if resp is not None:
resp.close()
os._exit(0)
except Exception as e:
sys.stderr.write(f"Request error: {e}\n")
text = collector.get_text()
clean = final_cleanup(text)
copy_to_clipboard(clean)
if resp is not None:
resp.close()
os._exit(1)
# normal exit
text = collector.get_text()
clean = final_cleanup(text)
copy_to_clipboard(clean)
if resp is not None:
try:
resp.close()
except Exception:
pass
# and now: really exit
os._exit(0)
# ---------------------------------------------------------------------
# screen capture
# ---------------------------------------------------------------------
def grab_full_desktop() -> tuple["Image.Image", dict]:
with mss.mss() as sct:
mon_all = sct.monitors[0]
shot = sct.grab(mon_all)
img = Image.frombytes("RGB", shot.size, shot.rgb)
return img, mon_all
def select_region(img_full: "Image.Image", bbox: dict) -> Optional[Tuple[int, int, int, int]]:
root = tk.Tk()
root.overrideredirect(True)
root.attributes("-topmost", True)
left = int(bbox.get("left", 0))
top = int(bbox.get("top", 0))
w = int(bbox.get("width", img_full.size[0]))
h = int(bbox.get("height", img_full.size[1]))
try:
root.geometry(f"{w}x{h}{'+' if left >= 0 else ''}{left}{'+' if top >= 0 else ''}{top}")
except Exception:
root.geometry(f"{w}x{h}+0+0")
canvas = tk.Canvas(root, width=w, height=h, highlightthickness=0, bd=0)
canvas.pack(fill="both", expand=True)
photo = ImageTk.PhotoImage(img_full)
canvas.create_image(0, 0, image=photo, anchor="nw")
canvas.create_rectangle(0, 0, w, h, fill="#000000", stipple="gray50", outline="")
sel = canvas.create_rectangle(0, 0, 0, 0, outline="#00ff00", width=2)
start = {"x": 0, "y": 0}
result = {"ok": False, "x1": 0, "y1": 0, "x2": 0, "y2": 0}
canvas.create_text(
20,
20,
anchor="nw",
text="Drag to select area. Release to confirm. ESC to cancel.",
fill="#ffffff",
font=("TkDefaultFont", 14, "bold"),
)
def on_press(e):
start["x"], start["y"] = e.x, e.y
canvas.coords(sel, e.x, e.y, e.x, e.y)
def on_drag(e):
ex = max(0, min(e.x, w - 1))
ey = max(0, min(e.y, h - 1))
canvas.coords(sel, start["x"], start["y"], ex, ey)
def on_release(e):
ex = max(0, min(e.x, w - 1))
ey = max(0, min(e.y, h - 1))
canvas.coords(sel, start["x"], start["y"], ex, ey)
x1, y1, x2, y2 = canvas.coords(sel)
result["ok"] = True
result["x1"] = int(x1)
result["y1"] = int(y1)
result["x2"] = int(x2)
result["y2"] = int(y2)
root.quit()
def on_key(e):
if e.keysym == "Escape":
result["ok"] = False
root.quit()
canvas.bind("<ButtonPress-1>", on_press)
canvas.bind("<B1-Motion>", on_drag)
canvas.bind("<ButtonRelease-1>", on_release)
root.bind("<Escape>", on_key)
root.mainloop()
try:
root.destroy()
except Exception:
pass
if not result["ok"]:
return None
x1 = result["x1"]
y1 = result["y1"]
x2 = result["x2"]
y2 = result["y2"]
if x2 < x1:
x1, x2 = x2, x1
if y2 < y1:
y1, y2 = y2, y1
return (x1, y1, x2, y2)
# ---------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------
def main(argv=None) -> int:
p = argparse.ArgumentParser("ocr live streamer (pyperclip)")
p.add_argument("image", nargs="?", help="Image path; if missing, capture from screen.")
p.add_argument("--url", "-u", default=DEFAULT_URL, help=f"OCR API base url (default: {DEFAULT_URL})")
p.add_argument("--no-curses", action="store_true", help="Disable ncurses.")
args = p.parse_args(argv)
use_curses = not args.no_curses
if args.image:
if not os.path.isfile(args.image):
print(f"File not found: {args.image}", file=sys.stderr)
return 2
with open(args.image, "rb") as f:
data = f.read()
return stream_infer(args.url, os.path.basename(args.image), data, use_curses=use_curses)
# interactive capture
try:
img, bbox = grab_full_desktop()
except Exception as e:
print(f"Failed to capture screen: {e}", file=sys.stderr)
return 1
box = select_region(img, bbox)
if box is None:
print("Cancelled.", file=sys.stderr)
return 1
x1, y1, x2, y2 = box
if x2 - x1 < 2 or y2 - y1 < 2:
print("Selection too small.", file=sys.stderr)
return 1
crop = img.crop((x1, y1, x2, y2))
buf = io.BytesIO()
crop.save(buf, format="PNG")
return stream_infer(args.url, "selection.png", buf.getvalue(), use_curses=use_curses)
if __name__ == "__main__":
# in practice stream_infer → os._exit(0), so this line rarely returns
rc = main()
sys.exit(rc)