mirror of https://github.com/MISP/misp-modules
fix: [ocr_enrich] Fixed tesseract input format
- It looks like the `image_to_string` method now assumes RGB format and the `imdecode` method seems to give BGR format, so we convert the image array beforepull/488/head
parent
859e490173
commit
611bb6fa9e
|
@ -6,14 +6,21 @@ import pytesseract
|
||||||
|
|
||||||
misperrors = {'error': 'Error'}
|
misperrors = {'error': 'Error'}
|
||||||
mispattributes = {'input': ['attachment'],
|
mispattributes = {'input': ['attachment'],
|
||||||
'output': ['freetext', 'text']}
|
'output': ['freetext']}
|
||||||
moduleinfo = {'version': '0.1', 'author': 'Sascha Rommelfangen',
|
moduleinfo = {'version': '0.2', 'author': 'Sascha Rommelfangen',
|
||||||
'description': 'OCR decoder',
|
'description': 'OCR decoder',
|
||||||
'module-type': ['expansion']}
|
'module-type': ['expansion']}
|
||||||
|
|
||||||
moduleconfig = []
|
moduleconfig = []
|
||||||
|
|
||||||
|
|
||||||
|
def filter_decoded(decoded):
|
||||||
|
for line in decoded.split('\n'):
|
||||||
|
decoded_line = line.strip('\t\x0b\x0c\r ')
|
||||||
|
if decoded_line:
|
||||||
|
yield decoded_line
|
||||||
|
|
||||||
|
|
||||||
def handler(q=False):
|
def handler(q=False):
|
||||||
if q is False:
|
if q is False:
|
||||||
return False
|
return False
|
||||||
|
@ -31,9 +38,16 @@ def handler(q=False):
|
||||||
image = img_array
|
image = img_array
|
||||||
image = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
image = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
||||||
try:
|
try:
|
||||||
decoded = pytesseract.image_to_string(image)
|
decoded = pytesseract.image_to_string(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
||||||
return {'results': [{'types': ['freetext'], 'values': decoded, 'comment': "OCR from file " + filename},
|
return {
|
||||||
{'types': ['text'], 'values': decoded, 'comment': "ORC from file " + filename}]}
|
'results':[
|
||||||
|
{
|
||||||
|
'types': ['freetext'],
|
||||||
|
'values': list(filter_decoded(decoded)),
|
||||||
|
'comment': f"OCR from file {filename}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
err = "Couldn't analyze file type. Only images are supported right now."
|
err = "Couldn't analyze file type. Only images are supported right now."
|
||||||
|
|
Loading…
Reference in New Issue