chg: [qrcode] improve qrcode extractor + add v5.8 update

This commit is contained in:
terrtia 2024-10-02 15:07:21 +02:00
parent 0d55483419
commit a0b9b961dc
No known key found for this signature in database
GPG key ID: 1E1B1F50D84613D0
4 changed files with 107 additions and 24 deletions

View file

@ -9,10 +9,13 @@ The OcrExtractor Module
################################## ##################################
# Import External packages # Import External packages
################################## ##################################
import cv2
import os import os
import sys import sys
import cv2
from pyzbar.pyzbar import decode
from qreader import QReader
sys.path.append(os.environ['AIL_BIN']) sys.path.append(os.environ['AIL_BIN'])
################################## ##################################
# Import Project packages # 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) self.r_cache.setex(f'qrcode:no:{self.obj.type}:{self.obj.id}', 86400, 0)
def extract_qrcode(self, path): def extract_qrcode(self, path):
detector = cv2.QRCodeDetector() # TODO Move me in init ??? qr_codes = False
image = cv2.imread(path) contents = []
image = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
# multiple extraction
try: try:
qr_found, contents, qarray, _ = detector.detectAndDecodeMulti(image) decodeds = decode(image)
if qr_found: for decoded in decodeds:
return contents if decoded.data:
else: contents.append(decoded.data.decode())
# simple extraction except ValueError as e:
content, box, _ = detector.detectAndDecode(image) self.logger.error(f'{e}: {self.obj.get_global_id()}')
if content:
return [content] if not contents:
else: detector = cv2.QRCodeDetector()
return [] qr, decodeds, qarray, _ = detector.detectAndDecodeMulti(image)
except cv2.error as e: if qr:
self.logger.error(f'{e}, {self.obj.get_global_id()}') 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): def compute(self, message):
obj = self.get_obj() obj = self.get_obj()
@ -77,7 +96,7 @@ class QrCodeReader(AbstractModule):
# image - screenshot # image - screenshot
path = self.obj.get_filepath() path = self.obj.get_filepath()
contents = self.extract_qrcode(path) is_qrcode, contents = self.extract_qrcode(path)
if not contents: if not contents:
# print('no qr code detected') # print('no qr code detected')
self.add_to_cache() self.add_to_cache()
@ -95,16 +114,14 @@ class QrCodeReader(AbstractModule):
o_subtype, o_id = c_id.split(':', 1) o_subtype, o_id = c_id.split(':', 1)
qr_code.add_correlation(obj_type, o_subtype, o_id) 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 ??? # TODO only if new ???
self.add_message_to_queue(obj=qr_code, queue='Item') 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__': if __name__ == '__main__':
module = QrCodeReader() module = QrCodeReader()
module.run() module.run()
# from lib.objects.Images import Image
# module.obj = Image('8a690f4d09509dbfe52a6fb139db500b16b3d5f07e22617944752c4d4885737c')
# module.compute(None)

View file

@ -87,6 +87,10 @@ ail_typo_squatting
# OCR # OCR
easyocr easyocr
# Qrcode
qreader
pyzbar
# Tests # Tests
nose2>=0.12.0 nose2>=0.12.0
coverage>=5.5 coverage>=5.5

22
update/v5.8/Update.py Executable file
View file

@ -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()

40
update/v5.8/Update.sh Executable file
View file

@ -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