1.下载源码地址
https://github.com/Mukosame/Anime2Sketch
- 下载项目依赖包,下载模型权重文件
运行看效果
python test.py --dataroot=E:\01_hjz\datas\00-hjz\pictures --load_size=512
调整自定义测试图片路径
"""Test script for anime-to-sketch translation
Example:
python3 test.py --dataroot /your_path/dir --load_size 512
python3 test.py --dataroot /your_path/img.jpg --load_size 512
python3 test.py --dataroot D:\ai\test\sumiao --load_size 512 --output_dir D:\ai\test\sumiao
"""
import os
import torch
from anime2Sketch.model_anime2sketch import create_anime2sketch_model
from anime2Sketch.data import read_img_path, tensor_to_img, save_image
import argparse
parser = argparse.ArgumentParser(description='Anime-to-sketch test options.')
parser.add_argument('--dataroot', '-i', default='test_samples/', type=str)
parser.add_argument('--load_size', '-s', default=512, type=int)
parser.add_argument('--output_dir', '-o', default='results/', type=str)
parser.add_argument('--gpu_ids', '-g', default=[], help="gpu ids: e.g. 0 0,1,2 0,2.")
opt = parser.parse_args()
# create model
gpu_list = ','.join(str(x) for x in opt.gpu_ids)
os.environ['CUDA_VISIBLE_DEVICES'] = gpu_list
device = torch.device('cuda' if len(opt.gpu_ids) > 0 else 'cpu')
print(device)
model = create_anime2sketch_model().to(device) # create a model given opt.model and other options
model.eval()
def chose_image(source_image_path, output_folder_path):
# img = cv2.imread(source_image_path)
save_dir = output_folder_path
#os.makedirs(save_dir, exist_ok=True)
# test_path ='D:\\ai\\test\sumiao\\5.jpg';
print(source_image_path)
basename = os.path.basename(source_image_path)
#print(basename)
aus_path = os.path.join(save_dir,basename.split(".")[0] +"_cutout.png")
#print(aus_path)
img, aus_resize = read_img_path(source_image_path, opt.load_size)
aus_tensor = model(img.to(device))
aus_img = tensor_to_img(aus_tensor)
save_image(aus_img, aus_path, aus_resize)
if __name__ == '__main__':
file_path = r"D:\work-mengyi\photo\miniprogram\static\images\default.jpg"
base_name = os.path.basename(file_path)
print(base_name)
# 20200708002140.jpg
suffix = base_name.split(".")[1]
print("suffix: {}".format(suffix))
# suffix: jpg
suffix = base_name.split(".")[0]
print("filename2222: {}".format(suffix))
# filename: 20200708002140
base_name = os.path.dirname(file_path)
print(base_name)
# d:\aaaa
#chose_image('D:\\ai\\test\\sumiao\\5.jpg', 'D:\\ai\\test\\sumiao')
# get input data
# if os.path.isdir(opt.dataroot):
# test_list = get_image_list(opt.dataroot)
# elif os.path.isfile(opt.dataroot):
# test_list = [opt.dataroot]
# else:
# raise Exception("{} is not a valid directory or image file.".format(opt.dataroot))
# save outputs
# for test_path in tqdm(test_list):
# print(test_path)
# basename = os.path.basename(test_path)
# print(basename)
# aus_path = os.path.join(save_dir, basename)
# print(aus_path)
# img, aus_resize = read_img_path(test_path, opt.load_size)
# aus_tensor = model(img.to(device))
# aus_img = tensor_to_img(aus_tensor)
# save_image(aus_img, aus_path, aus_resize)
体验地址: