generated from GT-ZhangAcer/AI-Studio-Template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcartonModule.py
134 lines (111 loc) · 4.04 KB
/
cartonModule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 28 18:41:27 2020
@author: Administrator
"""
import cv2
import paddlehub as hub
import os
import CVTools
import time
import numpy as np
from tqdm import tqdm
from tqdm._tqdm import trange
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
def filesInFolder(rootdir, fileType='.jpg'):
pathList = []
nameList = []
filelist = os.listdir(rootdir) # 列出文件夹下所有的目录与文件
for i in range(len(filelist)):
if filelist[i][-4:] == fileType:
pathList.append(os.path.join(rootdir, filelist[i]))
nameList.append(filelist[i])
return pathList, nameList
# 模型加载
# use_gpu:是否使用GPU进行预测
# model = hub.Module(name='animegan_v2_hayao_99', use_gpu=True)
modelName = []
modelName.append('animegan_v2_shinkai_53')
modelName.append('animegan_v2_shinkai_33')
modelName.append('animegan_v2_paprika_98')
modelName.append('animegan_v2_paprika_97')
modelName.append('animegan_v2_paprika_74')
modelName.append('animegan_v2_paprika_54')
modelName.append('animegan_v2_hayao_99')
modelName.append('animegan_v2_hayao_64')
modelName.append('animegan_v1_hayao_60')
modelName.append('UGATIT_92w')
modelName.append('UGATIT_83w')
modelName.append('UGATIT_100w')
modelName.append('U2Net_Portrait')
modelName.append('Photo2Cartoon')
# 模型预测
# t1 = time.time()
# folderPath = 'E://DATA//XULIEZHEN'
# folderPath = 'E://DLresult//mopi9'
#
# # folderPath='E://DLresult//p1//pa97-2in1'
# outPath = 'E://DLresult//p1//sh53-2in1MOPI'
# outPath='E://DLresult//p1//pa97-8in1'
# os.makedirs(outPath)
# pathList, nameList = filesInFolder(folderPath)
def rename(pathList):
# 外循环遍历所有文件名,内循环遍历每个文件名的每个字符
for index, path in enumerate(pathList):
if 'result' in path:
# newpath=path.replace('zudui','组队')
newpath = path.replace('组队', 'zudui')
os.renames(path, newpath)
# rename()
# pathList, nameList = filesInFolder(folderPath)
# print(nameList)
##https://www.paddlepaddle.org.cn/hubdetail?name=animegan_v2_paprika_97&en_category=GANs
# model.style_transfer(paths=pathList[0:1],visualization=True,output_dir=outPath)
# bgpic = cv2.imread('bg1.jpg')
# img = cv2.imread('./test/mopi.jpg')
#
# img = cv2.imread('./thin 3.jpg')
# contenx = int(img.shape[1] / resizeIndex)
# conteny = int(img.shape[0] / resizeIndex)
# #
#
# image = bgpic.copy()
class cartoon_face():
def __init__(self,faceLandmarker):
self.faceLandmarker=faceLandmarker
self.perspect_size=256
# resizeIndex = 2
modelN = modelName[-7]
print('modelN', modelN)
self.cartoon_model = hub.Module(name=modelN, use_gpu=True)
def process(self,roi_img):
roi_img = cv2.medianBlur(roi_img, 3)
return self.cartoon_model.style_transfer(images=[roi_img], visualization=False)[0]
def run(self,user_bot,emotion_flag=0):
img_path=user_bot.imgPath
print('emotion_flag',emotion_flag)
print('img_path',img_path)
img=[]
img=cv2.imread(img_path)[:,:,:3]
if len(img.shape)==0:return []
landmarks=self.faceLandmarker.run(img)
if len(landmarks)>0:
roi_img=CVTools.roiChoice(landmarks, img, self.perspect_size)
user_bot.roiImg=roi_img
user_bot.roiCartoon=self.process(roi_img)
# roi_img = cv2.medianBlur(roi_img, 3)
# user_bot.roiCartoon =self.cartoon_model.style_transfer(images=[roi_img], visualization=False)[0]
return user_bot
else:
return user_bot
if __name__=='__main__':
from landmarkModule import landmarker
la=landmarker()
cf=cartoon_face(la)
import botClass
bot=botClass.bot()
bot.imgPath='pic/24451810641815485231.jpg'
out=cf.run(bot,2)
# print('in',image.shape,'out',out.shape)
cv2.imwrite('../cartoon.jpg', out.roiCartoon)
# print('time', time.time() - t1)