Faster pixel grabbing

This commit is contained in:
Kuba Orlik 2022-05-25 21:15:17 +02:00
parent 25cee9bc8b
commit ae0f6fde8d
3 changed files with 61 additions and 4 deletions

View File

@ -41,6 +41,11 @@ RUN apk add jq
RUN apk add sed RUN apk add sed
RUN apk add nodejs RUN apk add nodejs
COPY ./mozilla /root/.mozilla COPY ./mozilla /root/.mozilla
RUN echo https://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories
RUN apk update
RUN apk add wmctrl
RUN apk add git make gcc musl-dev libx11-dev
RUN git clone https://github.com/muquit/grabc && cd grabc && make && make install
COPY . /opt COPY . /opt
WORKDIR /opt WORKDIR /opt

View File

@ -36,10 +36,15 @@ annotate_header(){
convert -size $(get_size "$filename") xc:none -fill white "$overlay_filename" convert -size $(get_size "$filename") xc:none -fill white "$overlay_filename"
convert -size $(get_size "$filename") xc:none -fill transparent "$hardoverlay_filename" convert -size $(get_size "$filename") xc:none -fill transparent "$hardoverlay_filename"
START=$(date +%s.%N)
all_positions=$(python ./get-text-position.py "$cropped_filename" ) all_positions=$(python ./get-text-position.py "$cropped_filename" )
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
echo "{'ocr_took': '${DIFF}s'}"
while [ $# -gt 0 ] while [ $# -gt 0 ]
do do
START=$(date +%s.%N)
header=$1 header=$1
description=$2 description=$2
matching_line=$(echo "$all_positions" | grep "$header" | head -n 1) matching_line=$(echo "$all_positions" | grep "$header" | head -n 1)
@ -80,11 +85,18 @@ annotate_header(){
wait $second_job; wait $second_job;
shift shift
shift shift
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
echo "{'annotation_took': '${DIFF}s'}"
done done
START=$(date +%s.%N)
convert "$filename" "$overlay_filename" -compose Darken -composite "${annotated_filename}.step.png" convert "$filename" "$overlay_filename" -compose Darken -composite "${annotated_filename}.step.png"
convert "${annotated_filename}.step.png" "$hardoverlay_filename" -compose src-over -composite "$annotated_filename" convert "${annotated_filename}.step.png" "$hardoverlay_filename" -compose src-over -composite "$annotated_filename"
rm "$overlay_filename" "$annotated_filename.step.png" "$hardoverlay_filename" "$cropped_filename" "$filename" rm "$overlay_filename" "$annotated_filename.step.png" "$hardoverlay_filename" "$cropped_filename" "$filename"
echo "{'new_file': \"${BASE_URL}/$(echo "$annotated_filename" | sed 's|/opt/||')\"}" echo "{'new_file': \"${BASE_URL}/$(echo "$annotated_filename" | sed 's|/opt/||')\"}"
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
echo "{'composition_took': '${DIFF}s'}"
} }
#annotate_header "set-cookie" "identyfikator internetowy z cookie" "Cookie" "identyfikator internetowy z cookie" #annotate_header "set-cookie" "identyfikator internetowy z cookie" "Cookie" "identyfikator internetowy z cookie"

View File

@ -1,13 +1,36 @@
#!/bin/bash #!/bin/bash
get_pixel_color(){ timestamp(){
echo $((${EPOCHREALTIME//.} / 1000))
}
_get_pixel_color(){
# old version that takes up to .5s to run
START=$(timestamp)
x=$1; x=$1;
y=$2; y=$2;
output_path="/tmp/$(mktemp -u XXXXXX).png" output_path="/tmp/$(mktemp -u XXXXXX).png"
scrot $output_path scrot $output_path
magick $output_path -format "%[hex:p{$x,$y}]" info: magick $output_path -format "%[hex:p{$x,$y}]" info:
END=$(timestamp)
DIFF=$(echo "$END - $START" | bc)
echo "{'getting_pixel_color_took': '${DIFF}ms'}" > /dev/stderr
} }
get_pixel_color(){
x=$1;
y=$2;
FIREFOX_ID=$(wmctrl -lp | grep Firefox | awk '{print $1}')
if [ "$FIREFOX_ID" = "" ]
then
echo "NONE"
return
fi
grabc -w "$FIREFOX_ID" -l +${x}+${y}
}
extract_text(){ extract_text(){
output_path="/tmp/$(mktemp -u XXXXXX).png" output_path="/tmp/$(mktemp -u XXXXXX).png"
cropped_path=$output_path--cropped.png cropped_path=$output_path--cropped.png
@ -67,7 +90,12 @@ start_firefox(){
#echo 'user_pref("layout.css.devPixelsPerPx", "1.5");' >> /root/.mozilla/firefox/bifup8k5.docker/prefs.js #echo 'user_pref("layout.css.devPixelsPerPx", "1.5");' >> /root/.mozilla/firefox/bifup8k5.docker/prefs.js
firefox --devtools > /dev/null & firefox --devtools > /dev/null &
FIREFOX_PID=$! FIREFOX_PID=$!
sleep 3 while [ $(get_pixel_color 100 100) = "NONE" ]
do
echo "waiting for firefox to open the window"
sleep 0.1
done;
echo "firefox opened the window"
} }
prepare_firefox(){ prepare_firefox(){
@ -87,6 +115,18 @@ prepare_firefox(){
# sleep 5 # it needs some time # sleep 5 # it needs some time
} }
wait_for_pixel_color(){
x=$1;
y=$2;
color=$3;
timeout=$4;
while [ $(get_pixel_color 143 122) = "#2e3436" ]
do
sleep 0.1
done
}
load_website(){ load_website(){
URL=$1 URL=$1
keycombo Control_L l keycombo Control_L l
@ -98,7 +138,7 @@ load_website(){
sleep 1 sleep 1
echo "{'current_action': 'waiting for $URL to load...'}" echo "{'current_action': 'waiting for $URL to load...'}"
times=0 times=0
while [ $(get_pixel_color 143 122) = "2E3436" ] # the center of the X icon that becomes a "refresh" icon once the website is finished loading while [ $(get_pixel_color 143 122) = "#2e3436" ] # the center of the X icon that becomes a "refresh" icon once the website is finished loading
do do
sleep 0.5 sleep 0.5
#printf "." #printf "."
@ -136,7 +176,7 @@ network_inspector_search(){
} }
network_inspector_has_more_entries(){ network_inspector_has_more_entries(){
[ $(get_pixel_color 1267 1572) = "F9F9FA" ] [ $(get_pixel_color 1267 1572) = "#f9f9fa" ]
} }
network_inspector_next_entry(){ network_inspector_next_entry(){