RicardoM.Lu1 2023-04-13 14:12 采纳率: 82.1%
浏览 62
已结题

在转换模型输出的内容时遇到问题

在转换模型输出的内容时遇到问题,代码如下:

import os
import cv2
import torch, gc

import numpy as np
import albumentations as albu
import segmentation_models_pytorch as smp

from albumentations.pytorch import ToTensorV2
from albumentations import Compose
from numba.cuda.libdeviceimpl import args

from tqdm import tqdm

CONFIG = {
    'img_size': 224,
    'model_name': 'timm-efficientnet-b0',
    'ckpt': 'am_224_latest.pth.tar',
}

os.environ['CUDA_VISIBLE_DEVICES'] = "0"


# device = torch.device("cuda:0" if torch.cuda.is_available() and not args.no_cuda else "cpu")


def make_transforms(tta=False):
    list_transforms = []
    if tta:
        list_transforms.extend(
            [
                albu.HorizontalFlip(p=1),
            ]
        )
    list_transforms.extend(
        [
            albu.Resize(CONFIG['img_size'], CONFIG['img_size']),
            ToTensorV2(),
        ]
    )

    list_trfms = Compose(list_transforms)
    return list_trfms


model = smp.UnetPlusPlus(
    CONFIG['model_name'],
    classes=1,
    encoder_weights=None,
    activation=None,
).cuda()
stuff = torch.load(CONFIG['ckpt'])
model.load_state_dict(stuff['state_dict'])
model.eval()


def sigmoid(x):
    return 1 / (1 + np.exp(-x))


test_trans = make_transforms()
tta_trans = make_transforms(tta=True)


def crop_image(img, crop_w, crop_h):
    ori_size = img.shape[0:2]  # 原图尺寸 (height, width)
    row_num = int(ori_size[0] / crop_h) + 1
    column_num = int(ori_size[1] / crop_w) + 1

    new_height = row_num * crop_h  # 小图像尺寸整倍数的大图像
    new_width = column_num * crop_w

    pad_h = new_height - ori_size[0]  # 在高维度上需要填充的像素
    pad_w = new_width - ori_size[1]

    # 从右下方填充
    img_new = cv2.copyMakeBorder(img, 0, pad_h, 0, pad_w, cv2.BORDER_CONSTANT, None, (0, 0, 0))

    crop_list = []
    for i in range(row_num):
        for j in range(column_num):
            img_crop = img_new[i * crop_h: (i + 1) * crop_h, j * crop_w: (j + 1) * crop_w]
            crop_list.append(img_crop)
    return crop_list, row_num, column_num


def compose_crop(mask_list, crop_w, crop_h, row, column, ori_w, ori_h):
    new_width = crop_w * column
    new_height = crop_h * row

    to_image = np.zeros((new_height, new_width, 1))
    i = 0
    for j in range(row):
        for k in range(column):
            to_image[j * crop_h: (j + 1) * crop_h, k * crop_w: (k + 1) * crop_w] = mask_list[i]
            i += 1

    return to_image[0:ori_h, 0:ori_w]


# input_dir = 'test.png'
# output_dir = 'image_output'
# trans_type = 'compose'

# if not os.path.exists(output_dir):
#     os.makedirs(output_dir)
def seal_ps(images, trans_type):
    # for temp_img in tqdm(os.listdir('test.png')):
    #     print(temp_img)
    #     if temp_img == '.ipynb_checkpoints':
    #         continue
    #     img_path = os.path.join(input_dir, temp_img)
    img_ori = cv2.imread(images)
    ori_size = img_ori.shape[0:2]
    crop_list, row_num, column_num = crop_image(img_ori, CONFIG['img_size'], CONFIG['img_size'])

    mask_list = []

    for i, crop_item in enumerate(crop_list):
        # img_path = os.path.join('example/1', name)
        # image = cv2.imread(img_path)
        img_rgb = cv2.cvtColor(crop_item, cv2.COLOR_BGR2RGB)
        shape = img_rgb.shape
        augmented = test_trans(image=img_rgb)
        img = augmented['image']
        img = img.float()

        ttaa = tta_trans(image=img_rgb)
        image_tta = ttaa['image']
        image_tta = image_tta.float()

        mask1 = 0

        if trans_type == 'default':

            masks1 = model(img.unsqueeze(0).cuda())

            mask1 += albu.Resize(shape[0], shape[1])(image=masks1[0].permute(1, 2, 0).detach().cpu().numpy())['image']
        elif trans_type == 'tta':
            masks_tta1 = model(image_tta.unsqueeze(0).cuda())
            mask1 += \
                albu.Resize(shape[0], shape[1])(
                    image=np.flip(masks_tta1[0].permute(1, 2, 0).detach().cpu().numpy(), axis=1))[
                    'image']
        elif trans_type == 'compose':
            masks1 = model(img.unsqueeze(0).cuda())

            mask1 += albu.Resize(shape[0], shape[1])(image=masks1[0].permute(1, 2, 0).detach().cpu().numpy())['image']
            masks_tta1 = model(image_tta.unsqueeze(0).cuda())
            mask1 += \
                albu.Resize(shape[0], shape[1])(
                    image=np.flip(masks_tta1[0].permute(1, 2, 0).detach().cpu().numpy(), axis=1))[
                    'image']
            mask1 /= 2

        fake_mask = ((sigmoid(mask1) > 0.5) * 255.).astype(np.uint8)
        mask_list.append(fake_mask.astype(np.uint8))

    mask_image = compose_crop(mask_list, CONFIG['img_size'], CONFIG['img_size'], row_num, column_num, ori_size[1],
                              ori_size[0])

    mask_image = mask_image.squeeze()
    mask_image = mask_image.astype(np.uint8)

    contours, _ = cv2.findContours(mask_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(img_ori, contours, -1, (255, 0, 0), 1)

    # img_ori = img_ori[:, :, ::-1]
    # img_ori[..., 2] = np.where(mask_image == 1, 255, img_ori[..., 2])

    # cv2.imwrite(os.path.join(output_dir, temp_img), img_ori)
    gc.collect()
    torch.cuda.empty_cache()

    return mask_list


if __name__ == '__main__':
    from PIL import Image


    # pic = open("8.png", "rb")
    b = seal_ps('test.png', trans_type='default')
    # b.show()

    # b = cv2.imread('8.png')
    # im = Image.fromarray(b)
    print(b)
    print(type(b))

输出信息如下:

[array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8), array([[[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       ...,

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]],

       [[0],
        [0],
        [0],
        ...,
        [0],
        [0],
        [0]]], dtype=uint8)]
<class 'list'>

Process finished with exit code 0

我的目的就是把输出的这个图片信息转化成base64和图片供我其他的模型使用

  • 写回答

2条回答 默认 最新

  • Zyb0627 2023-04-13 19:56
    关注

    引用chatGPT作答,您需要将输出的图片信息转换为base64编码格式以及可供其他模型使用的图像,可以尝试使用以下代码进行转换:

    import cv2
    import base64
    import numpy as np
    
    def encode_image(image):
        # 将图像转换为JPEG格式,压缩质量为95%
        _, img_encoded = cv2.imencode('.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
        # 将编码后的图像转换为base64编码格式
        img_base64 = base64.b64encode(img_encoded).decode('utf-8')
        return img_base64
    
    def decode_image(image_base64):
        # 将base64编码的图像解码
        img_decoded = base64.b64decode(image_base64)
        # 将解码后的图像转换为numpy数组格式
        img_array = np.frombuffer(img_decoded, dtype=np.uint8)
        # 将numpy数组转换为图像格式
        img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
        return img
    

    其中,encode_image函数将输入的图像转换为JPEG格式,然后将其编码为base64格式并返回;decode_image函数将输入的base64编码的图像解码为numpy数组,然后将其转换为图像格式并返回。

    您可以在将图像输出到其他模型之前,使用encode_image函数将其转换为base64格式,然后在其他模型中使用decode_image函数将其解码为图像格式。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月14日
  • 已采纳回答 4月14日
  • 创建了问题 4月13日

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败