ArchLinux OCR

在 windows 上面有挺不错的天若 ocr 软件可以用,最近发现自己手上竟然没有一款合适的 Linux ocr 软件可用。于是有了本文。


本来觉得 ArchLinux 应该是什么轮子都有,但是似乎并不是。也可能是我自己没搜到吧。如果你有更好的方法,更加简单的记得分享啊。我这里主要是参考了 知乎一篇文章 的方法。主要是为我自己的环境做了些适配。

我的环境是 ArchLinux, kde 桌面。手上用的截图软件是 falmeshot 。识别程序的核心是 tesseract,相应的语言包我只选了英语和中文,你可以按照自己的需要添加和删减。

首先是安装需要的包:

1
yay -S tesseract tesseract-data-chi_sim tesseract-data-chi_tra tesseract-data-eng falmeshot xclip

安装好之后在本地建立运行脚本:

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
#!/bin/env bash 
# Dependencies: tesseract-ocr imagemagick gnome-screenshot xclip

#Name: OCR Picture
#Author:andrew
#Fuction: take a screenshot and OCR the letters in the picture
#Path: /home/Username/...
#Date: 2020-02-10

#you can only scan one character at a time
SCR="/tmp/ocr"

####take a shot what you wana to OCR to text
# gnome-screenshot -a -f $SCR.png
flameshot gui -p /tmp -r > $SCR.png

####increase the png
mogrify -modulate 100,0 -resize 400% $SCR.png
#should increase detection rate

####OCR by tesseract
tesseract $SCR.png $SCR &> /dev/null -l eng+chi_tra+chi_sim

####use sed to delete the blanks & get the text and copy to clipboard
cat $SCR.txt | sed 's/ //g' | xclip -selection clipboard

#需要删除换行请使用此语句 并注释上一句(行首加#)
#cat $SCR.txt | sed 's/ //g'| xargs | xclip -selection clipboard

#弹窗提示OCR结束 the code below Thanks to 陈留阳
notify-send "OCR Done"

exit

记得给脚本可执行权限 chmod +x ocr.sh。 接下来在终端运行该脚本就能愉快的截图的。

为了使用更加方便,再给它添加一个快捷建,我是 kde 桌面,在 setting>Shortcuts>sustom shortcuts>Edit>New>Global shortcut>command/url 下添加即可。Trigger 设置快捷键,Action 下填脚本的完整位置。保存后,接下来用你的快捷键愉快的截图并识别了!

识别使用图如下,看起来效果不太好,可能以后试试用百度之类提供的接口吧。

out

Welcome to my other publishing channels