如何从Ruby中的GCP Aiplatform获得图像分类预测?

发布于 2025-02-12 03:11:20 字数 1198 浏览 3 评论 0原文

我是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;
};

,但是我有以下错误:无效的类型实例可以分配给combessage字段'instances'

我读了文档,但对于ruby sdk来说,这有点轻。 参数还可以,在此处的JS示例: https://github.com/googleapis/nodejs-ai-platform/blob/main/main/samples/predict-image-image-object-detection.js 我正在与这些参数一起工作,

我做错了什么?

I'm new with ruby and I want to use GCP AIPlatform but I'm struggeling with the payload.

So far, I have :

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

Here is my proto

message Instance {
  required bytes content = 1;
};

But I have the following error : Invalid type Instance to assign to submessage field 'instances'

I read the documentation but for ruby SDK it's a bit light.
The parameters are OK, the JS example here : https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/predict-image-object-detection.js is working with those parameters

What am I doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2025-02-19 03:11:20

我管理了

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对我来说很丑陋,但它起作用了

I managed it

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

The use of the Google::Protobuf::Value looks ugly to me but it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文