Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I use Flameshot combined with Tesseract and zbarimg to quickly clip areas of the screen and either OCR them or decode barcodes, which I then map to hotkey combinations.

For example, I have `bash -c 'flameshot gui -s -r | tesseract - - | gxmessage -title "Decoded Data" -fn "Consolas 12" -wrap -geometry 640x480 -file -'` mapped to Super+O, so I can just press the key combo, select a region of the screen, and have the OCRed text immediately displayed in a dialog box from gxmessage (which accounts for most of the command line). Replace 'tesseract' with 'zbarimg' and you have a barcode scanner.



Great idea - I ended up experimenting to improve the ocr accuracy:

    #!/bin/bash

    screenshot=$(mktemp)
    decoded_data=$(mktemp)
    processed_data=$(mktemp)

    cleanup() {
        rm "$screenshot" "$decoded_data" "$processed_data"
    }

    trap cleanup EXIT

    flameshot gui -s -r > "$screenshot"

    convert "$screenshot" \
        -colorspace Gray \
        -scale 1191x2000 \
        -unsharp 6.8x2.69+0 \
            -resize 500% \
        "$screenshot"

    tesseract \
        --dpi 300 \
        --oem 1 "$screenshot" - > "$decoded_data"

    grep -v '^\s*$' "$decoded_data" > "$processed_data"

    cat "$processed_data" | \
        xclip -selection clipboard

    yad --text-info --title="Decoded Data" \
        --width=940 \
        --height=580 \
        --wrap \
        --fontname="Iosevka 14" \
        --editable \
        --filename="$processed_data"


Nice! Once you start getting complex, a standalone script might be a good idea. But it should be noted that your ImageMagick processing can also be inserted into the original one-liner:

    bash -c 'flameshot gui -s -r | convert - -colorspace Gray -scale 1191x2000 -unsharp 6.8x2.69+0 -resize 500% png:- | tesseract - - | gxmessage -title "Decoded Data" -fn "Consolas 12" -wrap -geometry 640x480 -file -'


Indeed; I just wanted to break it into sequential steps so that in case of issues, I can conveniently add debugging steps in the middle as needed.


Last year, when I want to find a tool to do the sanpshot and OCR job, I found flameshot. However, the OCR feature hasn't been added as native function due to some issues I'm not very clear.

So I spent some time added the OCR function into flameshot. I didn't choose to compile tesseract into flameshot, but using the rest api way to call a server running remotely. The reason for this way is I also added llama.cpp translation feature after OCR.

Here're github repositories for my fork of flameshot and the OCR and translation server which is written causually in Rust.

https://github.com/jason-ni/flameshot https://github.com/jason-ni/flameshot-ocr-server


If you've installed Flameshot using homebrew on MacOS, this will do a similar thing:

  /opt/homebrew/Caskroom/flameshot/12.1.0/flameshot.app/Contents/MacOS/flameshot gui -s -r | tesseract - - | (text=$(cat) && osascript -e "display dialog \"$text\" with title \"OCR Result\" buttons {\"OK\"} default button \"OK\"")




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: