File size: 15,396 Bytes
9dce458 |
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Image/Manga Translator</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/petite-vue.iife.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@unocss/[email protected]/uno.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@iconify/[email protected]/dist/iconify.min.js"></script>
<style>
[v-cloak],
[un-cloak] {
display: none;
}
</style>
</head>
<body>
<form
action="#"
class="flex py-8 w-full min-h-100vh justify-center items-center"
@submit.prevent="onsubmit"
@vue:mounted="onmounted"
v-scope
v-cloak
un-cloak
>
<div class="flex flex-col w-85ch h-full justify-center gap-2">
<h1 class="text-center text-lg font-light">Image/Manga Translator</h1>
<div class="flex mx-4 justify-start items-end">
<div class="flex gap-4">
<div class="flex items-center" title="Detection resolution">
<i class="iconify" data-icon="carbon:fit-to-screen"></i>
<div class="relative">
<select class="w-9ch appearance-none bg-transparent border-b border-gray-300" v-model="detectionResolution">
<option value="S">1024px</option>
<option value="M">1536px</option>
<option value="L">2048px</option>
<option value="X">2560px</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Text detector">
<i class="iconify" data-icon="carbon:search-locate"></i>
<div class="relative">
<select class="w-9ch appearance-none bg-transparent border-b border-gray-300" v-model="textDetector">
<option value="auto">Default</option>
<option value="ctd">CTD</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Render text orientation">
<i class="iconify" data-icon="carbon:text-align-left"></i>
<div class="relative">
<select class="w-12ch appearance-none bg-transparent border-b border-gray-300" v-model="renderTextDirection">
<option value="auto">Auto</option>
<option value="h">Horizontal</option>
<option value="v">Vertical</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Translator">
<i class="iconify" data-icon="carbon:operations-record"></i>
<div class="relative">
<select class="w-9ch appearance-none bg-transparent border-b border-gray-300" v-model="translator">
<option v-for="key in validTranslators" :value="key">{{getTranslatorName(key)}}</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
<div class="flex items-center gap-1" title="Target language">
<i class="iconify" data-icon="carbon:language"></i>
<div class="relative">
<select class="w-15ch appearance-none bg-transparent border-b border-gray-300" v-model="targetLanguage">
<option value="CHS">简体中文</option>
<option value="CHT">繁體中文</option>
<option value="JPN">日本語</option>
<option value="ENG">English</option>
<option value="KOR">한국어</option>
<option value="VIN">Tiếng Việt</option>
<option value="CSY">čeština</option>
<option value="NLD">Nederlands</option>
<option value="FRA">français</option>
<option value="DEU">Deutsch</option>
<option value="HUN">magyar nyelv</option>
<option value="ITA">italiano</option>
<option value="PLK">polski</option>
<option value="PTB">português</option>
<option value="ROM">limba română</option>
<option value="RUS">русский язык</option>
<option value="ESP">español</option>
<option value="TRK">Türk dili</option>
<option value="IND">Indonesia</option>
</select>
<i class="iconify absolute top-1.5 right-1 pointer-events-none" data-icon="carbon:chevron-down"></i>
</div>
</div>
</div>
</div>
<div v-if="result" class="flex flex-col items-center">
<img class="my-2" :src="resultUri" />
<button class="px-2 py-1 text-center rounded-md text-blue-800 border-2 border-blue-300" @click="clear">Upload another</button>
</div>
<div v-else-if="status" class="grid w-full h-116 place-content-center rounded-2xl border-2 border-dashed border-gray-600">
<div v-if="error" class="flex flex-col items-center gap-2">
<div style="color: crimson">{{ statusText }}</div>
<button class="px-2 py-1 text-center rounded-md text-blue-800 border-2 border-blue-300" @click="clear">Upload another</button>
</div>
<div v-else class="flex flex-col items-center gap-2">
<i class="iconify w-8 h-8 text-gray-500 animate-spin" data-icon="carbon:progress-bar-round"></i>
<div>{{ statusText }}</div>
</div>
</div>
<label
v-else
class="grid w-full h-116 place-content-center rounded-2xl border-2 border-dashed border-gray-600 cursor-pointer"
for="file"
@dragenter.prevent
@dragover.prevent
@dragleave.prevent
@drop.prevent="ondrop"
>
<div v-if="file" class="flex flex-col items-center gap-2">
<div><span class="iconify-inline inline-block mr-2 scale-125" data-icon="carbon:image-search"></span>File Preview</div>
<img class="max-w-72 max-h-72" :src="fileUri" />
<button type="submit" class="px-2 py-1 rounded-md text-blue-800 border-2 border-blue-300">Translate</button>
<div class="text-sm text-gray-600">Click the empty space or paste/drag a new one to replace</div>
</div>
<div v-else class="flex flex-col items-center gap-2">
<i class="iconify w-8 h-8 text-gray-500" data-icon="carbon:cloud-upload"></i>
<div>Paste an image, click to select one or drag and drop here</div>
</div>
<input id="file" type="file" accept="image/png,image/jpeg,image/bmp,image/webp" class="hidden" @change="onfilechange" />
</label>
<div class="flex justify-center gap-2">
<div>
Please consider supporting us by
<a class="underline underline-blue-400" href="https://ko-fi.com/voilelabs" target="_blank" rel="noopener noreferrer">Ko-fi</a>
or
<a class="underline underline-blue-400" href="https://www.patreon.com/voilelabs" target="_blank" rel="noopener noreferrer"
>Patreon</a
>!
</div>
<a
class="underline underline-blue-400"
href="https://github.com/zyddnys/manga-image-translator"
target="_blank"
rel="noopener noreferrer"
>Source Code</a
>
</div>
</div>
</form>
<script>
const BASE_URI = '/'
const acceptTypes = ['image/png', 'image/jpeg', 'image/bmp', 'image/webp']
function formatSize(bytes) {
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return '0B'
const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${(bytes / k ** i).toFixed(2)}${sizes[i]}`
}
function formatProgress(loaded, total) {
return `${formatSize(loaded)}/${formatSize(total)}`
}
PetiteVue.createApp({
onmounted() {
window.addEventListener('paste', this.onpaste)
},
file: null,
get fileUri() {
return this.file ? URL.createObjectURL(this.file) : null
},
detectionResolution: 'M',
textDetector: 'auto',
renderTextDirection: 'auto',
translator: 'youdao',
validTranslators: ['youdao', 'baidu', 'google', 'deepl', 'papago', 'caiyun', 'offline', 'gpt3.5', 'none'],
getTranslatorName(key) {
if (key == 'none')
return "No Text"
return key ? key[0].toUpperCase() + key.slice(1) : "";
},
targetLanguage: 'CHS',
ondrop(e) {
const file = e.dataTransfer?.files?.[0]
if (file && acceptTypes.includes(file.type)) {
this.file = file
}
},
onfilechange(e) {
const file = e.target.files?.[0]
if (file && acceptTypes.includes(file.type)) {
this.file = file
}
},
onpaste(e) {
const items = (e.clipboardData || e.originalEvent.clipboardData).items
for (const item of items) {
if (item.kind === 'file') {
const file = item.getAsFile()
if (!file || !acceptTypes.includes(file.type)) continue
this.file = file
}
}
},
progress: null,
status: null,
queuePos: null,
cachedStatusText: '',
get statusText() {
var newStatusText = this._statusText
if (newStatusText != null && newStatusText != this.cachedStatusText) {
this.cachedStatusText = newStatusText
}
return this.cachedStatusText
},
get _statusText() {
switch (this.status) {
case 'upload': {
if (this.progress) {
return `Uploading (${this.progress})`
} else {
return 'Uploading'
}
}
case 'download':
if (this.progress) {
return `Downloading (${this.progress})`
} else {
return 'Downloading'
}
case 'pending':
if (this.queuePos) {
return `Queuing, your position is ${this.queuePos}`
} else {
return 'Processing'
}
case 'detection':
return 'Detecting texts'
case 'ocr':
return 'Running OCR'
case 'mask-generation':
return 'Generating text mask'
case 'inpainting':
return 'Running inpainting'
case 'upscaling':
return 'Running upscaling'
case 'translating':
return 'Translating'
case 'rendering':
return 'Rendering translated texts'
case 'error':
return 'Something went wrong, please try again'
case 'error-upload':
return 'Upload failed, please try again'
case 'error-lang':
return 'Your target language is not supported by the chosen translator'
case 'error-translating':
return 'Did not get any text back from the text translation service'
case 'error-too-large':
return 'Image size too large (greater than 8000x8000 px)'
case 'error-disconnect':
return 'Lost connection to server'
}
},
get error() {
return /^error/.test(this.status)
},
result: null,
get resultUri() {
return this.result ? URL.createObjectURL(this.result) : null
},
onsubmit(e) {
if (!this.file) return
this.progress = null
this.queuePos = null
this.status = 'upload'
const formData = new FormData()
formData.append('file', this.file)
formData.append('size', this.detectionResolution)
formData.append('detector', this.textDetector)
formData.append('direction', this.renderTextDirection)
formData.append('translator', this.translator)
formData.append('tgt_lang', this.targetLanguage)
const xhr = new XMLHttpRequest()
xhr.open('POST', `${BASE_URI}submit`, true)
xhr.onerror = (e) => {
this.status = 'error-disconnect'
}
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) this.progress = formatProgress(e.loaded, e.total)
}
xhr.onload = async () => {
if (xhr.status !== 200) {
this.status = 'error-upload'
return
}
response = JSON.parse(xhr.responseText)
const task_id = response['task_id']
this.status = response['status']
if (this.error)
return
this.status = 'pending'
async function tryFetchTaskState() {
try {
return await (await fetch(`${BASE_URI}task-state?taskid=${task_id}`)).json()
}
catch {
return null
}
}
for (;;) {
const timer = new Promise((resolve) => setTimeout(resolve, 500))
const res = await tryFetchTaskState()
if (res == null) {
this.status = 'error-disconnect'
break
}
const { state, finished, waiting } = res
// console.log(state, finished, waiting)
if (finished && !state.startsWith('error')) {
this.progress = null
this.status = 'download'
const xhrDownload = new XMLHttpRequest()
xhrDownload.open('GET', `${BASE_URI}result/${task_id}`, true)
xhrDownload.responseType = 'blob'
xhrDownload.onprogress = (e) => {
if (e.lengthComputable) this.progress = formatProgress(e.loaded, e.total)
}
xhrDownload.onload = () => {
this.result = xhrDownload.response
this.status = null
}
xhrDownload.send()
break
}
this.status = state
this.queuePos = waiting
if (/^error/.test(state)) {
break
}
await timer
}
}
xhr.send(formData)
},
clear() {
this.file = null
this.result = null
this.status = null
},
}).mount()
</script>
</body>
</html>
|