From 1fa6122648258d93de65a38428332721b0c12606 Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Sun, 16 May 2021 19:09:38 -0400 Subject: [PATCH] ocr script --- bin/ocr | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 bin/ocr diff --git a/bin/ocr b/bin/ocr new file mode 100755 index 0000000..6f24fbf --- /dev/null +++ b/bin/ocr @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +# Yoinked from https://github.com/JJGO/dotfiles +# Adapted from https://github.com/sdushantha/bin + +set -x +TEXT_FILE="/tmp/ocr.txt" +IMAGE_FILE="/tmp/ocr.png" + +function notify-send() { + osascript -e "display notification \"$2\" with title \"OCR\"" +} + +PATH="/usr/local/bin/:$PATH" + +# Check if the needed dependencies are installed +dependencies=(tesseract) +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?" + exit 1 + } +done + +# Take screenshot by selecting the area +screencapture -i "$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/}" + +# Check if the text was detected by checking number +# of lines in the file +LINES=$(wc -l < $TEXT_FILE) +if [ "$LINES" -eq 0 ]; then + notify-send "ocr" "no text was detected" + exit 1 +fi + +# Copy text to clipboard +# xclip -selection clip < "$TEXT_FILE" +pbcopy < "$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"