【问题标题】:Vize.ai Image Recognition in iOS appiOS 应用中的 Vize.ai 图像识别
【发布时间】:2018-04-08 18:11:47
【问题描述】:

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

在他们的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;
}];

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

此示例中的图像路径应该是什么?

这是我使用 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)
}

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

【问题讨论】:

    标签: ios image request swift4 image-recognition


    【解决方案1】:

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

    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)
                }
            })
        }
    

    【讨论】:

    • 您在使用 Alamofire 吗?如果你是,我会在HTTPHeadersmanager 上收到Use of unresolved identifier error。你知道问题可能是什么吗?我已经使用 CocoaPods 安装了 Alamofire。
    • 另外,jwtToken 是 API 访问令牌,taskID 是任务 UIUD,对吗?当我使用自己的 API 令牌和任务 ID 输入这些占位符时,我收到此错误:Expected a digit after integer literal prefix。你也遇到过这种情况吗?
    • @fi12 是的,我正在使用 Alamofire,请确保初始化管理器:class YourViewController: UIViewController { let manager: SessionManager (...) required init?(coder aDecoder: NSCoder) { let sessionConfiguration = SessionManager.default.session.configuration sessionConfiguration.timeoutIntervalForRequest = 20.0 self.manager = SessionManager(configuration: sessionConfiguration) super.init(coder: aDecoder) } (...) }
    • @fi12 是的,jwtToken 是 API 访问令牌,taskID 是任务 UUID。您是否将它们添加为字符串?
    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 2016-08-10
    • 2012-09-02
    • 1970-01-01
    • 2011-10-08
    • 2010-09-13
    • 1970-01-01
    相关资源
    最近更新 更多