无法将图像参数传递给Flutter中的gfpgan/api:输入验证错误422

huangapple go评论92阅读模式
英文:

Unable to Pass Image Parameter to replicate gfpgan/api in Flutter: Input Validation Error 422

问题

我正在将来自 replicate 的 gfpgan/api 集成到我的 Flutter 应用程序中,以允许用户增强他们的图像。然而,我在发送请求体中的图像方面遇到了问题。我已经尝试了 API 文档中提到的多种方法,但似乎都不起作用。

以下是我尝试过的方法:

  1. 直接使用图像文件路径:
final predictionUrl = 'https://api.replicate.com/v1/predictions';
final imgValue = imageFile != null ? Uri.file(imageFile.path).toString() : "";
print('img value: $imgValue');
final predictionData = {
  'version': modelVersion,
  'input': {'img': imgValue},
};

我测试了生成的路径,看起来是这样的:file:///C:/Users/pc/Pictures/Intropic.png

  1. 直接传递图像文件路径。
  2. 将图像文件编码为 base64 并将其传递为字符串。
  3. 以流的形式传递图像。

不幸的是,所有这些方法都导致以下错误:

Prediction request failed: 422
body: {"detail":"- input.img: Does not match format 'uri'\n","status":422,"title":"Input validation failed","invalid_fields":[{"type":…"}

我还尝试了另一种方法,成功地生成了使用 base64 编码的图像数据的输入体参数:

var input = Uri.dataFromString(base64Encode(fileBytes!), mimeType: 'application/octet-stream').toString();

然而,这种方法引发了不同的异常:

Prediction input failed validation: {"detail":[{"loc":["body","input","img"],"msg":"not enough values to unpack (expected 2, got…", "status": "failed"}

我非常感谢您对如何正确传递请求中的输入图像参数的任何指导或见解。

提前感谢您的帮助!

英文:

I am integrating the gfpgan/api from replicate (Official documentation : https://replicate.com/tencentarc/gfpgan) into my Flutter application to allow users to enhance their images. However, I'm encountering issues with sending the image in the request body. I have tried multiple approaches mentioned in the API documentation, but none of them seem to work.

Here are the methods I've tried:

  1. Using the image file path directly:
final predictionUrl = 'https://api.replicate.com/v1/predictions';
final imgValue = imageFile != null ? Uri.file(imageFile.path).toString() : "";
print('img value: $imgValue');
final predictionData = {
  'version': modelVersion,
  'input': {'img': imgValue},
};

I tested the generated path and it looks like: file:///C:/Users/pc/Pictures/Intropic.png.

  1. Passing the image file path directly.
  2. Encoding the image file as base64 and passing it as a string.
  3. Passing the image as a stream.

Unfortunately, all of these methods result in the following error:

Prediction request failed: 422
body: {"detail":"- input.img: Does not match format 'uri'\n","status":422,"title":"Input validation failed","invalid_fields":[{"type":…"}

I have also tried a different approach where I successfully generate the input body parameter using the base64-encoded image data:

var input = Uri.dataFromString(base64Encode(fileBytes!), mimeType: 'application/octet-stream').toString();

However, this method throws a different exception:

Prediction input failed validation: {"detail":[{"loc":["body","input","img"],"msg":"not enough values to unpack (expected 2, got…", "status": "failed"}

I would greatly appreciate any guidance or insights into the correct way to pass the input image parameter in the request.

Thank you in advance for your help!

答案1

得分: 0

以下是翻译好的部分:

问题的关键在于我们如何在使用来自 Replicate 的 gfpgan/api 时将图像传递到请求主体中。要正确发送图像,可以采用以下方法:

  1. 首先确保您有要发送的图像文件,并已导入必要的库:
import 'dart:convert';
import 'dart:io';
  1. 将图像文件读取为字节并将其编码为 base64 字符串。这部分您已经在您的代码中正确完成:
if (imageFile != null) {
    final fileBytes = await imageFile.readAsBytes();
    final base64Image = base64Encode(fileBytes);
    imgValue = 'data:image/jpeg;base64,$base64Image';
}

此代码将读取图像文件,将其编码为 base64,并构建了一个数据 URI(imgValue),您可以在请求中使用。

  1. 现在,正确构建您的 predictionData JSON 对象,确保提供 img 键和 imgValue
final predictionUrl = 'https://api.replicate.com/v1/predictions';
final predictionData = {
    'version': modelVersion,
    'input': {'img': imgValue},
};
英文:
The issue we were facing here was related to how we passed the image in the request body when using the gfpgan/api from replicate. To send the image correctly, we can follow this approach:

1. First, ensure that you have the image file you want to send, and you've imported the necessary libraries:

```
    import 'dart:convert';
    import 'dart:io';
```

   

2. Read the image file as bytes and encode it as a base64 string. This is the part you've already done correctly in your code:

   
```

if (imageFile != null) {
     final fileBytes = await imageFile.readAsBytes();
     final base64Image = base64Encode(fileBytes);
     imgValue = 'data:image/jpeg;base64,$base64Image';
   }

```

   

   This code reads the image file, encodes it as base64, and constructs a data URI (`imgValue`) that you can use in the request.

3. Now, construct your predictionData JSON object correctly, ensuring you provide the `img` key with the `imgValue`:

   
       final predictionUrl = 'https://api.replicate.com/v1/predictions';
       final predictionData = {
         'version': modelVersion,
         'input': {'img': imgValue},
       };

huangapple
  • 本文由 发表于 2023年6月25日 23:52:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定