AYohannes
AYohannes

Reputation: 625

using a local image from the device in order to classify it successfully

I am writing a visual recognition application that uses VisualRecognition.classify in order to classify images. I have configured my Swift environment and haven't been able to classify images when including a URL from the internet:

I have now created an application that uses the camera and photo library to allow users to take photos and have them classified. I am running into issues when passing along an fileURL from the device to the VisualRecognition service though.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {

        imageView.image = image
        imagePicker.dismiss(animated: true, completion: nil)

        let visualRecongnition = VisualRecognition(version: version, apiKey: apiKey)

        let imageData = image.jpegData(compressionQuality: 0.01)

        let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        let fileURL = documentURL.appendingPathComponent("tempImage.jpg")

        try? imageData?.write(to: fileURL, options: [])


        visualRecongnition.classify(imageFile: fileURL, success: { (classifiedImages) in
            print(classifiedImages)
        }) // getting error here " Missing argument for parameter 'completionHandler' in call"

    }else {

        print("There was an error picking Image")
    }
}

I have even attempted to include the NSURL directly into the classify call as I have done with the working external URL, but still run into the same error. Would really like to see how to use a local image from the device in order to classify it successfully

Upvotes: 0

Views: 120

Answers (2)

matt
matt

Reputation: 534903

The problem is that your call to classify does not correspond to the signature of the classify method. In this line:

visualRecongnition.classify(imageFile: fileURL, success: { (classifiedImages) in

change success to completionHandler, and add a second parameter in the closure (even if you ignore it), like this:

visualRecongnition.classify(imageFile: fileURL, completionHandler: { classifiedImages,_ in

Upvotes: 1

Jeshua Lacock
Jeshua Lacock

Reputation: 6668

You must first request permission to use the camera and library. Open your Info.plist file in Source code mode, and add the following lines:

<key>NSCameraUsageDescription</key>
<string>Ask for permission to use camera.</string>

<key>NSPhotoLibraryUsageDescription</key>
<string>Ask for permission to use Photo Library</string>

If you also want to be able to write images to the Camera Roll add this too:

<key>NSPhotoLibraryAddUsageDescription</key>
<string>Ask for permission to save images to Photo library</string>

Upvotes: 0

Related Questions