summaryrefslogtreecommitdiffstats
path: root/public/.local/bin/screenshot_ocr.sh
blob: d88accf0f0e14aaec77d272af3cb9d05dd24f43a (plain)
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
#!/bin/bash

# Select a region of the screen and perform OCR to it.  Save text in clipboard.

savepath="/tmp/screenshot_ocr.sh.d"
filename="$HOSTNAME-$(date -Iseconds | tr -d ':-').png"
# German: deu,  Spanish: spa,  Japanese vertical: jpn_vert
tesseract_options="-l eng+jpn"

_init() {
    if [ ! -d "$savepath" ]; then
        mkdir -p "$savepath"
    fi
    cd "$savepath"
}

_init
# Screenshot region of screen
shotgun -g $(hacksaw) "$filename"
# Exit if previous command was unsuccessful
test $? -ne 0 && exit
# Try to scan codes from the image
zbarimg $filename | xclip -selection "clipboard"
# Perform OCR and save to clipboard
tesseract $tesseract_options $filename - | xclip -selection "clipboard"
rm $filename

# TODO: clean japanese output.  If text is 80% japanese characters, then it
# should not contain spaces around the characters.