MolScribe 分子结构识别模型

1、MolScribe简介

MolScribe:一种分子结构识别模型,将分子实体的图像转换为SMILES字符串。
MolScribe 项目地址
在这里插入图片描述

2、代码示例

pip install MolScribe
pip install huggingface_hub 
2.1 案例1
import torch
from molscribe import MolScribe
from huggingface_hub import hf_hub_download

ckpt_path = hf_hub_download('yujieq/MolScribe', 'swin_base_char_aux_1m.pth')

model = MolScribe(ckpt_path, device=torch.device('cpu'))
output = model.predict_image_file('assets/example.png', return_atoms_bonds=True, return_confidence=True)
{
    'smiles': 'Fc1ccc(-c2cc(-c3ccccc3)n(-c3ccccc3)c2)cc1',
    'molfile': '***', 
    'confidence': 0.9175,
    'atoms': [{'atom_symbol': '[Ph]', 'x': 0.5714, 'y': 0.9523, 'confidence': 0.9127}, ... ],
    'bonds': [{'bond_type': 'single', 'endpoint_atoms': [0, 1], 'confidence': 0.9999}, ... ]
}
2.2 案例2
import torch
from molscribe import MolScribe
from huggingface_hub import hf_hub_download
import os
import cv2
import numpy as np


if __name__ == "__main__":

    DATAPATH = '/mnt/d/wsl_workspace/yolo11/result/001_predict'
    PREDICT_PATH = '/mnt/d/wsl_workspace/yolo11/MolScribe/result/01'
    if not os.path.exists(PREDICT_PATH):
        os.makedirs(PREDICT_PATH)


    ckpt_path = '/mnt/d/models/MolScribe/swin_base_char_aux_1m.pth'
    model = MolScribe(ckpt_path, device=torch.device('cpu'))


    font = cv2.FONT_HERSHEY_SIMPLEX  # 字体类型
    font_scale = 0.5  # 字体大小
    color = (0, 0, 255)  # 字体颜色
    thickness = 1  # 字体粗细

    for dirpath, dirnames, filenames in os.walk(DATAPATH):
        for filename in filenames:
            inputpath = os.path.join(dirpath,filename)
            savename = os.path.join(PREDICT_PATH,filename)
            image = cv2.imread(inputpath)

            large_matrix = np.full((300,500,3),255,dtype=np.uint8)
            start_row = (large_matrix.shape[0] - image.shape[0])//2
            start_col = (large_matrix.shape[1] - image.shape[1])//2
            large_matrix[start_row:start_row + image.shape[0], 
                         start_col:start_col + image.shape[1],:] = image
            
            output = model.predict_image_file(inputpath, return_atoms_bonds=True, return_confidence=True)
            print(filename,' : ',output['smiles'])
            cv2.putText(large_matrix, output['smiles'], (0, 290), font, font_scale, color, thickness)
            cv2.imwrite(savename,large_matrix)


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值