こんにちは、2z(Twitter: @2zn01 )です。
今回は画像を高画質化・拡大・ノイズ除去できる「waifu2x」のAPIを作ってみましたので、ご紹介いたします。
作ったもの
waifu2xというサービス/OSS(オープンソースソフトウェア)はご存知でしょうか?
簡単にいえば、AI(人工知能)を使って小さな画像を綺麗に拡大するサービス/OSSです。
公式のwaifu2xは以下のURLでサービス提供されています。
http://waifu2x.udp.jp/
また、GitHubでプログラムもオープンソースで公開されています。
https://github.com/nagadomi/waifu2x
私が作ったものは、「waifu2x」のOSS(オープンソースソフトウェア)へ以下の機能を追加した、公式とは別の派生サービスになります。
- 1. 複数ファイルのアップロード・拡大機能
- 2. 動画の拡大機能
- 3. 専有サーバによる高速な拡大処理
- 4. サイズ制限の緩和
こちらは以下のURLで公開しています。
■waifu2x-multi
https://waifu2x.me/
■waifu2x-multi Pro(有料版)
https://mng.waifu2x.me/
waifu2x-multiのシステム構成および処理の流れは以下の図のようになります。
また、画像ではなく、動画(アニメーションGIFも含む)の処理は以下の図の通りです。
waifu2xで画像を高速に変換するには、GPUサーバが必要となるため、別途インスタンス(サーバ)を立てる仕組みとしています。
今回はこの「waifu2x」を外部のプログラムから利用できるようにAPIを作ってみました。
本記事では作ったwaifu2xのAPIについて紹介していきます。
なお、APIキーなどについては、以下のページよりご確認ください。
https://mng.waifu2x.me/docs/api/
インスタンス一覧API
インスタンス(サーバ)の一覧を取得するAPIです。
HTTPリクエスト
GET https://mng.waifu2x.me/api/instance/list
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
apikey | string | APIキーを指定してください(必須) |
CURL Example
curl "https://mng.waifu2x.me/api/instance/list?apikey={$apikey}"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/instance/list?apikey={$apikey}",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :get, url: 'https://mng.waifu2x.me/api/instance/list?apikey={$apikey}')
puts response
Python Example
import requests
response = requests.get('https://mng.waifu2x.me/api/instance/list?apikey={$apikey}')
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"total": "1",
"instances": [
{
"id": "0",
"name": "GRID K520 x 1",
"type": "GRID K520 x 1 (Require $0.03 credit / 1 minute)",
"num": "1",
"status": "stopped",
"run_datetime": "2018-04-01 00:00:00",
"expire_datetime": "2018-04-02 00:00:00",
"reg_datetime": "2018-04-01 00:00:00",
"up_datetime": "2018-04-01 00:00:00"
}
]
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
インスタンスステータス取得API
インスタンス(サーバ)の一覧を取得するAPIです。
HTTPリクエスト
GET https://mng.waifu2x.me/api/instance/status
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
id | integer | インスタンスのIDを指定してください(必須) |
apikey | string | APIキーを指定してください(必須) |
CURL Example
curl "https://mng.waifu2x.me/api/instance/status?id={$id}&apikey={$apikey}"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/instance/status?id={$id}&apikey={$apikey}",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :get, url: 'https://mng.waifu2x.me/api/instance/status?id={$id}&apikey={$apikey}')
puts response
Python Example
import requests
response = requests.get('https://mng.waifu2x.me/api/instance/status?id={$id}&apikey={$apikey}')
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"instance": {
"id": "0",
"name": "GRID K520 x 1",
"type": "GRID K520 x 1 (Require $0.03 credit / 1 minute)",
"num": "1",
"status": "stopped",
"run_datetime": "2018-04-01 00:00:00",
"expire_datetime": "2018-04-02 00:00:00",
"reg_datetime": "2018-04-01 00:00:00",
"up_datetime": "2018-04-01 00:00:00"
},
"enabledRunning": true
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
インスタンス起動API
インスタンス(サーバ)を起動するためのAPIです。
HTTPリクエスト
POST https://mng.waifu2x.me/api/instance/start
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
id | integer | インスタンスのIDを指定してください(必須) |
apikey | string | APIキーを指定してください(必須) |
CURL Example
curl -X POST -F id={$id} -F apikey={$apikey} "https://mng.waifu2x.me/api/instance/start"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/instance/start",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'id' => $id,
'apikey' => $apikey,
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :post, url: 'https://mng.waifu2x.me/api/instance/start',
payload: {
'id' => id,
'apikey' => apikey,
}
)
puts response
Python Example
import requests
response = requests.post(
'https://mng.waifu2x.me/api/instance/start',
data={
'id': id,
'apikey': apikey,
}
)
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"status": "standby"
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
インスタンスリセットAPI
インスタンス(サーバ)上のwaifu2xのプロセスをリセットするためのAPIです。
HTTPリクエスト
POST https://mng.waifu2x.me/api/instance/reset
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
id | integer | インスタンスのIDを指定してください(必須) |
apikey | string | APIキーを指定してください(必須) |
CURL Example
curl -X POST -F id={$id} -F apikey={$apikey} "https://mng.waifu2x.me/api/instance/reset"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/instance/reset",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'id' => $id,
'apikey' => $apikey,
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :post, url: 'https://mng.waifu2x.me/api/instance/reset',
payload: {
'id' => id,
'apikey' => apikey,
}
)
puts response
Python Example
import requests
response = requests.post(
'https://mng.waifu2x.me/api/instance/reset',
data={
'id': id,
'apikey': apikey,
}
)
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"status": "standby"
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
インスタンス停止API
インスタンス(サーバ)を停止するためのAPIです。
HTTPリクエスト
POST https://mng.waifu2x.me/api/instance/stop
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
id | integer | インスタンスのIDを指定してください(必須) |
apikey | string | APIキーを指定してください(必須) |
CURL Example
curl -X POST -F id={$id} -F apikey={$apikey} "https://mng.waifu2x.me/api/instance/stop"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/instance/stop",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'id' => $id,
'apikey' => $apikey,
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :post, url: 'https://mng.waifu2x.me/api/instance/stop',
payload: {
'id' => id,
'apikey' => apikey,
}
)
puts response
Python Example
import requests
response = requests.post(
'https://mng.waifu2x.me/api/instance/stop',
data={
'id': id,
'apikey': apikey,
}
)
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"status": "stopped"
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
画像ファイル変換API
画像ファイルを拡大・ノイズ除去するためのAPIです。
HTTPリクエスト
POST https://mng.waifu2x.me/api/file/convert
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
id | integer | インスタンスのIDを指定してください(必須) |
apikey | string | APIキーを指定してください(必須) |
file | binary | 変換対象の画像ファイルを指定してください(必須) |
style | string | 変換対象の画像ファイルのスタイルを以下から指定してください(必須) ・イラスト: "art" ・写真: "photo" |
noise | integer | 画像変換時のJPEGノイズ除去の強さを以下から指定してください(必須) ・除去しない: "-1" ・低め: "0" ・普通: "1" ・高め: "2" ・最高: "3" |
scale | integer | 画像変換時の拡大倍率を 0~10 の間で指定してください(必須) 0または1が指定された場合は拡大しません |
comp | integer | 画像変換時の圧縮率を 0~10 の間で指定してください(必須) 0が指定された場合は圧縮しません |
CURL Example
curl -X POST -F id={$id} -F apikey={$apikey} -F style={$style} -F noise={$noise} -F scale={$scale} -F comp={$comp} -F "file=@{$filepath}" "https://mng.waifu2x.me/api/file/convert"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/file/convert",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'id' => $id,
'apikey' => $apikey,
'style' => $style,
'noise' => $noise,
'scale' => $scale,
'comp' => $comp,
'file' => new CURLFile($filepath),
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :post, url: 'https://mng.waifu2x.me/api/file/convert',
payload: {
'id' => id,
'apikey' => apikey,
'style' => style,
'noise' => noise,
'scale' => scale,
'comp' => comp,
'file' => File.new(filepath),
}
)
puts response
Python Example
import requests
response = requests.post(
'https://mng.waifu2x.me/api/file/convert',
data={
'id': id,
'apikey': apikey,
'style': style,
'noise': noise,
'scale': scale,
'comp': comp
},
files={
'file': open(filepath, 'rb')
}
)
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"id": "0",
"src": "https://mng.waifu2x.me/fixtures/0/1/2/sample.png"
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
画像ファイル一覧API
拡大・ノイズ除去した画像ファイルの一覧を取得するためのAPIです。
HTTPリクエスト
GET https://mng.waifu2x.me/api/file/list
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
id | integer | インスタンスのIDを指定してください(必須) |
apikey | string | APIキーを指定してください(必須) |
CURL Example
curl "https://mng.waifu2x.me/api/file/list?id={$id}&apikey={$apikey}"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/file/list?id={$id}&apikey={$apikey}",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :get, url: 'https://mng.waifu2x.me/api/file/list?id={$id}&apikey={$apikey}')
puts response
Python Example
import requests
response = requests.get('https://mng.waifu2x.me/api/file/list?id={$id}&apikey={$apikey}')
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"total": 1,
"files": [
{
"id": "0",
"name": "sample.png",
"status": "waiting",
"size": 123456,
"type": "image/png",
"src": "https://mng.waifu2x.me/fixtures/0/1/2/sample.png",
"params": {
"id": "0",
"fpath": "0/1/2/sample.png",
"method": "convert",
"style": "art",
"noise": "0",
"scale": "2",
"comp": "0",
"mail": null
},
"reg_datetime": "2018-04-01 00:00:00",
"up_datetime": "2018-04-01 00:00:00"
}
]
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
画像ファイルステータス取得API
拡大・ノイズ除去した画像ファイルのステータス(処理状況)を取得するためのAPIです。
HTTPリクエスト
GET https://mng.waifu2x.me/api/file/status
クエリパラメータ
パラメータ | 型 | 説明 |
---|---|---|
id | integer | 画像ファイルのIDを指定してください(必須) |
apikey | string | APIキーを指定してください(必須) |
CURL Example
curl "https://mng.waifu2x.me/api/file/status?id={$id}&apikey={$apikey}"
PHP Example
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://mng.waifu2x.me/api/file/status?id={$id}&apikey={$apikey}",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
curl_close($ch);
Ruby Example
require 'rest_client'
response = RestClient::Request.execute(method: :get, url: 'https://mng.waifu2x.me/api/file/status?id={$id}&apikey={$apikey}')
puts response
Python Example
import requests
response = requests.get('https://mng.waifu2x.me/api/file/status?id={$id}&apikey={$apikey}')
print(response.json())
成功時のレスポンス
JSON形式
{
"state": 1,
"file": {
"id": "0",
"name": "sample.png",
"status": "running",
"size": 123456,
"type": "image/png",
"src": "https://mng.waifu2x.me/fixtures/0/1/2/sample.png",
"params": {
"id": "74",
"fpath": "0/1/2/sample.png",
"method": "convert",
"style": "art",
"noise": "0",
"scale": "2",
"comp": "0",
"mail": null
},
"reg_datetime": "2018-04-01 00:00:00",
"up_datetime": "2018-04-01 00:00:00"
}
}
失敗時のレスポンス
JSON形式
{
"state": 0,
"message":"Invalid access."
}
APIの活用例
画像を取扱っているアプリやwebサイト、メディアサイトなどで事前に画像を高解像度化・高画質化する際に活用することができるかと思います。
特に画像が低解像度のものしかない場合だったり、JPEGノイズがある場合などは、特に効果が発揮されます。
最後に
画像の高画質化・拡大・ノイズ除去をしたいときは、ぜひwaifu2x-multiをお試しください!
■waifu2x-multi
https://waifu2x.me/
■waifu2x-multi Pro(有料版)
https://mng.waifu2x.me/
少しでも興味をもって頂けましたら、ぜひ以下のアカウントをフォローしたり、以下のツイートをいいねやリツイートで応援して頂けると嬉しいです!
・waifu2x-multi
※今さらリリース報告です!
— 2z@みんなのAI「AIメーカー」開発中 (@2zn01) 2018年8月26日
画像をきれいに拡大する「waifu2x」というサービス/OSSがあります!
一つ一つの画像を指定して変換するのが面倒なので、複数画像のアップロードに対応し、一括で変換できるサービスを作りました!!
(2016年2月に)
ぜひ使ってみてください!https://t.co/REOo3XOEgV pic.twitter.com/yTUh63Nvmw
※今さらリリース報告の第2弾です!
— 2z@みんなのAI「AIメーカー」開発中 (@2zn01) 2018年8月26日
画像をきれいに拡大する「waifu2x」ですが、動画対応させてリリースしました!!
(2018年2月ぐらいに)
動画のコマごとに拡大するため、めっちゃ時間かかりますが、このプレビューではGPUサーバを10台同時に立てて並列処理させてます!https://t.co/cCDWUcaS2X pic.twitter.com/mSID2DjUE4
・AIメーカー
今話題のAIをweb上で誰でも気軽に作れる「AIメーカー」を開発しました!
— 2z@AIメーカー (@2zn01) 2018年7月19日
①AIに覚えさせたいタグを入力
②タグから自動で画像データを収集
③AIがデータから学習
の3ステップで誰でも簡単にAIを作れます!
動画では手相占いのAIに挑戦!
みんなもAIを作って遊んでみてね!https://t.co/66DFU7GRZ2 pic.twitter.com/ie1LmioyA1
・文字起こし
みんな~!「AIメーカー」で文字起こし機能をリリースしたよー!
— 2z@みんなのAI「AIメーカー」開発中 (@2zn01) 2018年8月18日
以下の方法で簡単に文字起こしを試せるので、ぜひ使ってみて~!!
📺YouTubeから文字起こし
🔗画像、音声、動画から文字起こし
🎙️録音で文字起こし
AIをうまく使って少しでも面倒な作業から解放だぁ~!https://t.co/qo13Wo6Yli pic.twitter.com/gsRigVROnK
・字幕起こし
動画をアップすると、動画内の音声からAIが自動でテロップをつけてくれる「テロップメーカー」というサービスを作りました!🎉https://t.co/d4IMrfFPzS
— 2z / AIメーカー開発者 (@2zn01) May 17, 2020
YouTuber、VTuberの方、動画のテロップ入れが面倒な方はぜひ使ってみてください!✨
テロップは編集可能で字幕ファイルのダウンロードもできます