1
0
Fork 0
mirror of synced 2024-06-28 19:20:32 +12:00

Improve web interface

This commit is contained in:
nagadomi 2015-11-07 22:54:29 +09:00
parent 15f3f2d901
commit 3b2aa67e55
9 changed files with 219 additions and 234 deletions

View file

@ -2,51 +2,17 @@
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="canonical" href="http://waifu2x.udp.jp/">
<title>waifu2x</title> <title>waifu2x</title>
<style type="text/css"> <link href="style.css" rel="stylesheet" type="text/css">
body {
margin: 1em 2em 1em 2em;
background: LightGray;
width: 640px;
}
fieldset {
margin-top: 1em;
margin-bottom: 1em;
}
.about {
position: relative;
display: inline-block;
font-size: 0.9em;
padding: 1em 5px 0.2em 0;
}
.help {
font-size: 0.85em;
margin: 1em 0 0 0;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript"> <script type="text/javascript" src="ui.js"></script>
function clear_file() {
var new_file = $("#file").clone();
new_file.change(clear_url);
$("#file").replaceWith(new_file);
}
function clear_url() {
$("#url").val("")
}
$(function (){
$("#url").change(clear_file);
$("#file").change(clear_url);
})
</script>
</head> </head>
<body> <body>
<h1>waifu2x</h1> <h1>waifu2x</h1>
<div class="header"> <div class="header">
<div style="position:absolute; display:block; top:0; left:540px; max-height:140px;"> <div class="github-banner">
<img style="position:absolute; display:block; left:0; top:0; width:149px; height:149px; border:0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"> <img class="github-banner-image" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png">
<a href="https://github.com/nagadomi/waifu2x" target="_blank" style="position:absolute; display:block; left:0; top:0; width:149px; height:130px;"></a> <a class="github-banner-link" href="https://github.com/nagadomi/waifu2x" target="_blank"></a>
</div> </div>
<a href="index.html">en</a>/<a href="index.ja.html">ja</a>/<a href="index.ru.html">ru</a> <a href="index.html">en</a>/<a href="index.ja.html">ja</a>/<a href="index.ru.html">ru</a>
</div> </div>
@ -67,11 +33,18 @@
</div> </div>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>Style</legend>
<label><input type="radio" name="style" value="art" checked>Artwork</label>
<label><input type="radio" name="style" value="photo">Photo</label>
</fieldset>
<fieldset class="noise-field">
<legend>Noise Reduction (expect JPEG Artifact)</legend> <legend>Noise Reduction (expect JPEG Artifact)</legend>
<label><input type="radio" name="noise" value="0"> None</label> <label><input type="radio" name="noise" value="0"> None</label>
<label><input type="radio" name="noise" value="1" checked="checked"> Medium</label> <label><input type="radio" name="noise" value="1" checked="checked"> Medium</label>
<label><input type="radio" name="noise" value="2"> High</label> <label><input type="radio" name="noise" value="2"> High</label>
<div class="help">When using 2x scaling, we never recommend to use high level of noise reduction, it almost always makes image worse, it makes sense for only some rare cases when image had really bad quality from the beginning.</div> <div class="help">
When using 2x scaling, we never recommend to use high level of noise reduction, it almost always makes image worse, it makes sense for only some rare cases when image had really bad quality from the beginning.
</div>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>Upscaling</legend> <legend>Upscaling</legend>
@ -82,7 +55,7 @@
<input type="submit"/> <input type="submit"/>
</form> </form>
<div class="help"> <div class="help">
<ul style="padding-left: 15px;"> <ul class="padding-left">
<li>If you are using Firefox, Please press the CTRL+S key to save image. "Save Image" option doesn't work. <li>If you are using Firefox, Please press the CTRL+S key to save image. "Save Image" option doesn't work.
</ul> </ul>
</div> </div>

View file

@ -2,51 +2,17 @@
<html lang="ja"> <html lang="ja">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="canonical" href="http://waifu2x.udp.jp/"> <link href="style.css" rel="stylesheet" type="text/css">
<title>waifu2x</title> <title>waifu2x</title>
<style type="text/css">
body {
margin: 1em 2em 1em 2em;
background: LightGray;
width: 640px;
}
fieldset {
margin-top: 1em;
margin-bottom: 1em;
}
.about {
position: relative;
display: inline-block;
font-size: 0.8em;
padding: 1em 5px 0.2em 0;
}
.help {
font-size: 0.8em;
margin: 1em 0 0 0;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript"> <script type="text/javascript" src="ui.js"></script>
function clear_file() {
var new_file = $("#file").clone();
new_file.change(clear_url);
$("#file").replaceWith(new_file);
}
function clear_url() {
$("#url").val("")
}
$(function (){
$("#url").change(clear_file);
$("#file").change(clear_url);
})
</script>
</head> </head>
<body> <body>
<h1>waifu2x</h1> <h1>waifu2x</h1>
<div class="header"> <div class="header">
<div style="position:absolute; display:block; top:0; left:540px; max-height:140px;"> <div class="github-banner">
<img style="position:absolute; display:block; left:0; top:0; width:149px; height:149px; border:0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"> <img class="github-banner-image" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png">
<a href="https://github.com/nagadomi/waifu2x" target="_blank" style="position:absolute; display:block; left:0; top:0; width:149px; height:130px;"></a> <a class="github-banner-link" href="https://github.com/nagadomi/waifu2x" target="_blank"></a>
</div> </div>
<a href="index.html">en</a>/<a href="index.ja.html">ja</a>/<a href="index.ru.html">ru</a> <a href="index.html">en</a>/<a href="index.ja.html">ja</a>/<a href="index.ru.html">ru</a>
</div> </div>
@ -67,6 +33,11 @@
</div> </div>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>スタイル</legend>
<label><input type="radio" name="style" value="art" checked>イラスト</label>
<label><input type="radio" name="style" value="photo">写真</label>
</fieldset>
<fieldset class="noise-field">
<legend>ノイズ除去 (JPEGイズを想定)</legend> <legend>ノイズ除去 (JPEGイズを想定)</legend>
<label><input type="radio" name="noise" value="0"> なし</label> <label><input type="radio" name="noise" value="0"> なし</label>
<label><input type="radio" name="noise" value="1" checked="checked"></label> <label><input type="radio" name="noise" value="1" checked="checked"></label>
@ -81,7 +52,7 @@
<input type="submit" value="実行"/> <input type="submit" value="実行"/>
</form> </form>
<div class="help"> <div class="help">
<ul style="padding-left: 15px;"> <ul class="padding-left">
<li>なし/なしで入力画像を変換せずに出力する。ブラウザのタブで変換結果を比較したい人用。 <li>なし/なしで入力画像を変換せずに出力する。ブラウザのタブで変換結果を比較したい人用。
<li>Firefoxの方は、右クリから画像が保存できないようなので、CTRL+SキーかALTキー後 ファイル - ページを保存 で画像を保存してください。 <li>Firefoxの方は、右クリから画像が保存できないようなので、CTRL+SキーかALTキー後 ファイル - ページを保存 で画像を保存してください。
</ul> </ul>

View file

@ -2,51 +2,18 @@
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="canonical" href="http://waifu2x.udp.jp/"> <link href="style.css" rel="stylesheet" type="text/css">
<title>waifu2x</title> <title>waifu2x</title>
<style type="text/css"> <link href="style.css" rel="stylesheet" type="text/css">
body {
margin: 1em 2em 1em 2em;
background: LightGray;
width: 640px;
}
fieldset {
margin-top: 1em;
margin-bottom: 1em;
}
.about {
position: relative;
display: inline-block;
font-size: 0.9em;
padding: 1em 5px 0.2em 0;
}
.help {
font-size: 0.85em;
margin: 1em 0 0 0;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript"> <script type="text/javascript" src="ui.js"></script>
function clear_file() {
var new_file = $("#file").clone();
new_file.change(clear_url);
$("#file").replaceWith(new_file);
}
function clear_url() {
$("#url").val("")
}
$(function (){
$("#url").change(clear_file);
$("#file").change(clear_url);
})
</script>
</head> </head>
<body> <body>
<h1>waifu2x</h1> <h1>waifu2x</h1>
<div class="header"> <div class="header">
<div style="position:absolute; display:block; top:0; left:540px; max-height:140px;"> <div class="github-banner">
<img style="position:absolute; display:block; left:0; top:0; width:149px; height:149px; border:0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"> <img class="github-banner-image" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png">
<a href="https://github.com/nagadomi/waifu2x" target="_blank" style="position:absolute; display:block; left:0; top:0; width:149px; height:130px;"></a> <a class="github-banner-link" href="https://github.com/nagadomi/waifu2x" target="_blank"></a>
</div> </div>
<a href="index.html">en</a>/<a href="index.ja.html">ja</a>/<a href="index.ru.html">ru</a> <a href="index.html">en</a>/<a href="index.ja.html">ja</a>/<a href="index.ru.html">ru</a>
</div> </div>
@ -67,10 +34,15 @@
</div> </div>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>Устранение шума (артефактов JPEG)</legend> <legend>Стиль</legend>
<label><input type="radio" name="style" value="art" checked>Произведение искусства</label>
<label><input type="radio" name="style" value="photo">Фотография</label>
</fieldset>
<fieldset class="noise-field">
<legend>Устранение шума (артефактов JPEG)</legend>
<label><input type="radio" name="noise" value="0"> Нет</label> <label><input type="radio" name="noise" value="0"> Нет</label>
<label><input type="radio" name="noise" value="1" checked="checked"> Средне</label> <label><input type="radio" name="noise" value="1" checked="checked"> Средне</label>
<label><input type="radio" name="noise" value="2"> Сильно (не рекомендуется)</label> <label><input type="radio" name="noise" value="2"> Сильно</label>
<div class="help">Устранение шума нужно использовать, если на картинке действительно есть шум, иначе это даст противоположный эффект. Также не рекомендуется сильное устранение шума, оно даёт выгоду только в редких случаях, когда картинка изначально была сильно испорчена.</div> <div class="help">Устранение шума нужно использовать, если на картинке действительно есть шум, иначе это даст противоположный эффект. Также не рекомендуется сильное устранение шума, оно даёт выгоду только в редких случаях, когда картинка изначально была сильно испорчена.</div>
</fieldset> </fieldset>
<fieldset> <fieldset>
@ -82,8 +54,9 @@
<input type="submit"/> <input type="submit"/>
</form> </form>
<div class="help"> <div class="help">
<ul style="padding-left: 15px;"> <ul class="padding-left">
<li>Если Вы используете Firefox, для сохранения изображения Вам придётся нажать Ctrl+S (опция в меню "Сохранить изображение" работать не будет!) <li>Если Вы используете Firefox, для сохранения изображения Вам придётся нажать Ctrl+S (опция в меню "Сохранить изображение" работать не будет!)
</li>
</ul> </ul>
</div> </div>
</body> </body>

46
assets/style.css Normal file
View file

@ -0,0 +1,46 @@
body {
margin: 1em 2em 1em 2em;
background: LightGray;
width: 640px;
}
fieldset {
margin-top: 1em;
margin-bottom: 1em;
}
.about {
position: relative;
display: inline-block;
font-size: 0.9em;
padding: 1em 5px 0.2em 0;
}
.help {
font-size: 0.85em;
margin: 1em 0 0 0;
}
.github-banner {
position:absolute;
display:block;
top:0;
left:540px;
max-height:140px;
}
.github-banner-image {
position: absolute;
display: block;
left: 0;
top: 0;
width: 149px;
height: 149px;
border: 0;
}
.github-banner-link {
position: absolute;
display: block;
left:0;
top:0;
width:149px;
height:130px;
}
.padding-left {
padding-left: 15px;
}

24
assets/ui.js Normal file
View file

@ -0,0 +1,24 @@
$(function (){
function clear_file() {
var new_file = $("#file").clone();
new_file.change(clear_url);
$("#file").replaceWith(new_file);
}
function clear_url() {
$("#url").val("")
}
function on_change_style(e) {
var style = $("input[name=style]:checked").val()
if (style == "photo") {
$("input[name=noise]").prop("disabled", true);
$(".noise-field").hide()
} else {
$("input[name=noise]").prop("disabled", false);
$(".noise-field").show();
}
}
$("#url").change(clear_file);
$("#file").change(clear_url);
$("input[name=style]").change(on_change_style);
})

48
lib/cleanup_model.lua Normal file
View file

@ -0,0 +1,48 @@
-- ref: https://github.com/torch/nn/issues/112#issuecomment-64427049
local function zeroDataSize(data)
if type(data) == 'table' then
for i = 1, #data do
data[i] = zeroDataSize(data[i])
end
elseif type(data) == 'userdata' then
data = torch.Tensor():typeAs(data)
end
return data
end
-- Resize the output, gradInput, etc temporary tensors to zero (so that the
-- on disk size is smaller)
local function cleanupModel(node)
if node.output ~= nil then
node.output = zeroDataSize(node.output)
end
if node.gradInput ~= nil then
node.gradInput = zeroDataSize(node.gradInput)
end
if node.finput ~= nil then
node.finput = zeroDataSize(node.finput)
end
if tostring(node) == "nn.LeakyReLU" or tostring(node) == "w2nn.LeakyReLU" then
if node.negative ~= nil then
node.negative = zeroDataSize(node.negative)
end
end
if tostring(node) == "nn.Dropout" then
if node.noise ~= nil then
node.noise = zeroDataSize(node.noise)
end
end
-- Recurse on nodes with 'modules'
if (node.modules ~= nil) then
if (type(node.modules) == 'table') then
for i = 1, #node.modules do
local child = node.modules[i]
cleanupModel(child)
end
end
end
end
function w2nn.cleanup_model(model)
cleanupModel(model)
return model
end

View file

@ -21,5 +21,6 @@ else
require 'DepthExpand2x' require 'DepthExpand2x'
require 'WeightedMSECriterion' require 'WeightedMSECriterion'
require 'WeightedHuberCriterion' require 'WeightedHuberCriterion'
require 'cleanup_model'
return w2nn return w2nn
end end

View file

@ -4,53 +4,6 @@ package.path = path.join(path.dirname(__FILE__), "..", "lib", "?.lua;") .. packa
require 'w2nn' require 'w2nn'
torch.setdefaulttensortype("torch.FloatTensor") torch.setdefaulttensortype("torch.FloatTensor")
-- ref: https://github.com/torch/nn/issues/112#issuecomment-64427049
local function zeroDataSize(data)
if type(data) == 'table' then
for i = 1, #data do
data[i] = zeroDataSize(data[i])
end
elseif type(data) == 'userdata' then
data = torch.Tensor():typeAs(data)
end
return data
end
-- Resize the output, gradInput, etc temporary tensors to zero (so that the
-- on disk size is smaller)
local function cleanupModel(node)
if node.output ~= nil then
node.output = zeroDataSize(node.output)
end
if node.gradInput ~= nil then
node.gradInput = zeroDataSize(node.gradInput)
end
if node.finput ~= nil then
node.finput = zeroDataSize(node.finput)
end
if tostring(node) == "nn.LeakyReLU" or tostring(node) == "w2nn.LeakyReLU" then
if node.negative ~= nil then
node.negative = zeroDataSize(node.negative)
end
end
if tostring(node) == "nn.Dropout" then
if node.noise ~= nil then
node.noise = zeroDataSize(node.noise)
end
end
-- Recurse on nodes with 'modules'
if (node.modules ~= nil) then
if (type(node.modules) == 'table') then
for i = 1, #node.modules do
local child = node.modules[i]
cleanupModel(child)
end
end
end
collectgarbage()
end
local cmd = torch.CmdLine() local cmd = torch.CmdLine()
cmd:text() cmd:text()
cmd:text("cleanup model") cmd:text("cleanup model")
@ -62,7 +15,7 @@ cmd:option("-oformat", "binary", 'output format')
local opt = cmd:parse(arg) local opt = cmd:parse(arg)
local model = torch.load(opt.model, opt.iformat) local model = torch.load(opt.model, opt.iformat)
if model then if model then
cleanupModel(model) w2nn.cleanup_model(model)
torch.save(opt.model, model, opt.oformat) torch.save(opt.model, model, opt.oformat)
else else
error("model not found") error("model not found")

122
web.lua
View file

@ -1,10 +1,10 @@
local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)() local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
package.path = path.join(path.dirname(__FILE__), "lib", "?.lua;") .. package.path require 'pl'
local ROOT = path.dirname(__FILE__)
package.path = path.join(ROOT, "lib", "?.lua;") .. package.path
_G.TURBO_SSL = true _G.TURBO_SSL = true
require 'pl'
require 'w2nn' require 'w2nn'
local turbo = require 'turbo'
local uuid = require 'uuid' local uuid = require 'uuid'
local ffi = require 'ffi' local ffi = require 'ffi'
local md5 = require 'md5' local md5 = require 'md5'
@ -12,6 +12,11 @@ local iproc = require 'iproc'
local reconstruct = require 'reconstruct' local reconstruct = require 'reconstruct'
local image_loader = require 'image_loader' local image_loader = require 'image_loader'
-- Notes: turbo and xlua has different implementation of string:split().
-- Therefore, string:split() has conflict issue.
-- In this script, use turbo's string:split().
local turbo = require 'turbo'
local cmd = torch.CmdLine() local cmd = torch.CmdLine()
cmd:text() cmd:text()
cmd:text("waifu2x-api") cmd:text("waifu2x-api")
@ -29,14 +34,14 @@ if cudnn then
cudnn.fastest = true cudnn.fastest = true
cudnn.benchmark = false cudnn.benchmark = false
end end
local ART_MODEL_DIR = path.join(ROOT, "models", "anime_style_art_rgb")
local PHOTO_MODEL_DIR = path.join(ROOT, "models", "ukbench")
local art_noise1_model = torch.load(path.join(ART_MODEL_DIR, "noise1_model.t7"), "ascii")
local art_noise2_model = torch.load(path.join(ART_MODEL_DIR, "noise2_model.t7"), "ascii")
local art_scale2_model = torch.load(path.join(ART_MODEL_DIR, "scale2.0x_model.t7"), "ascii")
local photo_scale2_model = torch.load(path.join(PHOTO_MODEL_DIR, "scale2.0x_model.t7"), "ascii")
local MODEL_DIR = "./models/anime_style_art_rgb" local CACHE_DIR = path.join(ROOT, "cache")
local noise1_model = torch.load(path.join(MODEL_DIR, "noise1_model.t7"), "ascii")
local noise2_model = torch.load(path.join(MODEL_DIR, "noise2_model.t7"), "ascii")
local scale20_model = torch.load(path.join(MODEL_DIR, "scale2.0x_model.t7"), "ascii")
local USE_CACHE = true
local CACHE_DIR = "./cache"
local MAX_NOISE_IMAGE = 2560 * 2560 local MAX_NOISE_IMAGE = 2560 * 2560
local MAX_SCALE_IMAGE = 1280 * 1280 local MAX_SCALE_IMAGE = 1280 * 1280
local CURL_OPTIONS = { local CURL_OPTIONS = {
@ -55,15 +60,6 @@ local function valid_size(x, scale)
end end
end end
local function apply_denoise1(x)
return reconstruct.image(noise1_model, x)
end
local function apply_denoise2(x)
return reconstruct.image(noise2_model, x)
end
local function apply_scale2x(x)
return reconstruct.scale(scale20_model, 2.0, x)
end
local function cache_url(url) local function cache_url(url)
local hash = md5.sumhexa(url) local hash = md5.sumhexa(url)
local cache_file = path.join(CACHE_DIR, "url_" .. hash) local cache_file = path.join(CACHE_DIR, "url_" .. hash)
@ -91,15 +87,6 @@ local function cache_url(url)
end end
return nil, nil, nil return nil, nil, nil
end end
local function cache_do(cache, x, func)
if path.exists(cache) then
return image.load(cache)
else
x = func(x)
image.save(cache, x)
return x
end
end
local function get_image(req) local function get_image(req)
local file = req:get_argument("file", "") local file = req:get_argument("file", "")
local url = req:get_argument("url", "") local url = req:get_argument("url", "")
@ -114,7 +101,30 @@ local function get_image(req)
end end
return nil, nil, nil return nil, nil, nil
end end
local function convert(x, options)
local cache_file = path.join(CACHE_DIR, options.prefix .. ".png")
if path.exists(cache_file) then
return image.load(cache_file)
else
if options.style == "art" then
if options.method == "scale" then
x = reconstruct.scale(art_scale2_model, 2.0, x)
w2nn.cleanup_model(art_scale2_model)
elseif options.method == "noise1" then
x = reconstruct.image(art_noise1_model, x)
w2nn.cleanup_model(art_noise1_model)
else -- options.method == "noise2"
x = reconstruct.image(art_noise2_model, x)
w2nn.cleanup_model(art_noise2_model)
end
else -- photo
x = reconstruct.scale(photo_scale2_model, 2.0, x)
w2nn.cleanup_model(photo_scale2_model)
end
image.save(cache_file, x)
return x
end
end
local function client_disconnected(handler) local function client_disconnected(handler)
return not(handler.request and return not(handler.request and
handler.request.connection and handler.request.connection and
@ -129,30 +139,28 @@ function APIHandler:post()
self:write("client disconnected") self:write("client disconnected")
return return
end end
local x, alpha, src = get_image(self) local x, alpha, blob = get_image(self)
local scale = tonumber(self:get_argument("scale", "0")) local scale = tonumber(self:get_argument("scale", "0"))
local noise = tonumber(self:get_argument("noise", "0")) local noise = tonumber(self:get_argument("noise", "0"))
local style = self:get_argument("style", "art")
if style ~= "art" then
style = "photo" -- style must be art or photo
end
if x and valid_size(x, scale) then if x and valid_size(x, scale) then
if USE_CACHE and (noise ~= 0 or scale ~= 0) then if (noise ~= 0 or scale ~= 0) then
local hash = md5.sumhexa(src) local hash = md5.sumhexa(blob)
local cache_noise1 = path.join(CACHE_DIR, hash .. "_noise1.png")
local cache_noise2 = path.join(CACHE_DIR, hash .. "_noise2.png")
local cache_scale = path.join(CACHE_DIR, hash .. "_scale.png")
local cache_noise1_scale = path.join(CACHE_DIR, hash .. "_noise1_scale.png")
local cache_noise2_scale = path.join(CACHE_DIR, hash .. "_noise2_scale.png")
if noise == 1 then if noise == 1 then
x = cache_do(cache_noise1, x, apply_denoise1) x = convert(x, {method = "noise1", style = style, prefix = style .. "_noise1_" .. hash})
elseif noise == 2 then elseif noise == 2 then
x = cache_do(cache_noise2, x, apply_denoise2) x = convert(x, {method = "noise2", style = style, prefix = style .. "_noise2_" .. hash})
end end
if scale == 1 or scale == 2 then if scale == 1 or scale == 2 then
if noise == 1 then if noise == 1 then
x = cache_do(cache_noise1_scale, x, apply_scale2x) x = convert(x, {method = "scale", style = style, prefix = style .. "_noise1_scale_" .. hash})
elseif noise == 2 then elseif noise == 2 then
x = cache_do(cache_noise2_scale, x, apply_scale2x) x = convert(x, {method = "scale", style = style, prefix = style .. "_noise2_scale_" .. hash})
else else
x = cache_do(cache_scale, x, apply_scale2x) x = convert(x, {method = "scale", style = style, prefix = style .. "_scale_" .. hash})
end end
if scale == 1 then if scale == 1 then
x = iproc.scale(x, x = iproc.scale(x,
@ -161,23 +169,9 @@ function APIHandler:post()
"Jinc") "Jinc")
end end
end end
elseif noise ~= 0 or scale ~= 0 then
if noise == 1 then
x = apply_denoise1(x)
elseif noise == 2 then
x = apply_denoise2(x)
end
if scale == 1 then
local x16 = {math.floor(x:size(3) * 1.6 + 0.5), math.floor(x:size(2) * 1.6 + 0.5)}
x = apply_scale2x(x)
x = iproc.scale(x, x16[1], x16[2], "Jinc")
elseif scale == 2 then
x = apply_scale2x(x)
end
end end
local name = uuid() .. ".png" local name = uuid() .. ".png"
local blob, len = image_loader.encode_png(x, alpha) local blob, len = image_loader.encode_png(x, alpha)
self:set_header("Content-Disposition", string.format('filename="%s"', name)) self:set_header("Content-Disposition", string.format('filename="%s"', name))
self:set_header("Content-Type", "image/png") self:set_header("Content-Type", "image/png")
self:set_header("Content-Length", string.format("%d", len)) self:set_header("Content-Length", string.format("%d", len))
@ -194,9 +188,9 @@ function APIHandler:post()
collectgarbage() collectgarbage()
end end
local FormHandler = class("FormHandler", turbo.web.RequestHandler) local FormHandler = class("FormHandler", turbo.web.RequestHandler)
local index_ja = file.read("./assets/index.ja.html") local index_ja = file.read(path.join(ROOT, "assets", "index.ja.html"))
local index_ru = file.read("./assets/index.ru.html") local index_ru = file.read(path.join(ROOT, "assets", "index.ru.html"))
local index_en = file.read("./assets/index.html") local index_en = file.read(path.join(ROOT, "assets", "index.html"))
function FormHandler:get() function FormHandler:get()
local lang = self.request.headers:get("Accept-Language") local lang = self.request.headers:get("Accept-Language")
if lang then if lang then
@ -226,9 +220,11 @@ turbo.log.categories = {
local app = turbo.web.Application:new( local app = turbo.web.Application:new(
{ {
{"^/$", FormHandler}, {"^/$", FormHandler},
{"^/index.html", turbo.web.StaticFileHandler, path.join("./assets", "index.html")}, {"^/style.css", turbo.web.StaticFileHandler, path.join(ROOT, "assets", "style.css")},
{"^/index.ja.html", turbo.web.StaticFileHandler, path.join("./assets", "index.ja.html")}, {"^/ui.js", turbo.web.StaticFileHandler, path.join(ROOT, "assets", "ui.js")},
{"^/index.ru.html", turbo.web.StaticFileHandler, path.join("./assets", "index.ru.html")}, {"^/index.html", turbo.web.StaticFileHandler, path.join(ROOT, "assets", "index.html")},
{"^/index.ja.html", turbo.web.StaticFileHandler, path.join(ROOT, "assets", "index.ja.html")},
{"^/index.ru.html", turbo.web.StaticFileHandler, path.join(ROOT, "assets", "index.ru.html")},
{"^/api$", APIHandler}, {"^/api$", APIHandler},
} }
) )