ios - iOS 应用中的 Vize.ai 图像识别

标签 ios image request swift4 image-recognition

我正在尝试实现 Vize.ai使用 Swift 4 在 iOS 应用程序中进行图像识别。

在他们的 documentation这是他们为 Objective C 提供的代码示例:

NSDictionary *headers = @{@"Authorization": @"JWT {your JWT token}", @"Content-Type": @"application/x-www-form-urlencoded", @"Accept": @"text/plain"};
UNIUrlConnection *asyncConnection = [[UNIRest post:^(UNISimpleRequest *request) {
  [request setUrl:@"http://cl-api.vize.ai/{your task ID}?image={path/myimage.png}"];
  [request setHeaders:headers];
}] asundefinedAsync:^(UNIHTTPundefinedResponse *response, NSError *error) {
  NSInteger code = response.code;
  NSDictionary *responseHeaders = response.headers;
  UNIJsonNode *body = response.body;
  NSData *rawBody = response.rawBody;
}];

如您所见,我必须将图像路径传递给请求。在我的应用程序中,用户可以选择分析添加到项目 Assets 文件夹的默认图片或从库中添加/拍照。

这个例子中的图像路径应该是什么?

这是我如何使用 Swift 4 发出请求,我添加到其中的任何图像路径都会给我返回“缺少图像或 url”响应错误:

let headers: HTTPHeaders = [
    "Authorization": "JWT \(jwtToken)",
    "Content-Type": "application/x-www-form-urlencoded",
    "Accept": "text/plain"
]
let url = "https://cl-api.vize.ai/\(taskID)?image=\(imagePath)"
Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
    debugPrint(response)
}

非常感谢任何帮助。谢谢!

最佳答案

所以我设法使用多部分表单数据找到了解决方案。这是它的完整代码。

func getVizeImageAnalysis(image: UIImage) {
        let headers: HTTPHeaders = [
            "Authorization": "JWT \(jwtToken)",
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "text/plain"
        ]
        let url = "https://cl-api.vize.ai/\(taskID)"

        manager.upload(multipartFormData: { multiPartData in
            // Add image
            if let imageData = UIImageJPEGRepresentation(image, 0.8)  {
                multiPartData.append(imageData, withName: "image", fileName: "pickedImage", mimeType: "image/jpeg")
            }

        }, to: url, method: .post, headers: headers, encodingCompletion: {
            encodingResult in
            switch encodingResult {
            case .success(let request, _, _):
                request.responseJSON{ response in
                debugPrint(response)
            }
            case .failure(let encodingError):
                print(encodingError)
            }
        })
    }

关于ios - iOS 应用中的 Vize.ai 图像识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46969941/

相关文章:

ios - 如何在 UITextView 的文本后面动态创建多个按钮

IOS with Rails 后端 Amazon S3 直接上传

javascript - 在页面加载之前使用 jQuery 定位图像

python - 发送 POST 请求的正文

javascript - 在连续的 Ajax 函数中使用相同的变量

ios - Swift 枚举作为函数中的参数

ios - 阻止应用程序进入后台,需要代码才能关闭应用程序

java - 每秒捕获屏幕图像并将其显示在应用程序上是否有效?

javascript - 为什么 Mootools Multibox 中的图像在第一次显示时会有滚动条?

c# - Request.Url.UserInfo 无值