Exemplo n.º 1
0
def model_file_check():
    if os.path.isfile(Config.IMAGE_MODEL_FILE):
        return
    Logger.info('正在下载模型文件...')
    with requests.get(Config.OCR.get('image_model_url'), stream=True) as r:
        with open(Config.IMAGE_MODEL_FILE, 'wb') as f:
            shutil.copyfileobj(r.raw, f)
            Logger.info('下载成功\n')
Exemplo n.º 2
0
def model_file_check():
    try:
        if os.path.isfile(Config.IMAGE_MODEL_FILE):
            model_hash_verify()
            return
        Logger.info('正在下载模型文件...')
        with requests.get(Config.OCR.get('image_model_url'), stream=True) as r:
            with open(Config.IMAGE_MODEL_FILE, 'wb') as f:
                shutil.copyfileobj(r.raw, f)
                Logger.info('下载成功\n')
                model_hash_verify()
    except InvalidModelException:
        return model_file_check()
Exemplo n.º 3
0
    def get_coordinate(self, img_str):
        # 储存最终坐标结果
        result = ''

        try:
            # 读取并预处理验证码
            img = cv2.imdecode(np.fromstring(img_str, np.uint8),
                               cv2.IMREAD_COLOR)
            text = self.get_text(img)
            images = np.array(list(self._get_imgs(img)))
            images = self.preprocess_input(images)

            label = self.model.predict(text)
            label = label.argmax()
            text = self.texts[label]

            # list放文字
            titles = [text]

            position = []

            # 获取下一个词
            # 根据第一个词的长度来定位第二个词的位置
            if len(text) == 1:
                offset = 27
            elif len(text) == 2:
                offset = 47
            else:
                offset = 60
            text2 = self.get_text(img, offset=offset)
            if text2.mean() < 0.95:
                label = self.model.predict(text2)
                label = label.argmax()
                text2 = self.texts[label]
                titles.append(text2)

            labels = self.code_model.predict(images)
            labels = labels.argmax(axis=1)

            for pos, label in enumerate(labels):
                if self.texts[label] in titles:
                    position.append(pos + 1)

            # 没有识别到结果
            if len(position) == 0:
                return result
            result = position
            Logger.info('识别结果: %s' % result)
        except:
            pass
        return result
Exemplo n.º 4
0
class IpUtils:
    def __init__(self, config: Config, logger: Logger):
        self._config = Config() if config is None else config
        self._logger = Logger() if logger is None else logger

    def get_external_ip(self) -> str:
        external_ip = requests.get(self._config.get_ip_provider_url()).text
        self._logger.info('External ip address: %s' % external_ip)
        return external_ip

    @staticmethod
    def gather_ip_from_dns_response(dns_response_content: str) -> str:
        match = re.search(r'\"data\":\"(\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4})\"',
                          dns_response_content)
        if match:
            return match.group(1)
        else:
            raise ValueError('Ip cannot be gathered from dns response: %s' %
                             dns_response_content)
Exemplo n.º 5
0
                    dropout, device, hid_act, pooler_act, add_pooler
"""

device = 'cuda:0' if cfg.config['cuda'] else 'cpu'
bert_checkpoint = torch.load('saved_models/biobert-base-cased-v1.1.pth')
dense_layer_checkpoint = torch.load('saved_models/to_embedding.pt')

n_classes = len(train_data_loader.dataset.label2id)
model = MultiInstanceLearning(n_classes=n_classes,
                              vocab_size=len(vocab),
                              device=device,
                              bert_checkpoint=bert_checkpoint,
                              dense_layer_checkpoint=dense_layer_checkpoint,
                              **cfg.config)
model.to(device)
logger.info(model)

# optimizer and criterion

param = filter(lambda p: p.requires_grad, model.parameters())
# param = [p for p in model.parameters() if p.requires_grad]
optimizer = get_optimizer(cfg.config['optimizer'], param, lr=cfg.config['lr'])
lr_scheduler = ReduceLROnPlateau(optimizer,
                                 'max',
                                 factor=cfg.config['lr_decay'],
                                 patience=cfg.config['lr_decay_patience'])
criterion = nn.CrossEntropyLoss(
    weight=train_data_loader.dataset.weights.to(device))

#trainer
trainer = MILTrainer(model=model,
Exemplo n.º 6
0
                DB_INFO_ALERT_SETTINGS)

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--load', action='store_const', const=True)
    parser.add_argument('--save', action='store_const', const=True)
    parser.add_argument('--drop_elastic', action='store_const', const=True)
    parser.add_argument('--migrate', action='store_const', const=True)

    options = parser.parse_args(sys.argv[1:])

    col = Collector(base_path=BASE_PATH, sources=DATA_SOURCES)

    if options.load:
        logger.info('Action --load triggered', alert=True)

        col.load_all()  # TODO добавить какой-нибудь report об окончании загрузки с информацией о результатах

        logger.info('Action --load completed', alert=True)

    if options.save:
        logger.info('Action --save triggered', alert=True)

        report = col.save_all()

        logger.info(report, alert=True)

        logger.info('Action --save completed', alert=True)

    if options.drop_elastic: