在转换模型输出的内容时遇到问题,代码如下:
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和图片供我其他的模型使用