2024-04-08 17:16:07 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The OcrExtractor Module
|
|
|
|
======================
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
2024-04-24 14:43:11 +02:00
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2024-04-10 16:43:54 +02:00
|
|
|
from lib import chats_viewer
|
|
|
|
from lib.objects import Messages
|
2024-04-08 17:16:07 +02:00
|
|
|
from lib.objects import Ocrs
|
|
|
|
|
2024-04-26 10:31:31 +02:00
|
|
|
|
2024-04-10 16:43:54 +02:00
|
|
|
# Default to eng
|
2024-04-26 10:31:31 +02:00
|
|
|
def get_model_languages(obj, ocr_languages, add_en=True):
|
2024-04-10 16:43:54 +02:00
|
|
|
if add_en:
|
|
|
|
model_languages = {'en'}
|
|
|
|
else:
|
|
|
|
model_languages = set()
|
|
|
|
|
|
|
|
ob = obj.get_first_correlation('message')
|
|
|
|
if ob:
|
|
|
|
message = Messages.Message(ob.split(':', 2)[-1])
|
|
|
|
lang = message.get_language()
|
|
|
|
if lang:
|
|
|
|
model_languages.add(lang)
|
|
|
|
return model_languages
|
|
|
|
|
|
|
|
ob = obj.get_first_correlation('chat-subchannel')
|
|
|
|
if ob:
|
|
|
|
ob = chats_viewer.get_obj_chat_from_global_id(ob)
|
|
|
|
lang = ob.get_main_language()
|
|
|
|
if lang:
|
|
|
|
model_languages.add(lang)
|
|
|
|
return model_languages
|
|
|
|
|
|
|
|
ob = obj.get_first_correlation('chat')
|
|
|
|
if ob:
|
|
|
|
ob = chats_viewer.get_obj_chat_from_global_id(ob)
|
|
|
|
lang = ob.get_main_language()
|
|
|
|
if lang:
|
|
|
|
model_languages.add(lang)
|
|
|
|
return model_languages
|
|
|
|
|
2024-04-26 10:31:31 +02:00
|
|
|
model_languages = Ocrs.sanityze_ocr_languages(model_languages, ocr_languages=ocr_languages)
|
|
|
|
|
2024-04-10 16:43:54 +02:00
|
|
|
return model_languages
|
|
|
|
|
|
|
|
# TODO thread
|
|
|
|
|
2024-04-08 17:16:07 +02:00
|
|
|
|
|
|
|
class OcrExtractor(AbstractModule):
|
|
|
|
"""
|
|
|
|
OcrExtractor for AIL framework
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(OcrExtractor, self).__init__()
|
|
|
|
|
|
|
|
# Waiting time in seconds between to message processed
|
|
|
|
self.pending_seconds = 1
|
|
|
|
|
2024-04-24 14:43:11 +02:00
|
|
|
config_loader = ConfigLoader()
|
|
|
|
self.r_cache = config_loader.get_redis_conn("Redis_Cache")
|
|
|
|
|
2024-04-26 10:31:31 +02:00
|
|
|
self.ocr_languages = Ocrs.get_ocr_languages()
|
|
|
|
|
2024-04-08 17:16:07 +02:00
|
|
|
# Send module state to logs
|
|
|
|
self.logger.info(f'Module {self.module_name} initialized')
|
|
|
|
|
2024-04-24 14:43:11 +02:00
|
|
|
def is_cached(self):
|
|
|
|
return self.r_cache.exists(f'ocr:no:{self.obj.id}')
|
|
|
|
|
|
|
|
def add_to_cache(self):
|
|
|
|
self.r_cache.setex(f'ocr:no:{self.obj.id}', 86400, 0)
|
|
|
|
|
2024-04-08 17:16:07 +02:00
|
|
|
def compute(self, message):
|
|
|
|
image = self.get_obj()
|
2024-04-25 14:18:22 +02:00
|
|
|
date = message
|
2024-04-08 17:16:07 +02:00
|
|
|
|
|
|
|
ocr = Ocrs.Ocr(image.id)
|
2024-04-24 14:43:11 +02:00
|
|
|
if self.is_cached():
|
|
|
|
return None
|
|
|
|
|
2024-04-24 15:16:18 +02:00
|
|
|
if self.obj.is_gif():
|
|
|
|
self.logger.warning(f'Ignoring GIF: {self.obj.id}')
|
|
|
|
return None
|
|
|
|
|
2024-04-08 17:16:07 +02:00
|
|
|
if not ocr.exists():
|
2024-04-24 14:43:11 +02:00
|
|
|
path = image.get_filepath()
|
2024-04-26 10:31:31 +02:00
|
|
|
languages = get_model_languages(image, self.ocr_languages)
|
2024-04-25 14:18:22 +02:00
|
|
|
print(image.id, languages)
|
2024-04-08 17:16:07 +02:00
|
|
|
texts = Ocrs.extract_text(path, languages)
|
|
|
|
if texts:
|
2024-04-24 14:43:11 +02:00
|
|
|
print('create')
|
2024-04-08 17:16:07 +02:00
|
|
|
ocr = Ocrs.create(image.id, texts)
|
2024-04-25 14:18:22 +02:00
|
|
|
if ocr:
|
|
|
|
self.add_message_to_queue(ocr)
|
|
|
|
else:
|
|
|
|
print('no text')
|
|
|
|
self.add_to_cache()
|
2024-04-24 14:43:11 +02:00
|
|
|
# Save in cache
|
|
|
|
else:
|
|
|
|
print('no text detected')
|
|
|
|
self.add_to_cache()
|
|
|
|
else:
|
2024-04-25 14:18:22 +02:00
|
|
|
# print(image.id)
|
|
|
|
# print('update correlation', date)
|
|
|
|
ocr.update_correlation(date=date)
|
2024-04-08 17:16:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
module = OcrExtractor()
|
|
|
|
module.run()
|