Add tools for interacting with scanner

This commit is contained in:
The Magician 2024-11-24 17:47:43 +00:00
parent 9d6d39d75f
commit 2fdbb0d467
6 changed files with 171 additions and 0 deletions

59
scanner/card_scanner Executable file
View File

@ -0,0 +1,59 @@
#!/bin/bash
check_error() {
if test $? -ne 0; then
echo "card_scanner: $1 exited with unexpected error"
exit 1
fi
}
STORAGE_DIR="$HOME/.local/share/sevenkeys/scanimages/"
mkdir -p "$STORAGE_DIR"
printf "Database profile: "
read profile
printf "Storage area name: "
read storageAreaName
ADD_CARDS=0
echo "scantap: Beginning scan loop"
while true; do
rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)"
filename="$STORAGE_DIR/$rng.png"
scanimage --output-file="$filename" --source "ADF Front" --mode Color --page-width 63mm --page-height 88mm 2>/dev/null
# scanimage exits with code 7 if no documents are available in scanner
if test $? -eq 7; then
if test $ADD_CARDS -eq 0; then
ADD_CARDS=1
echo "scantap: No more cards in feeder" >&2
fi
# If we have generated a zero-length file, then delete it
if ! test -s "$filename"; then
rm "$filename"
fi
continue
fi
ADD_CARDS=0
check_error "scanimage"
convert -rotate 180 "$filename" "$filename"
check_error "convert"
cardLocationId="$(./sevenkeys --profile="$profile" store --storagearea="$storageAreaName" --id="00000000-0000-0000-0000-0000000000000")"
check_error "sevenkeys"
if test "$profile" == "development"; then
databaseName="sevenkeys_development"
else
databaseName="sevenkeys"
fi
mysql --silent --silent --user=root --password="$(pass show sevenkeys/mysql)" \
-e "USE $databaseName; INSERT INTO CardScan (CardLocationId, Filename) VALUES ('$cardLocationId', '$rng.png');"
check_error "mysql"
done

8
scanner/crop Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
crop() {
convert "$1" -crop 1200x120+90+100 "$2"
}
crop old_border.png name_old.png
crop new_border.png name_new.png

49
scanner/profiler Executable file
View File

@ -0,0 +1,49 @@
#!/bin/bash
CLASSNAME="svnProfiler"
TITLE_FILENAME="/tmp/title.png"
touch "$TITLE_FILENAME"
get_window_id() {
while true; do
id="$(xdotool search --classname "$CLASSNAME")"
if test "$?" -ne 0; then
continue
fi
echo "$id"
break
done
}
printf "Database profile: "
read profile
if test "$profile" == "development"; then
databaseName="sevenkeys_development"
else
databaseName="sevenkeys"
fi
STORAGE_DIR="$HOME/.local/share/sevenkeys/scanimages/"
cd "$STORAGE_DIR"
files="$(find *.png)"
nsxiv -N "$CLASSNAME" $files &
nsxivWindowId="$(get_window_id)"
nsxiv "$TITLE_FILENAME" &
for file in $files; do
cardLocationId="$(mysql --silent --silent --user=root --password="$(pass show sevenkeys/mysql)" -e "USE $databaseName; SELECT CardLocationId FROM CardScan WHERE Filename = '$file';")"
# TODO: Detect features of the card automatically
#convert "$file" -crop 1200x120+90+100 "$TITLE_FILENAME"
#title="$(tesseract --psm 9 "$TITLE_FILENAME" stdout)"
#echo "$title"
cardPrintingId="$(sevenkeys --profile="$profile" search-printings)"
sevenkeys --profile="$profile" replace --card-location-id="$cardLocationId" --card-printing-id="$cardPrintingId"
xdotool key --window "$nsxivWindowId" 'n'
done

9
scanner/psm_test Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
for i in $(seq 1 13); do
echo "PSM $i:" >>output
echo "Old card:" >>output
tesseract name_old.png stdout --psm "$i" >>output
echo "New card:" >>output
tesseract name_new.png stdout --psm "$i" >>output
echo >>output
done

34
scanner/test_card_scanner_count Executable file
View File

@ -0,0 +1,34 @@
#!/bin/bash
if test -z "$1"; then
echo "usage: test_card_scanner_count <count>" >&2
exit 1
fi
STORAGE_DIR="$HOME/.local/share/sevenkeys/testimages/"
mkdir -p "$STORAGE_DIR"
starting_file_count="$(find $STORAGE_DIR/*.png | wc -l)"
echo "test_card_scanner_count: Beginning test"
while true; do
rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)"
filename="$STORAGE_DIR/$rng.png"
scanimage --output-file="$filename" --source "ADF Front" --mode Color --page-width 63mm --page-height 88mm 2>/dev/null
# scanimage exits with code 7 if no documents are available in scanner
if test $? -eq 7; then
echo "test_card_scanner_count: No more cards in feeder" >&2
rm "$filename"
break
fi
done
ending_file_count="$(find $STORAGE_DIR/*.png | wc -l)"
if test $(( ending_file_count - starting_file_count )) -ne "$1"; then
echo "FAILED: Start: $starting_file_count, End: $ending_file_count"
else
echo "SUCCESS"
fi

12
scanner/test_scanner_loop Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
STORAGE_DIR="$HOME/.local/share/sevenkeys/testimages/"
mkdir -p "$STORAGE_DIR"
echo "test_card_scanner_count: Beginning test"
while true; do
rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)"
filename="$STORAGE_DIR/$rng.png"
scanimage --output-file="$filename" --source "ADF Front" --mode Color --page-width 63mm --page-height 88mm 2>/dev/null
done