# Classification

{% 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 リクエストを初期化
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` = すべての予測クラスと、それに関連する confidence 値のコレクション
  * `class` = classification のラベル
  * `confidence` = 画像に検出された classification の対象が含まれていることに対するモデルの confidence
* `top` = 最も高い confidence の予測クラス
* `confidence` = 最も高い予測 confidence スコア
* `image_path` = 予測された画像のパス
* `prediction_type` = inference を実行するために使用された model type, `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` = すべての予測クラスと、それに関連する confidence 値のコレクション
  * `class` = classification のラベル
  * `confidence` = 画像に検出された classification の対象が含まれていることに対するモデルの confidence
* `predicted_classes` = モデル予測で返されるすべての classification（ラベル/classes）の一覧を含む配列
* `image_path` = 予測された画像のパス
* `prediction_type` = inference を実行するために使用された model type, `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 | データセットのバージョンを識別する version number です。                                      |

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

| Name     | 種類     | 説明                                           |
| -------- | ------ | -------------------------------------------- |
| api\_key | string | あなたの API キー（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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.roboflow.com/roboflow/roboflow-jp/deploy/serverless/classification.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
