如何在ruby中实现GCP AIPlatform的图像分类预测?



我是ruby的新手,我想使用GCP AIPlatform,但我正在与有效负载作斗争。

到目前为止,我有:

client = ::Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|
config.endpoint = "#{location}-aiplatform.googleapis.com"
end
img = File.open(imgPath, 'rb') do |img|
'data:image/png;base64,' + Base64.strict_encode64(img.read)
end
instance = Instance.new(:content => img)
request = Google::Cloud::AIPlatform::V1::PredictRequest.new(
endpoint: "projects/#{project}/locations/#{location}/endpoints/#{endpoint}",
instances: [instance]
)
result = client.predict request
p result

这是我的原型

message Instance {
required bytes content = 1;
};

但是我有以下错误:Invalid type Instance to assign to submessage field 'instances'

我读了文档,但对于ruby SDK,它有点轻。参数是OK的,这里的JS示例:https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/predict-image-object-detection.js正在使用这些参数

我做错了什么?

我成功了

client = Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|
config.endpoint = "#{location}-aiplatform.googleapis.com"
end
img = File.open(imgPath, 'rb') do |img|
Base64.strict_encode64(img.read)
end
instance = Google::Protobuf::Value.new(:struct_value => {:fields => {
:content => {:string_value => img}
}})
endpoint = "projects/#{project}/locations/#{location}/endpoints/#{endpoint}"

request = Google::Cloud::AIPlatform::V1::PredictRequest.new(
endpoint: endpoint,
instances: [instance]
)
result = client.predict request
p result

Google::Protobuf::Value的使用对我来说看起来很丑,但它是有效的

相关内容

  • 没有找到相关文章

最新更新