From ae9ef044f6ffe2a88f46192b2ca75e5edc5a9fe1 Mon Sep 17 00:00:00 2001 From: ruzko <75946143+ruzko@users.noreply.github.com> Date: Mon, 23 May 2022 13:17:40 +0200 Subject: [PATCH 1/4] enable parallell processing This significantly speeds up post-processing. Some logic is provided to determine which Pi model the script is running on, and number of parallell processes are limited accordingly --- tools/raw2ogg2anim | 84 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/tools/raw2ogg2anim b/tools/raw2ogg2anim index 011efac..70fc731 100755 --- a/tools/raw2ogg2anim +++ b/tools/raw2ogg2anim @@ -2,57 +2,99 @@ # needs: dcraw, double, netpbm tools, gstreamer, gifenc.sh + +#Determining the model of Raspberry Pi we're running on +if [ "`lscpu | grep 'Model' | grep -o 'A72'`" = "A72" ]; then +num_processes=40 +echo "Model 4, running 40 parallell processes" + +elif [ "`lscpu | grep 'Model' | grep -o 'A53'`" = "A53" ]; then +num_processes=20 +echo "Model 3, running 20 parallell processes" + + +else +num_processes=10 +echo "couldn't determine model, running 10 parallell processes" +fi + + +#converts raw frames to png format +dcraw_conversion() { + dcraw $f + echo -en "$f \r" +} + +#stretches lines so that videos recorded with line skips look better +line_doubling() { + if [ "$5" = "" ]; then + ln -s $f $f.d + else + if [ "$5" = "d" ]; then + double $f > $f.d + else + double $f > $f.D + double $f.D > $f.d + fi + fi + echo -en "$f \r" +} + +#converts from intermediary format ppm to final image format png +ppmtopng_conversion() { + pnmtopng $f > $f.png + echo -en "$f \r" +} + + +#Beginning of legacy script if [ "$4" = "" ]; then echo "format: `basename $0` vname first last fps [d[d]]"; exit; fi echo "removing old auxiliary files" rm -f out.*.raw out.*.ppm out.*.ppm.[dDT] out.*.ppm.d.png + +# this appears to be slower when parallellized, so it's a single process for the time being. echo "copying /dev/shm/out.????.raw files" for((f=$2; f<=$3; ++f)) do - # cp /dev/shm/out.$(printf "%04d" $f).raw . - cat hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw +cat /dev/shm/hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw echo -en "$f \r" done echo + echo "dcraw each .raw file (to .ppm)" for f in out.*.raw do - dcraw $f - echo -en "$f \r" + ((i=i%num_processes)); ((i++==0)) && wait + dcraw_conversion & done -echo +wait echo ".ppm -> .ppm.d" for f in out.*.ppm do - if [ "$5" = "" ]; then - ln -s $f $f.d - else - if [ "$5" = "d" ]; then - double $f > $f.d - else - double $f > $f.D - double $f.D > $f.d - fi - fi - echo -en "$f \r" + ((i=i%num_processes)); ((i++==0)) && wait + line_doubling & done -echo +wait echo ".ppm.d -> .ppm.d.png" for f in out.*.ppm.d do - pnmtopng $f > $f.png - echo -en "$f \r" + ((i=i%num_processes)); ((i++==0)) && wait + ppmtopng_conversion & done -echo +wait echo "now creating $1.ogg" gst-launch-1.0 multifilesrc location="out.%04d.ppm.d.png" index=$2 caps="image/png,framerate=\(fraction\)$4/1" ! pngdec ! videorate ! videoconvert ! videorate ! theoraenc ! oggmux ! filesink location="$1.ogg" echo "now creating $1.anim.gif" -gifenc.sh $1.ogg $1.anim.gif +gifenc.sh $1.ogg $1.gif + +echo "removing new auxiliary files" +rm -f out.* echo "done" From 2c11a7b64d2f9536d91668ecb918641e9d418c73 Mon Sep 17 00:00:00 2001 From: ruzko <75946143+ruzko@users.noreply.github.com> Date: Mon, 30 May 2022 14:48:41 +0200 Subject: [PATCH 2/4] add options option one appends current time to filename. This lets a user use the same command repeatedly without overwriting previous gif files. Option two lets the user keep new auxiliary files if they want to. --- tools/raw2ogg2anim | 76 +++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/tools/raw2ogg2anim b/tools/raw2ogg2anim index 70fc731..19f11b8 100755 --- a/tools/raw2ogg2anim +++ b/tools/raw2ogg2anim @@ -3,29 +3,30 @@ # needs: dcraw, double, netpbm tools, gstreamer, gifenc.sh -#Determining the model of Raspberry Pi we're running on +#Determining the model of Raspberry Pi we're running on, to set an appropriate number of parallell processes if [ "`lscpu | grep 'Model' | grep -o 'A72'`" = "A72" ]; then -num_processes=40 -echo "Model 4, running 40 parallell processes" - -elif [ "`lscpu | grep 'Model' | grep -o 'A53'`" = "A53" ]; then -num_processes=20 -echo "Model 3, running 20 parallell processes" - - -else -num_processes=10 -echo "couldn't determine model, running 10 parallell processes" + num_processes=40 + echo "Model 4, running 40 parallell processes" +else + if [ "`lscpu | grep 'Model' | grep -o 'A53'`" = "A53" ]; then + num_processes=20 + echo "Model 3, running 20 parallell processes" + else + num_processes=10 + echo "couldn't determine model, running 10 parallell processes" + fi fi -#converts raw frames to png format +#The following three code blocks define functions to be called later +#1. converts raw frames to png format dcraw_conversion() { dcraw $f echo -en "$f \r" } -#stretches lines so that videos recorded with line skips look better + +#2. stretches lines so that videos recorded with line skips look better line_doubling() { if [ "$5" = "" ]; then ln -s $f $f.d @@ -40,28 +41,42 @@ line_doubling() { echo -en "$f \r" } -#converts from intermediary format ppm to final image format png + +#3. converts from intermediary format ppm to final image format png ppmtopng_conversion() { pnmtopng $f > $f.png echo -en "$f \r" } -#Beginning of legacy script -if [ "$4" = "" ]; then echo "format: `basename $0` vname first last fps [d[d]]"; exit; fi +# Taking user inputs +if [ "$4" = "" ]; then echo "format: `basename $0` vname first last fps [-t] [-k] [d[d]]"; exit; fi + + +# If the user inputs "-t" as parameter 5, then we append the current date and time to the filename +if [ "$5" = "-t" ]; then + echo "appending date and time to filename" + momentOfCapture=`date +%m.%d_T%H:%M:%S` +fi + + +# if the user adds the command line option -c or --count, ls | grep $1 is run in cwd, +# and the new filename is incremented by one in relation to the highest numbered similar filename. + +#ls | grep $1 + echo "removing old auxiliary files" rm -f out.*.raw out.*.ppm out.*.ppm.[dDT] out.*.ppm.d.png -# this appears to be slower when parallellized, so it's a single process for the time being. +# this command appears to be slower when parallellized, so it's a single process for the time being. echo "copying /dev/shm/out.????.raw files" for((f=$2; f<=$3; ++f)) do -cat /dev/shm/hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw + cat /dev/shm/hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw echo -en "$f \r" done -echo echo "dcraw each .raw file (to .ppm)" @@ -72,6 +87,7 @@ do done wait + echo ".ppm -> .ppm.d" for f in out.*.ppm do @@ -80,6 +96,7 @@ do done wait + echo ".ppm.d -> .ppm.d.png" for f in out.*.ppm.d do @@ -88,13 +105,24 @@ do done wait + echo "now creating $1.ogg" gst-launch-1.0 multifilesrc location="out.%04d.ppm.d.png" index=$2 caps="image/png,framerate=\(fraction\)$4/1" ! pngdec ! videorate ! videoconvert ! videorate ! theoraenc ! oggmux ! filesink location="$1.ogg" -echo "now creating $1.anim.gif" -gifenc.sh $1.ogg $1.gif -echo "removing new auxiliary files" -rm -f out.* +if [ "$5" = "-T" ]; then + echo "now creating $1-$momentOfCapture.gif" + gifenc.sh $1.ogg $1-$momentOfCapture.gif +else + echo "now creating $1.gif" + gifenc.sh $1.ogg $1.gif +fi + + +#comment the following two line if you want to keep individual image frames and other files in the current directory +[if "$6" != "-k" ]; then + echo "removing new auxiliary files" + rm -f out.* +fi echo "done" From 31cd273273dae817929513ebbaa7d08d78543a91 Mon Sep 17 00:00:00 2001 From: ruzko <75946143+ruzko@users.noreply.github.com> Date: Fri, 10 Jun 2022 15:17:21 +0200 Subject: [PATCH 3/4] Create recording_trigger.sh Add script to run arbitrary command or read command from preset file when a gpio pin is read as HIGH. Uses BCM numbering scheme. --- tools/640x128_s_600fps | 18 +++++++++++++ tools/preset_rec_cmd.txt | 1 + tools/recording_trigger.sh | 55 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100755 tools/640x128_s_600fps create mode 100644 tools/preset_rec_cmd.txt create mode 100755 tools/recording_trigger.sh diff --git a/tools/640x128_s_600fps b/tools/640x128_s_600fps new file mode 100755 index 0000000..a1c39b3 --- /dev/null +++ b/tools/640x128_s_600fps @@ -0,0 +1,18 @@ +#!/bin/bash + +if [ "$1" = "" ]; then echo "format: `basename $0` [recording time in milliseconds]"; exit; fi + +echo "removing /dev/shm/out.*.raw" +rm -rf /dev/shm/* + +echo "creating files for timestamp and headers" +touch /dev/shm/tstamps.csv +touch /dev/shm/hd0.32k + +echo "capturing frames for ${1}ms with 600fps requested" +raspiraw -md 7 -t $1 --fps 600 -y 10 --height 128 --top 0 --vinc 1F -ts /dev/shm/tstamps.csv -hd0 /dev/shm/hd0.32k -eus 1400 -sr 1 --regs "0157,d0;0158,04" -o /dev/shm/out.%04d.raw 2>/dev/null >/dev/null + +echo "frame delta time[us] distribution" +cut -f1 -d, /dev/shm/tstamps.csv | sort -n | uniq -c + + diff --git a/tools/preset_rec_cmd.txt b/tools/preset_rec_cmd.txt new file mode 100644 index 0000000..06967c1 --- /dev/null +++ b/tools/preset_rec_cmd.txt @@ -0,0 +1 @@ +640x128_s_600fps 4000 diff --git a/tools/recording_trigger.sh b/tools/recording_trigger.sh new file mode 100755 index 0000000..3184318 --- /dev/null +++ b/tools/recording_trigger.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# This script reads the state of a hardcoded pin every set interval, and if the pin is read 'HIGH', +# a user-defined recording is launched. After the command is launched, the script exits. + + +# First the user lets us know whether to launch a preset or custom command +echo 'Please enter the recording command you wish to launch:' +echo 'If you instead want to read a preset command from a file, press key f and enter' +rec_cmd= +while [[ $rec_cmd = "" ]]; do + read rec_cmd +done + + +# Read command from a file if rec_cmd == f, otherwise store input as command +if [[ $rec_cmd = "f" ]]; then + rec_cmd=$( /sys/class/gpio/export +fi +echo "pin $input_pin is set as input pin" + + +#reading the input pin continuously, every interval +declare -i i +while [ 1 ]; do + status=$(< /sys/class/gpio/gpio$input_pin/value) + #echo $status + if [[ $status = "1" ]]; then + i+=1 + fi + sleep 0.1 #seconds + if [[ $status != "1" && i>="1" ]]; then + i=0 + fi + if [[ $i = "5" ]]; then + break + fi +done + + +#run commmand +$rec_cmd From 0bcfb9b2ac09d4681a83e5fb546cd17a60e60ada Mon Sep 17 00:00:00 2001 From: ruzko <75946143+ruzko@users.noreply.github.com> Date: Fri, 10 Jun 2022 15:23:24 +0200 Subject: [PATCH 4/4] removed unnecessary application specific files --- tools/640x128_s_600fps | 18 ------------------ tools/preset_rec_cmd.txt | 2 +- 2 files changed, 1 insertion(+), 19 deletions(-) delete mode 100755 tools/640x128_s_600fps diff --git a/tools/640x128_s_600fps b/tools/640x128_s_600fps deleted file mode 100755 index a1c39b3..0000000 --- a/tools/640x128_s_600fps +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -if [ "$1" = "" ]; then echo "format: `basename $0` [recording time in milliseconds]"; exit; fi - -echo "removing /dev/shm/out.*.raw" -rm -rf /dev/shm/* - -echo "creating files for timestamp and headers" -touch /dev/shm/tstamps.csv -touch /dev/shm/hd0.32k - -echo "capturing frames for ${1}ms with 600fps requested" -raspiraw -md 7 -t $1 --fps 600 -y 10 --height 128 --top 0 --vinc 1F -ts /dev/shm/tstamps.csv -hd0 /dev/shm/hd0.32k -eus 1400 -sr 1 --regs "0157,d0;0158,04" -o /dev/shm/out.%04d.raw 2>/dev/null >/dev/null - -echo "frame delta time[us] distribution" -cut -f1 -d, /dev/shm/tstamps.csv | sort -n | uniq -c - - diff --git a/tools/preset_rec_cmd.txt b/tools/preset_rec_cmd.txt index 06967c1..cdb5d53 100644 --- a/tools/preset_rec_cmd.txt +++ b/tools/preset_rec_cmd.txt @@ -1 +1 @@ -640x128_s_600fps 4000 +640x128_s 4000