Spaces:
Runtime error
Runtime error
File size: 5,568 Bytes
bb0f5a9 |
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
from tkinter import Tk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename
from GUI import View
from Inference import StyleCLIP
import argparse
#%%
class PlayInteractively(): #Controller
'''
followed Model View Controller Design Pattern
controller, model, view
'''
def __init__(self,dataset_name='ffhq'):
self.root = Tk()
self.view=View(self.root)
self.img_ratio=2
self.style_clip=StyleCLIP(dataset_name)
self.view.neutral.bind("<Return>", self.text_n)
self.view.target.bind("<Return>", self.text_t)
self.view.alpha.bind('<ButtonRelease-1>', self.ChangeAlpha)
self.view.beta.bind('<ButtonRelease-1>', self.ChangeBeta)
self.view.set_init.bind('<ButtonPress-1>', self.SetInit)
self.view.reset.bind('<ButtonPress-1>', self.Reset)
self.view.bg.bind('<Double-1>', self.open_img)
self.drawn = None
self.view.target.delete(1.0, "end")
self.view.target.insert("end", self.style_clip.target)
#
self.view.neutral.delete(1.0, "end")
self.view.neutral.insert("end", self.style_clip.neutral)
def Reset(self,event):
self.style_clip.GetDt2()
self.style_clip.M.alpha=[0]
self.view.beta.set(self.style_clip.beta)
self.view.alpha.set(0)
img=self.style_clip.GetImg()
img=Image.fromarray(img)
img = ImageTk.PhotoImage(img)
self.addImage_m(img)
def SetInit(self,event):
codes=self.style_clip.GetCode()
self.style_clip.M.dlatent_tmp=[tmp[:,0] for tmp in codes]
print('set init')
def ChangeAlpha(self,event):
tmp=self.view.alpha.get()
self.style_clip.M.alpha=[float(tmp)]
img=self.style_clip.GetImg()
print('manipulate one')
img=Image.fromarray(img)
img = ImageTk.PhotoImage(img)
self.addImage_m(img)
def ChangeBeta(self,event):
tmp=self.view.beta.get()
self.style_clip.beta=float(tmp)
img=self.style_clip.GetImg()
print('manipulate one')
img=Image.fromarray(img)
img = ImageTk.PhotoImage(img)
self.addImage_m(img)
def ChangeDataset(self,event):
dataset_name=self.view.set_category.get()
self.style_clip.LoadData(dataset_name)
self.view.target.delete(1.0, "end")
self.view.target.insert("end", self.style_clip.target)
self.view.neutral.delete(1.0, "end")
self.view.neutral.insert("end", self.style_clip.neutral)
def text_t(self,event):
tmp=self.view.target.get("1.0",'end')
tmp=tmp.replace('\n','')
self.view.target.delete(1.0, "end")
self.view.target.insert("end", tmp)
print('target',tmp,'###')
self.style_clip.target=tmp
self.style_clip.GetDt2()
self.view.beta.set(self.style_clip.beta)
self.view.alpha.set(3)
self.style_clip.M.alpha=[3]
img=self.style_clip.GetImg()
print('manipulate one')
img=Image.fromarray(img)
img = ImageTk.PhotoImage(img)
self.addImage_m(img)
def text_n(self,event):
tmp=self.view.neutral.get("1.0",'end')
tmp=tmp.replace('\n','')
self.view.neutral.delete(1.0, "end")
self.view.neutral.insert("end", tmp)
print('neutral',tmp,'###')
self.style_clip.neutral=tmp
self.view.target.delete(1.0, "end")
self.view.target.insert("end", tmp)
def run(self):
self.root.mainloop()
def addImage(self,img):
self.view.bg.create_image(self.view.width/2, self.view.height/2, image=img, anchor='center')
self.image=img #save a copy of image. if not the image will disappear
def addImage_m(self,img):
self.view.mani.create_image(512, 512, image=img, anchor='center')
self.image2=img
def openfn(self):
filename = askopenfilename(title='open',initialdir='./data/'+self.style_clip.M.dataset_name+'/',filetypes=[("all image format", ".jpg"),("all image format", ".png")])
return filename
def open_img(self,event):
x = self.openfn()
print(x)
img = Image.open(x)
img2 = img.resize(( 512,512), Image.ANTIALIAS)
img2 = ImageTk.PhotoImage(img2)
self.addImage(img2)
img = ImageTk.PhotoImage(img)
self.addImage_m(img)
img_index=x.split('/')[-1].split('.')[0]
img_index=int(img_index)
print(img_index)
self.style_clip.M.img_index=img_index
self.style_clip.M.dlatent_tmp=[tmp[img_index:(img_index+1)] for tmp in self.style_clip.M.dlatents]
self.style_clip.GetDt2()
self.view.beta.set(self.style_clip.beta)
self.view.alpha.set(3)
#%%
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--dataset_name',type=str,default='ffhq',
help='name of dataset, for example, ffhq')
args = parser.parse_args()
dataset_name=args.dataset_name
self=PlayInteractively(dataset_name)
self.run()
|