2022-04-20 23:23:48 +02:00
#!/bin/bash
get_size( ) {
filename = $1
magick " $filename " -format "%[w]x%[h]" info:
}
get_width( ) {
filename = $1
magick " $filename " -format "%[w]" info:
}
annotate_header( ) {
2022-04-21 22:20:44 +02:00
filename = $1
shift;
2022-05-05 21:54:34 +02:00
domain = $1 ;
shift;
count = $1 ;
shift;
2022-04-20 23:23:48 +02:00
d = $( date "+%Y-%m-%d__%H_%M_%S" )
#filename="2022-03-10__19_33_55__set-cookie.png"
cropped_filename = " ${ filename } __cropped.png "
overlay_filename = " ${ cropped_filename } __overlay.png "
hardoverlay_filename = " ${ cropped_filename } __hardoverlay.png "
2022-05-05 21:54:34 +02:00
annotated_filename = " $( dirname " $filename " ) / ${ domain } __ ${ count } .final.png " # the name is crucial, because the web app part filters files based on the name
2022-04-20 23:23:48 +02:00
# crop
left = 2056
top = 330
width = 824
height = 1260
vips extract_area " $filename " " $cropped_filename " $left $top $width $height
# prepare overlay canvases
convert -size $( get_size " $filename " ) xc:none -fill white " $overlay_filename "
convert -size $( get_size " $filename " ) xc:none -fill transparent " $hardoverlay_filename "
2022-05-25 21:15:17 +02:00
START = $( date +%s.%N)
2022-04-20 23:23:48 +02:00
all_positions = $( python ./get-text-position.py " $cropped_filename " )
2022-05-25 21:15:17 +02:00
END = $( date +%s.%N)
DIFF = $( echo " $END - $START " | bc)
2022-05-27 15:49:56 +02:00
echo " {\"ocr_took\": \" ${ DIFF } s\"} "
2022-04-20 23:23:48 +02:00
while [ $# -gt 0 ]
do
2022-05-25 21:15:17 +02:00
START = $( date +%s.%N)
2022-04-20 23:23:48 +02:00
header = $1
description = $2
matching_line = $( echo " $all_positions " | grep " $header " | head -n 1)
if [ " $matching_line " = "" ] ; then
shift;
shift;
continue
fi ;
coords = $( echo $matching_line | awk '{print $1 " " $2 " " $3 " " $4}' )
highlight_margin = 5
rect_x1 = $(( $( echo " $coords " | awk '{print $1}' ) - highlight_margin + left))
rect_y1 = $(( $( echo " $coords " | awk '{print $2}' ) - highlight_margin + top))
rect_h = $(( $( echo " $coords " | awk '{print $4}' ) + 2 * highlight_margin))
rect_x2 = $(( 796 + left))
rect_y2 = $(( rect_y1 + rect_h))
convert " $overlay_filename " \
-fill "#ff000066" \
-stroke "#ff000066" \
-draw " rectangle $rect_x1 , $rect_y1 $rect_x2 , $rect_y2 " \
" $overlay_filename " &
first_job = $!
triangle_height = 30
triangle_width = 30
convert " $hardoverlay_filename " \
-fill "red" \
-draw " path 'M $(( rect_x1 - 10 )) , $(( rect_y1 + rect_h / 2 )) L $(( rect_x1 - triangle_width - 10 )) , $(( rect_y1 - triangle_height + rect_h / 2 )) L $(( rect_x1 - triangle_width - 10 )) , $(( rect_y1 + triangle_height + rect_h / 2 )) Z' " \
-draw " rectangle $(( rect_x1-20)) , $rect_y1 $(( rect_x1 - 200 )) , $rect_y2 " \
-gravity NorthEast \
-pointsize 50 \
-undercolor White \
-font "Noto-Sans-Regular" \
-draw " text 985, $(( rect_y1 - 30 )) ' $description ' " \
" $hardoverlay_filename " &
second_job = $!
wait $first_job ;
wait $second_job ;
shift
shift
2022-05-25 21:15:17 +02:00
END = $( date +%s.%N)
DIFF = $( echo " $END - $START " | bc)
2022-05-27 15:49:56 +02:00
echo " {\"annotation_took\": \" ${ DIFF } s\"} "
2022-04-20 23:23:48 +02:00
done
2022-05-25 21:15:17 +02:00
START = $( date +%s.%N)
2022-05-05 21:54:34 +02:00
convert " $filename " " $overlay_filename " -compose Darken -composite " ${ annotated_filename } .step.png "
convert " ${ annotated_filename } .step.png " " $hardoverlay_filename " -compose src-over -composite " $annotated_filename "
2022-04-20 23:23:48 +02:00
rm " $overlay_filename " " $annotated_filename .step.png " " $hardoverlay_filename " " $cropped_filename " " $filename "
2022-05-27 15:49:56 +02:00
echo " {\"new_file\": \" ${ BASE_URL } / $( echo " $annotated_filename " | sed 's|/opt/||' ) \"} "
2022-05-25 21:15:17 +02:00
END = $( date +%s.%N)
DIFF = $( echo " $END - $START " | bc)
2022-05-27 15:49:56 +02:00
echo " {\"composition_took\": \" ${ DIFF } s\"} "
2022-04-20 23:23:48 +02:00
}
#annotate_header "set-cookie" "identyfikator internetowy z cookie" "Cookie" "identyfikator internetowy z cookie"