# 分類

{% tabs %}
{% tab title="Python" %}
**ローカル画像とホストされた画像で推論を実行**

依存関係をインストールするには、 `pip install inference-sdk`.

```python
from inference_sdk import InferenceHTTPClient

CLIENT = InferenceHTTPClient(
    api_url="https://classify.roboflow.com",
    api_key="API_KEY"
)

result = CLIENT.infer(your_image.jpg, model_id="vehicle-classification-eapcd/2")
```

{% endtab %}

{% tab title="Javascript" %}
**Node.js**

私たちは [axios](https://github.com/axios/axios) を使って POST リクエストを実行しているので、まず `npm install axios` を実行して依存関係をインストールしてください。

**ローカル画像で推論する**

```
const axios = require("axios");
const fs = require("fs");

const image = fs.readFileSync("YOUR_IMAGE.jpg", {
    encoding: "base64"
});

axios({
    method: "POST",
    url: "https://classify.roboflow.com/your-model/42",
    params: {
        api_key: "YOUR_KEY"
    },
    data: image,
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"
    }
})
.then(function(response) {
    console.log(response.data);
})
.catch(function(error) {
    console.log(error.message);
});
```

{% endtab %}

{% tab title="Swift" %}
**base64 を使用してローカル画像をアップロード**

```swift
import UIKit

// 画像を読み込み、Base64 に変換
let image = UIImage(named: "your-image-path") // アップロードする画像へのパス 例: image.jpg
let imageData = image?.jpegData(compressionQuality: 1)
let fileContent = imageData?.base64EncodedString()
let postData = fileContent!.data(using: .utf8)

// API_KEY、Model、Model Version を使って Inference Server Request を初期化
var request = URLRequest(url: URL(string: "https://classify.roboflow.com/your-model/your-model-version?api_key=YOUR_APIKEY&name=YOUR_IMAGE.jpg")!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData

// POST リクエストを実行
URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
    
    // レスポンスを文字列にパース
    guard let data = data else {
        print(String(describing: error))
        return
    }
    
    // レスポンス文字列を Dictionary に変換
    do {
        let dict = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
    } catch {
        print(error.localizedDescription)
    }
    
    // 文字列レスポンスを出力
    print(String(data: data, encoding: .utf8)!)
}).resume()
```

{% endtab %}
{% endtabs %}

## Response Object Formats

{% tabs %}
{% tab title="Single-Label Classification" %}
**Single-Label Classification**

ホストされたAPI推論ルートは、 `JSON` 予測の配列を含むオブジェクトを返します。各予測には次のプロパティがあります:

* `time` = 画像を処理して予測を返すまでの総時間（秒）
* `image` = 画像に関する情報を保持するオブジェクト `width` と `height`
  * `width` 予測された画像の高さ
  * `height` = 予測された画像の高さ
* `predictions` = 予測に対する、予測されたすべてのクラスとそれぞれの信頼値のコレクション
  * `class` = 分類のラベル
  * `confidence` = 画像に検出された分類の対象が含まれていることに対するモデルの信頼度
* `top` = 最も信頼度の高い予測クラス
* `confidence` = 最も高い予測信頼スコア
* `image_path` = 予測された画像のパス
* `prediction_type` = inference の実行に使用されたモデルタイプ、 `ClassificationModel` この場合

{% code overflow="wrap" %}

```json
// JSONオブジェクトの例
{
  "time": 0.19064618100037478,
  "image": {
    "width": 210,
    "height": 113
  },
  "predictions": [
    {
      "class": "real-image",
      "confidence": 0.7149
    },
    {
      "class": "illustration",
      "confidence": 0.2851
    }
  ],
  "top": "real-image",
  "confidence": 0.7149,
  "image_path": "/cropped-images-1.jpg",
  "prediction_type": "ClassificationModel"
}
```

{% endcode %}
{% endtab %}

{% tab title="Multi-Label Classification" %}
**Multi-Label Classification**

ホストされたAPI推論ルートは、 `JSON` 予測の配列を含むオブジェクトを返します。各予測には次のプロパティがあります:

* `time` = 画像を処理して予測を返すまでの総時間（秒）
* `image` = 画像に関する情報を保持するオブジェクト `width` と `height`
  * `width` 予測された画像の高さ
  * `height` = 予測された画像の高さ
* `predictions` = 予測に対する、予測されたすべてのクラスとそれぞれの信頼値のコレクション
  * `class` = 分類のラベル
  * `confidence` = 画像に検出された分類の対象が含まれていることに対するモデルの信頼度
* `predicted_classes` = モデル予測で返されたすべての分類（ラベル/クラス）の一覧を含む配列
* `image_path` = 予測された画像のパス
* `prediction_type` = inference の実行に使用されたモデルタイプ、 `ClassificationModel` この場合

<pre class="language-json" data-overflow="wrap"><code class="lang-json"><strong>// JSONオブジェクトの例
</strong>{
  "time": 0.19291414400004214,
  "image": {
    "width": 113,
    "height": 210
  },
  "predictions": {
    "dent": {
      "confidence": 0.5253503322601318
    },
    "severe": {
      "confidence": 0.5804202556610107
    }
  },
  "predicted_classes": [
    "dent",
    "severe"
  ],
  "image_path": "/car-model-343.jpg",
  "prediction_type": "ClassificationModel"
}
</code></pre>

{% endtab %}
{% endtabs %}

## API Reference

## Inference API の使用

<mark style="color:緑;">`POST`</mark> `https://classify.roboflow.com/:datasetSlug/:versionNumber`

base64 エンコードされた画像を、モデルのエンドポイントに直接POSTできます。または、画像がすでに別の場所でホストされている場合は、 `image` クエリ文字列内のパラメータとしてURLを渡すこともできます。

#### パスパラメータ

| Name        | 種類     | 説明                                                               |
| ----------- | ------ | ---------------------------------------------------------------- |
| datasetSlug | string | データセット名のURLセーフ版です。メインの project view の URL を見ることで Web UI で確認できます。 |
|             | string | データセットのバージョンを識別するバージョン番号です。                                      |

#### クエリパラメータ

| Name     | 種類     | 説明                                             |
| -------- | ------ | ---------------------------------------------- |
| api\_key | string | あなたの API key（workspace の API settings ページから取得） |

{% tabs %}
{% tab title="200: OK " %}

```json
{
   "predictions":{
      "bird":{
         "confidence":0.5282308459281921
      },
      "cat":{
         "confidence":0.5069406032562256
      },
      "dog":{
         "confidence":0.49514248967170715
      }
   },
   "predicted_classes":[
      "bird",
      "cat"
   ]
}
```

{% endtab %}

{% tab title="403: Forbidden " %}

```json
{
   "message":"Forbidden"
}j
```

{% endtab %}
{% endtabs %}
