Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
generate-motionMaskPGMs.sh - for use with RPiWebCamInterface
#!/bin/bash
# betlog - 2021-01-02--21-07-42
#
# USE:
# 1 - in the RPi_Cam_Web_Interface for your camera > camera settings > set image res to match video res > 'record image' > 'download videos and images' > download the image
# 2 - open the exported image in your eitor of choice > paint areas to be monitored for motion as white and everything else black > save the file (replace the original)
# 3a - if you have a service menu set up as per the gist:
# - right click the now black and white file > convert > 'RPiCamWebInterface - generate And Install motionMask'
# 3b - if you just want to execute from a terminal:
# - execute: "RPiCamWebInterface-generateAndInstall-motionMaskPGM.sh <filename created above>"
# 4 - wait a moment for the script to select the reachable nodes > select the target machine
#
# NOTE: right-click/service/context menu is the only check that input is an image
# NOTE: 2592x1944 - is normal/max cam image capture resolution...just BTW
#
if [[ ${#@} -gt 1 ]];then
kdialog --title "ERROR" --passivepopup "only ONE file can be used as a mask" 5
exit
fi
targetHostList="
pizerocam0
pizerocam1
pizerocam2
pizerocam3
"
# ===============================================
unset targetHostMenu
while IFS= read -r line; do
[[ -z $line ]] && continue
check=$(ping -qc 1 -W 1 ${line}) #silent
error=$?
[[ $error -eq 0 ]] && targetHostMenu+="TRUE $line " || targetHostMenu+="FALSE $line "
done<<<"$targetHostList"
while [[ -z $targetHostString ]]; do
targetHostString=$(zenity --title "SELECT HOST(S)" --text="${0##*/}" --list --checklist --column "create motionmap" --column "hostname" $targetHostMenu 2> /dev/null)
[[ $? -eq 1 ]] && exit
targetHostString=$(echo "$targetHostString"|sed 's/|/\n/') #to preserve the zenity cancel code
done
# echo "targetHostString: $targetHostString"
msg1="${0##*/}"
msg2="PROCESSING:\\n$targetHostString"
echo -ne "$msg1\\n$msg2\\n"
kdialog --title "$msg1" --passivepopup "$msg2" 5
camImage="$1"
path=${1%/*}
# echo "camImage: $camImage"
# echo "path: $path"
camImageSize=$(identify -format '%wx%h' "$camImage")
# echo "camImageSize: $camImageSize"
case $camImageSize in
768x576) size=49x36;;
1296x972) size=82x61;;
1920x1080) size=121x68;;
*)
msg1="ERROR:"
msg2="not a valid image size (768x576, 1296x972 or 1920x1080 only)"
echo "$msg1 $msg2"
kdialog --title "$msg1" --passivepopup "$msg2" 5
exit
;;
esac
while IFS= read -r targetHost;do
[[ -z $targetHost ]] && continue
echo "removing old masks from $targetHost"
ssh -q $targetHost "sudo rm /tmp/*.pgm 2> /dev/null; sudo rm /var/www/html/*.pgm 2> /dev/null" < /dev/null
# exportName="${path}/${targetHost}-motionMask${camImageSize}.pgm"
exportName="${path}/motionMask-${camImageSize}.pgm"
echo "generating: $exportName"
convert -resize $size! "$camImage" "$exportName"
echo "installing to: $targetHost"
scp -q ${exportName} $targetHost:/tmp
ssh -q $targetHost "sudo mv /tmp/*.pgm /var/www/html" < /dev/null # will be $exportName only
[[ $? -eq 0 ]] && result="FINISHED: $(ssh -q $targetHost "ls /var/www/html/*.pgm" < /dev/null)" || result="ERROR: $result"
echo "$result"
kdialog --title "${result%%: *}" --passivepopup "${result##*: }" 5
done<<<"$targetHostString"
[Desktop Entry]
Type=Service
Icon=user-desktop
X-KDE-Priority=TopLevel
X-KDE-Submenu=Convert
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/*
Actions=RPiCamWebInterfacePGM
[Desktop Action RPiCamWebInterfacePGM]
Icon=view-process-all
Name=RPiCamWebInterface - generate And Install motionMask
Exec=/home/user/documents/scripts/raspberry/RPiCamWebInterface-generateAndInstall-motionMaskPGM.sh %U
#!/bin/bash
# betlog - 2016-09-28--11-16-04
# https://wiki.natenom.com/w/Linux/KDE/Service_Menu
# https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
#
# kde4-config --path services
# kf5-config --path services
#
# plasma-desktop -v
# plasmashell --version
source="/home/user/documents/scripts/rightClickContextMenus"
# ===================================================
version="$(lsb_release -sr)"; version=${version%%.*}
if [[ $version -eq 14 ]];then
target="/home/user/.kde/share/kde4/services"
elif [[ $version -eq 18 ]];then
target="/home/user/.local/share/kservices5"
else
msg="ERROR - er what?"
echo "${msg}"
kdialog --passivepopup "${msg}" 5
exit
fi
[[ -d "$target" ]] || mkdir -p "$target"
rm --force $target/betlog*.desktop
cp --verbose --force $source/betlog*.desktop $target/
if [[ $version -eq 18 ]];then
# DO NOT USE SUDO!
kbuildsycoca5
fi
echo "${0##*/} - DONE"
kdialog --title "${0##*/}" --passivepopup "FINISHED COPYING" 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment