-
Notifications
You must be signed in to change notification settings - Fork 1
/
ocr.sh
executable file
·66 lines (55 loc) · 2.13 KB
/
ocr.sh
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
#!/usr/bin/env bash
#
# Siddharth Dushantha 2020
#
# https://github.com/sdushantha/bin
#
TEXT_FILE="/tmp/ocr.txt"
IMAGE_FILE="/tmp/ocr.png"
# Check if the needed dependencies are installed
dependencies=(tesseract maim notify-send xclip)
for dependency in "${dependencies[@]}"; do
type -p "$dependency" &>/dev/null || {
# The reason why we are sending the error as a notification is because
# user is most likely going to run this script by binding it to their
# keyboard, therefor they cant see any text that is outputed using echo
notify-send "ocr" "Could not find '${dependency}', is it installed?"
echo "Could not find '${dependency}', is it installed?"
exit 1
}
done
# Take screenshot by selecting the area
maim -s "$IMAGE_FILE"
# Get the exit code of the previous command.
# So in this case, it is the screenshot command. If it did not exit with an
# exit code 0, then it means the user canceled the process of taking a
# screenshot by doing something like pressing the escape key
STATUS=$?
# If the user pressed the escape key or did something to terminate the proccess
# taking a screenshot, then just exit
[ $STATUS -ne 0 ] && exit 1
# Do the magic (∩^o^)⊃━☆゚.*・。゚
# Notice how I have removing the extension .txt from the file path. This is
# because tesseract adds .txt to the given file path anyways. So if we were to
# specify /tmp/ocr.txt as the file path, tesseract would out the text to
# /tmp/ocr.txt.txt
tesseract "$IMAGE_FILE" "${TEXT_FILE//\.txt/}" 2> /dev/null
# Remove the new page character.
# Source: https://askubuntu.com/a/1276441/782646
sed -i 's/\x0c//' "$TEXT_FILE"
# Check if the text was detected by checking number
# of lines in the file
NUM_LINES=$(wc -l < $TEXT_FILE)
if [ "$NUM_LINES" -eq 0 ]; then
notify-send "ocr" "no text was detected"
exit 1
fi
# Copy text to clipboard
xclip -selection clip < "$TEXT_FILE"
# Send a notification with the text that was grabbed using OCR
notify-send "ocr" "$(cat $TEXT_FILE)"
# Clean up
# "Always leave the area better than you found it"
# - My first grade teacher
rm "$TEXT_FILE"
rm "$IMAGE_FILE"