Skip to content

Commit

Permalink
chg: [qrcode] improve qrcode extractor + add v5.8 update
Browse files Browse the repository at this point in the history
  • Loading branch information
Terrtia committed Oct 2, 2024
1 parent 0d55483 commit a0b9b96
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 24 deletions.
65 changes: 41 additions & 24 deletions bin/modules/QrCodeReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
##################################
# 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
Expand Down Expand Up @@ -46,23 +49,39 @@ def add_to_cache(self):
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()
Expand All @@ -77,7 +96,7 @@ def compute(self, message):

# 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()
Expand All @@ -95,16 +114,14 @@ def compute(self, message):
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)
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ ail_typo_squatting
# OCR
easyocr

# Qrcode
qreader
pyzbar

# Tests
nose2>=0.12.0
coverage>=5.5
Expand Down
22 changes: 22 additions & 0 deletions update/v5.8/Update.py
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions update/v5.8/Update.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a0b9b96

Please sign in to comment.