From ae0f6fde8da2a73f510d15593355d3e5bef05e3e Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Wed, 25 May 2022 21:15:17 +0200 Subject: [PATCH] Faster pixel grabbing --- Docker/Dockerfile | 5 ++++ Docker/annotate_header.sh | 12 ++++++++++ Docker/utils.sh | 48 +++++++++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/Docker/Dockerfile b/Docker/Dockerfile index 0da69be..aa53ae1 100644 --- a/Docker/Dockerfile +++ b/Docker/Dockerfile @@ -41,6 +41,11 @@ RUN apk add jq RUN apk add sed RUN apk add nodejs 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 WORKDIR /opt diff --git a/Docker/annotate_header.sh b/Docker/annotate_header.sh index e6f9b4e..5097a24 100755 --- a/Docker/annotate_header.sh +++ b/Docker/annotate_header.sh @@ -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 transparent "$hardoverlay_filename" + START=$(date +%s.%N) 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 ] do + START=$(date +%s.%N) header=$1 description=$2 matching_line=$(echo "$all_positions" | grep "$header" | head -n 1) @@ -80,11 +85,18 @@ annotate_header(){ wait $second_job; shift shift + END=$(date +%s.%N) + DIFF=$(echo "$END - $START" | bc) + echo "{'annotation_took': '${DIFF}s'}" done + START=$(date +%s.%N) convert "$filename" "$overlay_filename" -compose Darken -composite "${annotated_filename}.step.png" 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" 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" diff --git a/Docker/utils.sh b/Docker/utils.sh index bdd884a..b002776 100644 --- a/Docker/utils.sh +++ b/Docker/utils.sh @@ -1,13 +1,36 @@ #!/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; y=$2; output_path="/tmp/$(mktemp -u XXXXXX).png" scrot $output_path 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(){ output_path="/tmp/$(mktemp -u XXXXXX).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 firefox --devtools > /dev/null & 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(){ @@ -87,6 +115,18 @@ prepare_firefox(){ # 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(){ URL=$1 keycombo Control_L l @@ -98,7 +138,7 @@ load_website(){ sleep 1 echo "{'current_action': 'waiting for $URL to load...'}" 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 sleep 0.5 #printf "." @@ -136,7 +176,7 @@ network_inspector_search(){ } network_inspector_has_more_entries(){ - [ $(get_pixel_color 1267 1572) = "F9F9FA" ] + [ $(get_pixel_color 1267 1572) = "#f9f9fa" ] } network_inspector_next_entry(){