Membuat prediksi individu

Setelah membuat (melatih) model, Anda dapat meminta prediction untuk gambar menggunakan predict prediksi. Metode predict menerapkan label ke gambar Anda berdasarkan objek utama gambar yang diprediksi model Anda.

Prediksi online (individu)

Bagian ini menjelaskan pengiriman file individu untuk anotasi. Permintaan ini akan segera menampilkan respons.

Anda juga dapat mengirim batch file untuk anotasi. Anotasi file batch adalah operasi yang berjalan lama, dan menyimpan hasil dalam bucket Cloud Storage pilihan Anda.

UI web

  1. Buka Vision Dashboard dan klik ikon bola lampu di menu navigasi sebelah kiri untuk menampilkan model yang tersedia.

    Untuk melihat model project lain, pilih project dari menu drop-down di kanan atas panel judul.

  2. Klik baris model yang ingin Anda gunakan untuk memberi label pada gambar.

  3. Klik tab Pengujian & Penggunaan tepat di bawah batang judul.

  4. Klik Upload Gambar untuk mengupload gambar yang ingin Anda beri label.

    updated predict page

REST

Untuk menguji prediksi, Anda harus men-deploy model yang dihosting Cloud terlebih dahulu.

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • project-id: Project ID GCP Anda.
  • model-id: ID model Anda, dari respons saat membuat model. ID adalah elemen terakhir dari nama model Anda. Misalnya:
    • nama model: projects/project-id/locations/location-id/models/IOD4412217016962778756
    • id model: IOD4412217016962778756
  • base64-encoded-image: representasi base64 (string ASCII) dari data gambar biner Anda. String ini akan terlihat seperti string berikut: /9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==. Kunjungi topik mengenkode base64 untuk informasi selengkapnya.

Pertimbangan khusus kolom:

  • scoreThreshold - Nilai antara 0 hingga 1. Hanya dengan skor nilai minimum setidaknya nilai ini yang akan ditampilkan. Nilai defaultnya adalah 0.5.

Metode HTTP dan URL:

POST https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict

Isi JSON permintaan:

{
  "payload": {
    "image": {
      "imageBytes": "BASE64_ENCODED_IMAGE"
    }
  },
  "params": {
    "scoreThreshold": "0.5"
  }
}

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict"

PowerShell

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict" | Select-Object -Expand Content

Output ditampilkan dalam bentuk formulir JSON. Prediksi dari model AutoML Vision Anda terdapat di kolom payload:

  • displayName adalah label objek yang diprediksi oleh model AutoML Vision.
  • score menunjukkan tingkat keyakinan yang diterapkan pada label yang ditentukan pada gambar. Rentangnya mulai dari 0 (tanpa keyakinan) hingga 1 (keyakinan tinggi).
{
  "payload": [
    {
      "annotationSpecId": "7922029656637702144",
      "classification": {
        "score": 0.9960259
      },
      "displayName": "roses"
    }
  ]
}

Go

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan untuk bahasa ini di halaman Library Klien.

import (
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"os"

	automl "cloud.google.com/go/automl/apiv1"
	"cloud.google.com/go/automl/apiv1/automlpb"
)

// visionClassificationPredict does a prediction for image classification.
func visionClassificationPredict(w io.Writer, projectID string, location string, modelID string, filePath string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "ICN123456789..."
	// filePath := "path/to/image.jpg"

	ctx := context.Background()
	client, err := automl.NewPredictionClient(ctx)
	if err != nil {
		return fmt.Errorf("NewPredictionClient: %w", err)
	}
	defer client.Close()

	file, err := os.Open(filePath)
	if err != nil {
		return fmt.Errorf("Open: %w", err)
	}
	defer file.Close()
	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return fmt.Errorf("ReadAll: %w", err)
	}

	req := &automlpb.PredictRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
		Payload: &automlpb.ExamplePayload{
			Payload: &automlpb.ExamplePayload_Image{
				Image: &automlpb.Image{
					Data: &automlpb.Image_ImageBytes{
						ImageBytes: bytes,
					},
				},
			},
		},
		// Params is additional domain-specific parameters.
		Params: map[string]string{
			// score_threshold is used to filter the result.
			"score_threshold": "0.8",
		},
	}

	resp, err := client.Predict(ctx, req)
	if err != nil {
		return fmt.Errorf("Predict: %w", err)
	}

	for _, payload := range resp.GetPayload() {
		fmt.Fprintf(w, "Predicted class name: %v\n", payload.GetDisplayName())
		fmt.Fprintf(w, "Predicted class score: %v\n", payload.GetClassification().GetScore())
	}

	return nil
}

Java

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan untuk bahasa ini di halaman Library Klien.

import com.google.cloud.automl.v1.AnnotationPayload;
import com.google.cloud.automl.v1.ExamplePayload;
import com.google.cloud.automl.v1.Image;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.PredictRequest;
import com.google.cloud.automl.v1.PredictResponse;
import com.google.cloud.automl.v1.PredictionServiceClient;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

class VisionClassificationPredict {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String filePath = "path_to_local_file.jpg";
    predict(projectId, modelId, filePath);
  }

  static void predict(String projectId, String modelId, String filePath) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
      // Get the full path of the model.
      ModelName name = ModelName.of(projectId, "us-central1", modelId);
      ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
      Image image = Image.newBuilder().setImageBytes(content).build();
      ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build();
      PredictRequest predictRequest =
          PredictRequest.newBuilder()
              .setName(name.toString())
              .setPayload(payload)
              .putParams(
                  "score_threshold", "0.8") // [0.0-1.0] Only produce results higher than this value
              .build();

      PredictResponse response = client.predict(predictRequest);

      for (AnnotationPayload annotationPayload : response.getPayloadList()) {
        System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
        System.out.format(
            "Predicted class score: %.2f\n", annotationPayload.getClassification().getScore());
      }
    }
  }
}

Node.js

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan untuk bahasa ini di halaman Library Klien.

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const filePath = 'path_to_local_file.jpg';

// Imports the Google Cloud AutoML library
const {PredictionServiceClient} = require('@google-cloud/automl').v1;
const fs = require('fs');

// Instantiates a client
const client = new PredictionServiceClient();

// Read the file content for translation.
const content = fs.readFileSync(filePath);

async function predict() {
  // Construct request
  // params is additional domain-specific parameters.
  // score_threshold is used to filter the result
  const request = {
    name: client.modelPath(projectId, location, modelId),
    payload: {
      image: {
        imageBytes: content,
      },
    },
  };

  const [response] = await client.predict(request);

  for (const annotationPayload of response.payload) {
    console.log(`Predicted class name: ${annotationPayload.displayName}`);
    console.log(
      `Predicted class score: ${annotationPayload.classification.score}`
    );
  }
}

predict();

Python

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan untuk bahasa ini di halaman Library Klien.

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# model_id = "YOUR_MODEL_ID"
# file_path = "path_to_local_file.jpg"

prediction_client = automl.PredictionServiceClient()

# Get the full path of the model.
model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id)

# Read the file.
with open(file_path, "rb") as content_file:
    content = content_file.read()

image = automl.Image(image_bytes=content)
payload = automl.ExamplePayload(image=image)

# params is additional domain-specific parameters.
# score_threshold is used to filter the result
# https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest
params = {"score_threshold": "0.8"}

request = automl.PredictRequest(name=model_full_id, payload=payload, params=params)
response = prediction_client.predict(request=request)

print("Prediction results:")
for result in response.payload:
    print(f"Predicted class name: {result.display_name}")
    print(f"Predicted class score: {result.classification.score}")

Bahasa tambahan

C# : Ikuti Petunjuk penyiapan C# di halaman library klien, lalu kunjungi dokumentasi referensi AutoML Vision untuk .NET.

PHP : Ikuti Petunjuk penyiapan PHP di halaman library klien, lalu kunjungi dokumentasi referensi AutoML Vision untuk PHP.

Ruby : Ikuti Petunjuk penyiapan Ruby di halaman library klien, lalu kunjungi Dokumentasi referensi AutoML Vision untuk Ruby.