Classification
Roboflow でホストされている分類モデルで推論を実行します。
Node.js
当社は 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);
});ローカル画像を base64 でアップロードする
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、モデル、モデルバージョンで推論サーバーリクエストを初期化
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
}
// レスポンス文字列を辞書に変換
do {
let dict = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
// 文字列レスポンスを出力
print(String(data: data, encoding: .utf8)!)
}).resume()レスポンスオブジェクトの形式
Single-Label Classification
ホストされたAPI推論ルートは次を返します JSON 配列 predictions を含むオブジェクトを返します。各 prediction は以下のプロパティを持ちます:
time= 画像を処理して予測を返すのにかかる合計時間(秒)image= 画像に関する情報を保持するオブジェクトwidthおよびheightwidth予測された画像の高さheight= 予測された画像の高さ
predictions= 予測に対するすべての予測クラスとそれらに関連する信頼度の集合class= 分類のラベルconfidence= 画像に検出された分類のオブジェクトが含まれているというモデルの信頼度
top= 最も高い信頼度の予測クラスconfidence= 最も高い予測信頼度スコアimage_path= 予測された画像のパスprediction_type= 推論に使用されたモデルのタイプ、ClassificationModelこの場合
// 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"
}Multi-Label Classification
ホストされたAPI推論ルートは次を返します JSON 配列 predictions を含むオブジェクトを返します。各 prediction は以下のプロパティを持ちます:
time= 画像を処理して予測を返すのにかかる合計時間(秒)image= 画像に関する情報を保持するオブジェクトwidthおよびheightwidth予測された画像の高さheight= 予測された画像の高さ
predictions= 予測に対するすべての予測クラスとそれらに関連する信頼度の集合class= 分類のラベルconfidence= 画像に検出された分類のオブジェクトが含まれているというモデルの信頼度
predicted_classes= モデル予測で返されるすべての分類(ラベル/クラス)のリストを含む配列image_path= 予測された画像のパスprediction_type= 推論に使用されたモデルのタイプ、ClassificationModelこの場合
// JSONオブジェクトの例
{
"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"
}APIリファレンス
Inference APIの使用方法
POST https://classify.roboflow.com/:datasetSlug/:versionNumber
base64エンコードされた画像を直接モデルエンドポイントにPOSTできます。あるいは、画像が既に別の場所にホストされている場合は、クエリ文字列の image パラメータとしてURLを渡すことができます。
パスパラメータ
datasetSlug
string
URL セーフなデータセット名。メインのプロジェクトビューの URL を見てウェブ UI で確認できます。
string
データセットのバージョンを識別するバージョン番号。
Query Parameters
api_key
string
あなたの API キー(ワークスペースの API 設定ページから取得)
{
"predictions":{
"bird":{
"confidence":0.5282308459281921
},
"cat":{
"confidence":0.5069406032562256
},
"dog":{
"confidence":0.49514248967170715
}
},
"predicted_classes":[
"bird",
"cat"
]
}{
"message":"Forbidden"
}jLast updated
Was this helpful?