# inferencejs Reference

{% hint style="info" %}
について詳しく学ぶ `inferencejs` 、私たちの web SDK、 [こちら](/roboflow/roboflow-jp/deploy/sdks/web-browser.md)
{% endhint %}

### インストール

このライブラリは、vite、webpack、parcel などの bundler を使用して、ブラウザ内で使用するように設計されています。bundler の設定が済んでいる場合は、次のコマンドを実行してインストールできます。

`npm install inferencejs`

### Getting Started

まず、 `InferenceEngine`を初期化します。これにより、ユーザーインターフェースをブロックせずにモデルをダウンロードして\
実行できるバックグラウンドワーカーが起動します。

```typescript
import { InferenceEngine } from "inferencejs";

const PUBLISHABLE_KEY = "rf_a6cd..."; // Roboflow の自分の publishable key に置き換えてください

const inferEngine = new InferenceEngine();
const workerId = await inferEngine.startWorker("[PROJECT URL SLUG]", [VERSION NUMBER], PUBLISHABLE_KEY);

//モデルに対して推論を実行する
const result = await inferEngine.infer(workerId, img);
```

## API

### InferenceEngine

**`new InferenceEngine()`**

新しい InferenceEngine インスタンスを作成します。

**`startWorker(modelName: string, modelVersion: number, publishableKey: string): Promise<number>`**

指定されたモデルの新しい worker を開始し、 `workerId`. **重要**- `publishableKey` は必須であり、プロジェクト設定フォルダ内の Roboflow から取得できます。

**`infer(workerId: number, img: CVImage | ImageBitmap): Promise<Inference>`**

指定された `workerId`. `img` を使用して worker で画像に対して推論を実行します。 `new CVImage(HTMLImageElement | HTMLVideoElement | ImageBitmap | TFJS.Tensor)` または [`createImageBitmap`](https://developer.mozilla.org/en-US/docs/Web/API/createImageBitmap)

**`stopWorker(workerId: number): Promise<void>`**

指定された worker を停止します。 `workerId`.

### `YOLO Lite` `YOLOv8` `YOLOv5`

使用して推論を行った結果は、 `InferenceEngine` を使用した YOLO Lite、YOLOv8、または YOLOv5 の物体検出モデルでは、次の型の配列になります。

```typescript
type RFObjectDetectionPrediction = {
    class?: string;
    confidence?: number;
    bbox?: {
        x: number;
        y: number;
        width: number;
        height: number;
    };
    color?: string;
};
```

### `GazeDetections`

使用して推論を行った結果は、 `InferenceEngine` の Gaze モデルでは、次の型の配列になります。

```typescript
type GazeDetections = {
    leftEye: { x: number; y: number };
    rightEye: { x: number; y: number };
    yaw: number;
    pitch: number;
}[];
```

**`leftEye.x`**

左目の x 位置。入力画像の幅に対する割合として、0 から 1 の間の浮動小数点数で表されます。

**`leftEye.y`**

左目の y 位置。入力画像の高さに対する割合として、0 から 1 の間の浮動小数点数で表されます。

**`rightEye.x`**

右目の x 位置。入力画像の幅に対する割合として、0 から 1 の間の浮動小数点数で表されます。

**`rightEye.y`**

右目の y 位置。入力画像の高さに対する割合として、0 から 1 の間の浮動小数点数で表されます。

**`yaw`**

視線の yaw。ラジアンで測定されます。

**`pitch`**

視線の pitch。ラジアンで測定されます。

### `CVImage`

コンピュータビジョンのタスクに使用できる画像を表すクラスです。画像を操作したり変換したりするためのさまざまなメソッドを提供します。

#### **Constructor**

その `CVImage(image)` クラスコンストラクタは、このクラスの新しいインスタンスを初期化します。次の型のいずれか 1 つの画像を受け取ります。

* `ImageBitmap`：省略可能な `ImageBitmap` 画像の表現。
* `HTMLImageElement`：省略可能な `HTMLImageElement` 画像の表現。
* `tf.Tensor`：省略可能な `tf.Tensor` 画像の表現。
* `tf.Tensor4D`：省略可能な 4D `tf.Tensor` 画像の表現。

#### **Methods**

**`bitmap()`**

への Promise を返します。これは `ImageBitmap` 画像の表現に解決されます。画像がすでに bitmap である場合は、キャッシュされた bitmap を返します。

**`tensor()`**

への `tf.Tensor` 画像の表現を返します。画像がすでに tensor である場合は、キャッシュされた tensor を返します。

**`tensor4D()`**

への Promise を返します。これは 4D `tf.Tensor` 画像の表現に解決されます。画像がすでに 4D tensor である場合は、キャッシュされた 4D tensor を返します。

**`array()`**

画像の JavaScript 配列表現に解決される Promise を返します。画像がすでに tensor である場合は、tensor を配列に変換します。

**`dims()`**

画像の次元を含む配列を返します。画像が bitmap の場合は `[width, height]`を返します。画像が tensor の場合は、tensor の shape を返します。画像が HTML image element の場合は `[width, height]`.

**`dispose()`**

メモリを解放するために、画像の tensor 表現を破棄します。

**`static fromArray(array: tf.TensorLike)`**

与えられた tensor-like 配列から新しい `CVImage` インスタンスを作成します。


---

# 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/sdks/web-browser/web-inference.js/inferencejs-reference.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.
