videoFlow.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. #
  3. # this is a wrapper for creating all the required files for calculating flow
  4. # and visualizes flow as video at the end
  5. #
  6. usage()
  7. {
  8. echo -e "\t Usage of the script"
  9. echo -e "\t "
  10. echo -e "\t videoFlow.sh"
  11. echo -e "\t Options:"
  12. echo -e "\t "
  13. echo -e "\t -flowname name of the output video"
  14. echo -e "\t -dir directory to save all required files"
  15. echo -e "\t freearg: path to input video for flow calculation"
  16. }
  17. COUNTER=0
  18. DIR=`pwd`
  19. DIR=$DIR
  20. FREEARGCOUNTER=0
  21. while [ "$1" != "" ]; do
  22. COUNTER=$((COUNTER+1))
  23. PARAM=`echo "$1" | awk -F= '{print $1}'`
  24. case $PARAM in
  25. -flowname)
  26. shift
  27. FLOWNAME=`echo "$1" | awk -F= '{print $1}'`
  28. ;;
  29. -dir)
  30. shift
  31. DIR=`echo "$1" | awk -F= '{print $1}'`
  32. ;;
  33. -h | --help)
  34. usage
  35. exit 0
  36. ;;
  37. *)
  38. if [ "$FREEARGCOUNTER" -eq "0" ]; then
  39. FREEARG=`echo "$1" | awk -F= '{print $1}'`
  40. let FREEARGCOUNTER=FREEARGCOUTNER+1
  41. else
  42. echo "ERROR: unknown parameter \"$PARAM\""
  43. echo "ERROR: Only one argument allowed"
  44. usage
  45. exit 1
  46. fi
  47. ;;
  48. esac
  49. shift
  50. done
  51. if [ "$COUTNER" -eq "0" ]; then
  52. usage
  53. exit 1
  54. fi
  55. if [[ -d $DIR ]]; then
  56. echo "WARNING: Directory already exists"
  57. echo "Might get confusing results"
  58. else
  59. mkdir $DIR
  60. fi
  61. if [ ${DIR:$((${#DIR}-1)):1} == '/' ]; then
  62. DIR=${DIR:0:$((${#DIR}-1))}
  63. fi
  64. # get frame rate
  65. FRAMERATE=`ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 $FREEARG | head -n 1`
  66. # resize before separating frames and separate frames
  67. ffmpeg -y -i $FREEARG -vf scale=1280x720 rescaled.avi
  68. mkdir ${DIR}/frames; ffmpeg -i rescaled.avi -r $FRAMERATE $DIR/frames/%05d_frame.png
  69. mkdir ${DIR}/edges
  70. regex="${DIR}/frames/*.png"
  71. matlab -nodesktop -nojvm -r "RunEdgeDetection('$regex'); exit"
  72. # create matches files
  73. mkdir ${DIR}/matches; files=($(ls -1 ${DIR}/frames/)); let end=${#files[@]}-2; for i in `seq 0 $end`; do out=`basename ${files[i]} .png`; out=${out}_match; echo ${files[i]}; let j=i+1; deepmatching_1.2.2_c++/deepmatching ${DIR}/frames/${files[i]} ${DIR}/frames/${files[j]} -R 2 -nt 4 -png_settings -out ${DIR}/matches/${out}; done
  74. # create flow files
  75. mkdir ${DIR}/flow; files=($(ls -1 ${DIR}/matches/)); let end=${#files[@]}-2
  76. for i in `seq 0 $end`; do echo ${files[i]}; id1=`echo ${files[i]} | cut -c1-5`; let j=i+1; id2=`echo ${files[j]} | cut -c1-5`; EpicFlow_v1.00/epicflow ${DIR}/frames/${id1}_frame.png ${DIR}/frames/${id2}_frame.png ${DIR}/edges/${id1}_frame_edges ${DIR}/matches/${id1}_frame_match ${DIR}/flow/${id1}_frame_flow.flo; done
  77. # chek if some flow files were not created. If so copy the previous frame flow to the current one
  78. # check 1st frame
  79. first_file="${DIR}flow/00001_frame_flow.flo"
  80. all_files=(`ls ${DIR}flow/*_frame_flow.flo`)
  81. if [ ! -f $first_file ]; then cp ${all_files[0]} $first_file; fi
  82. # check the rest
  83. for i in `seq 1 $end`; do id_bef=`echo ${files[i-1]} | cut -c1-5`; flow_file_bef="${DIR}flow/${id_bef}_frame_flow.flo"; id_cur=`echo ${files[i]} | cut -c1-5`;flow_file="${DIR}flow/${id_cur}_frame_flow.flo"; if [ ! -f $flow_file ]; then echo "Flow file does not exist $flow_file. Copying previous frame's flow file"; cp $flow_file_bef $flow_file; fi; done
  84. # visualize flows
  85. # black-white
  86. for i in `ls ${DIR}/flow/*.flo`; do j=`echo $i | sed 's/\.flo//g'`; echo $j; flow-code/color_flow -bw $i ${j}.png 3; done
  87. ffmpeg -framerate 30 -i ${DIR}/flow/%05d_frame_flow.png -c:v libx264 -r 30 -pix_fmt yuv420p ${DIR}/${FLOWNAME}"_bw.mp4"
  88. # colored
  89. for i in `ls ${DIR}/flow/*.flo`; do j=`echo $i | sed 's/\.flo//g'`; echo $j; flow-code/color_flow $i ${j}.png 3; done
  90. ffmpeg -framerate 30 -i ${DIR}/flow/%05d_frame_flow.png -c:v libx264 -r 30 -pix_fmt yuv420p ${DIR}/${FLOWNAME}".mp4"
  91. echo $FLOWNAME >> completed.txt
  92. # clean up created files
  93. #rm -r ${DIR}/frames ${DIR}/edges ${DIR}/matches ${DIR}/flow