LoginSignup
93

More than 1 year has passed since last update.

posted at

updated at

Organization

画像を高画質化・拡大・ノイズ除去できる「waifu2x」のAPIを作ってみた

waifu2x-api.png


こんにちは、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のシステム構成および処理の流れは以下の図のようになります。

waifu2x-multiのシステム構成図・処理の流れ

また、画像ではなく、動画(アニメーションGIFも含む)の処理は以下の図の通りです。

waifu2xでの動画やアニメーション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
・AIメーカー
・文字起こし
・字幕起こし

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
93