-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
executable file
·118 lines (93 loc) · 2.45 KB
/
main.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
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
#!/bin/bash
TEXT_FILE="/tmp/ocr.txt"
IMAGE_FILE="/tmp/ocr.png"
notify(){
notify-send "Screenshot" "$1"
}
check_deps(){
dependencies=(tesseract maim notify-send xclip trans)
for dependency in "${dependencies[@]}"; do
type -p "$dependency" &>/dev/null || {
notify "Could not find '${dependency}', is it installed?"
echo "Could not find '${dependency}', is it installed?"
exit 1
}
done
}
do_ocr(){
# 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.
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 "No text was detected"
exit 1
fi
}
mode_copy(){
xclip -selection clipboard -t image/png < "$IMAGE_FILE"
}
mode_save(){
FILE_BASE=$(rofi -dmenu -p "Enter file name")
FILE_PATH="/tmp/$FILE_BASE.png"
cp "$IMAGE_FILE" "$FILE_PATH"
notify "Saved to $FILE_PATH."
}
mode_ocr(){
# Do OCR
do_ocr
# Copy text to clipboard
xclip -selection clip < "$TEXT_FILE"
# Send a notification with the text that was grabbed using OCR
notify "$(cat $TEXT_FILE)"
}
mode_scholar(){
do_ocr
# search google scholar for it
sed -i 's/ /_/' "$TEXT_FILE"
xdg-open "https://scholar.google.com/scholar?q=$(cat $TEXT_FILE)"
}
mode_translate(){
do_ocr
TRANS=$(trans -brief en:de "$(cat $TEXT_FILE)" || echo "Error translating.")
notify "$(cat $TEXT_FILE):\n$TRANS"
}
mode_qr(){
QR_CONTENT=$(zbarimg "$IMAGE_FILE" 2>/dev/null | head -n1 | cut -d':' -f2-)
echo "$QR_CONTENT" | xclip -selection clipboard
notify "$QR_CONTENT\nCopied to clipboard"
}
main(){
check_deps
# Take screenshot by selecting the area, exit if aborted
maim -s "$IMAGE_FILE" || exit 1
MODE=$(printf " Copy image to clipboard\n Save image to file\n Text recognition\n Google scholar search\n Translate to german\n Scan QR code" | rofi -dmenu -p "Mode ")
case $MODE in
" Copy image to clipboard")
mode_copy
;;
" Save image to file")
mode_save
;;
" Text recognition")
mode_ocr
;;
" Google scholar search")
mode_scholar
;;
" Scan QR code")
mode_qr
;;
" Translate to german")
mode_translate
;;
esac
rm "$TEXT_FILE" 2>/dev/null
rm "$IMAGE_FILE" 2>/dev/null
}
main