From a0b9b961dcf2093e3b5b318a7ac82b602faaadc0 Mon Sep 17 00:00:00 2001 From: terrtia Date: Wed, 2 Oct 2024 15:07:21 +0200 Subject: [PATCH] chg: [qrcode] improve qrcode extractor + add v5.8 update --- bin/modules/QrCodeReader.py | 65 +++++++++++++++++++++++-------------- requirements.txt | 4 +++ update/v5.8/Update.py | 22 +++++++++++++ update/v5.8/Update.sh | 40 +++++++++++++++++++++++ 4 files changed, 107 insertions(+), 24 deletions(-) create mode 100755 update/v5.8/Update.py create mode 100755 update/v5.8/Update.sh diff --git a/bin/modules/QrCodeReader.py b/bin/modules/QrCodeReader.py index 6c723add..bc7222d9 100755 --- a/bin/modules/QrCodeReader.py +++ b/bin/modules/QrCodeReader.py @@ -9,10 +9,13 @@ The OcrExtractor Module ################################## # Import External packages ################################## -import cv2 import os import sys +import cv2 +from pyzbar.pyzbar import decode +from qreader import QReader + sys.path.append(os.environ['AIL_BIN']) ################################## # Import Project packages @@ -46,23 +49,39 @@ class QrCodeReader(AbstractModule): self.r_cache.setex(f'qrcode:no:{self.obj.type}:{self.obj.id}', 86400, 0) def extract_qrcode(self, path): - detector = cv2.QRCodeDetector() # TODO Move me in init ??? - image = cv2.imread(path) - - # multiple extraction + qr_codes = False + contents = [] + image = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB) try: - qr_found, contents, qarray, _ = detector.detectAndDecodeMulti(image) - if qr_found: - return contents - else: - # simple extraction - content, box, _ = detector.detectAndDecode(image) - if content: - return [content] - else: - return [] - except cv2.error as e: - self.logger.error(f'{e}, {self.obj.get_global_id()}') + decodeds = decode(image) + for decoded in decodeds: + if decoded.data: + contents.append(decoded.data.decode()) + except ValueError as e: + self.logger.error(f'{e}: {self.obj.get_global_id()}') + + if not contents: + detector = cv2.QRCodeDetector() + qr, decodeds, qarray, _ = detector.detectAndDecodeMulti(image) + if qr: + qr_codes = True + for d in decodeds: + if d: + contents.append(d) + data_qr, box, qrcode_image = detector.detectAndDecode(image) + if data_qr: + contents.append(data_qr) + + if qr_codes and not contents: + # # # # 0.5s per image + try: + qreader = QReader() + decoded_text = qreader.detect_and_decode(image=image) + for d in decoded_text: + contents.append(d) + except ValueError as e: + self.logger.error(f'{e}: {self.obj.get_global_id()}') + return qr_codes, contents def compute(self, message): obj = self.get_obj() @@ -77,7 +96,7 @@ class QrCodeReader(AbstractModule): # image - screenshot path = self.obj.get_filepath() - contents = self.extract_qrcode(path) + is_qrcode, contents = self.extract_qrcode(path) if not contents: # print('no qr code detected') self.add_to_cache() @@ -95,16 +114,14 @@ class QrCodeReader(AbstractModule): o_subtype, o_id = c_id.split(':', 1) qr_code.add_correlation(obj_type, o_subtype, o_id) - tag = 'infoleak:automatic-detection="qrcode"' - self.add_message_to_queue(obj=self.obj, message=tag, queue='Tags') - # TODO only if new ??? self.add_message_to_queue(obj=qr_code, queue='Item') + if is_qrcode: + tag = 'infoleak:automatic-detection="qrcode"' + self.add_message_to_queue(obj=self.obj, message=tag, queue='Tags') + if __name__ == '__main__': module = QrCodeReader() module.run() - # from lib.objects.Images import Image - # module.obj = Image('8a690f4d09509dbfe52a6fb139db500b16b3d5f07e22617944752c4d4885737c') - # module.compute(None) diff --git a/requirements.txt b/requirements.txt index e95dbf28..831582b0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -87,6 +87,10 @@ ail_typo_squatting # OCR easyocr +# Qrcode +qreader +pyzbar + # Tests nose2>=0.12.0 coverage>=5.5 diff --git a/update/v5.8/Update.py b/update/v5.8/Update.py new file mode 100755 index 00000000..60f7fb0b --- /dev/null +++ b/update/v5.8/Update.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +# -*-coding:UTF-8 -* + +import os +import sys + +sys.path.append(os.environ['AIL_HOME']) +################################## +# Import Project packages +################################## +from update.bin.ail_updater import AIL_Updater + +class Updater(AIL_Updater): + """default Updater.""" + + def __init__(self, version): + super(Updater, self).__init__(version) + + +if __name__ == '__main__': + updater = Updater('v5.8') + updater.run_update() diff --git a/update/v5.8/Update.sh b/update/v5.8/Update.sh new file mode 100755 index 00000000..d0c2547b --- /dev/null +++ b/update/v5.8/Update.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +[ -z "$AIL_HOME" ] && echo "Needs the env var AIL_HOME. Run the script from the virtual environment." && exit 1; +[ -z "$AIL_REDIS" ] && echo "Needs the env var AIL_REDIS. Run the script from the virtual environment." && exit 1; +[ -z "$AIL_BIN" ] && echo "Needs the env var AIL_ARDB. Run the script from the virtual environment." && exit 1; +[ -z "$AIL_FLASK" ] && echo "Needs the env var AIL_FLASK. Run the script from the virtual environment." && exit 1; + +export PATH=$AIL_HOME:$PATH +export PATH=$AIL_REDIS:$PATH +export PATH=$AIL_BIN:$PATH +export PATH=$AIL_FLASK:$PATH + +GREEN="\\033[1;32m" +DEFAULT="\\033[0;39m" + +echo -e $GREEN"Shutting down AIL ..."$DEFAULT +bash ${AIL_BIN}/LAUNCH.sh -ks +wait + +# SUBMODULES # +git submodule update + +bash ${AIL_BIN}/LAUNCH.sh -lrv +bash ${AIL_BIN}/LAUNCH.sh -lkv + +echo "" +echo -e $GREEN"Updating python packages ..."$DEFAULT +echo "" +pip install -U pyzbar +pip install -U qreader + +echo "" +echo -e $GREEN"Updating AIL VERSION ..."$DEFAULT +echo "" +python ${AIL_HOME}/update/v5.8/Update.py +wait +echo "" +echo "" + +exit 0