forked from vns9/cartoon-gan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
217 lines (202 loc) · 8.63 KB
/
index.html
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<style>
body {
font-family: Arial
}
img {
max-width: 600px;
}
pre {
color: black;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
font-size: 14pt;
}
.MathJax {
color: black;
font-size: 3em !important;
}
</style>
</head>
<body>
<div id="app">
<v-app>
<v-app-bar dark absolute color="primary" app>
<v-toolbar-title>Cartoon-GAN</v-toolbar-title>
<v-spacer></v-spacer>
</v-app-bar>
<v-main>
<v-container class="px-16">
<v-card tile outlined :loading="loading" :color="dropping ? 'grey lighten-4' : ''">
<v-card-title>
Select Image
</v-card-title>
<v-card-text>
<v-row dropzone tile :height="height" @drop.prevent="drop"
@dragover.prevent="dropping = true" @dragleave.prevent="dropping = false"
@click="$refs.import.click()">
<v-col cols="12" justify="center" align="center">
Drag an image here or paste using Ctrl+V
</v-col>
<v-col cols="12" justify="center" align="center">
<v-btn :small="small" color="primary" :loading="loading">
or select image
</v-btn>
</v-col>
<input v-show="false" ref="import" type="file" @change="selectImage" />
</v-row>
</v-card-text>
</v-card>
<v-row class="py-6" wrap>
<v-col cols="6" justify="center" align="center" v-show="dropped">
<v-card tile outlined>
<v-card-title>
Input
</v-card-title>
<v-card-text>
<img style="width: 100%" id="input" />
</v-card-text>
</v-card>
</v-col>
<v-col cols="6" justify="center" align="center" v-show="output">
<v-card tile outlined>
<v-card-title>
Output
</v-card-title>
<v-card-text>
<img style="width: 100%" id="output" />
</v-card-text>
</v-card>
</v-col>
</v-row>
</v-container>
</v-main>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
small: false,
height: "500px",
saveOnServer: true,
uploadPercentage: 0,
loading: false,
uploadUrl: "api/",
contentTypes: {
"text": "application/json",
"file": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
},
dropping: false,
dropped: false,
output: false
}
},
methods: {
hexToBase64(str) {
return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "")
.replace(/([\da-fA-F]{2}) ?/g, "0x$1 ")
.replace(/ +$/, "").split(" ")))
},
drop(e) {
this.dropping = false
this.dropped = true
const html = e.dataTransfer.getData("text/html")
const files = e.target.files || e.dataTransfer.files
if (html && html.length) {
const regex = /src="?([^"\s]+)"?\s*/
let url = regex.exec(html)
if (!url || url.length < 1) return
else url = url[1]
const vm = this
this.checkImage(url, function (valid) {
if (valid) {
var image = document.getElementById('input');
image.src = url;
let data = { url }
vm.upload(data, "text")
} else console.log("Invalid Image")
})
} else if (files && files.length) this.uploadImage(files[0])
},
selectImage(e) {
const file = e.target.files[0]
this.dropped = true
if (file.type.indexOf("image") === 0) this.uploadImage(file)
},
uploadImage(file) {
const image = document.getElementById('input')
image.src = URL.createObjectURL(file);
const reader = new FileReader()
reader.readAsDataURL(file)
const formData = new FormData()
formData.append("file", file, file.name)
this.upload(formData, "file")
},
checkImage(url, callback) {
const image = new Image()
image.onload = function () {
callback(true)
}
image.onerror = function () {
callback(false)
}
image.src = url
},
upload(data, type) {
this.loading = true
this.output = false
axios
.post(this.uploadUrl, data, {
responseType: "blob",
headers: {
"Content-Type": this.contentTypes[type]
},
onUploadProgress: function (event) {
this.uploadPercentage = parseInt(Math.round((event.loaded / event.total) * 100))
}.bind(this)
})
.then(this.afterUpload, (error) => {
console.log(error)
this.loading = false
})
},
afterUpload(response) {
const url = URL.createObjectURL(response.data);
const img = document.getElementById("output")
img.src = url;
this.uploadPercentage = 0
this.loading = false
this.output = true
},
},
mounted() {
const vm = this;
document.onpaste = function (event) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (index in items) {
const item = items[index]
if (item.kind === 'file') {
vm.dropped = true
var blob = item.getAsFile()
vm.uploadImage(blob)
break
}
}
}
}
});
</script>
</body>
</html>