How to Build an AR Object Segmentation and Foreground Extraction Application for iOS [Tutorial]
Tags: tutorial, beginners, ios, augmentedreality
Published: 2021-03-04 22:57:50

Looking to build an augmented reality (AR) Object Segmentation and Foreground Extraction Application for iOS (Swift/Xcode)? Use this foreground extraction iOS application that let's the user to select an image, extract foreground and upload the image object to the echoAR console. The full demo can also be found on echoAR's [GitHub](https://github.com/echoARxyz/iOS-echoAR-demo-Object-Segmentation).

![1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614897764000/7HhaIJNTM.png)

# Register

If you don't have an echoAR API key yet, make sure to register for FREE at [echoAR](https://console.echoar.xyz/#/auth/register).

# Installation

* Clone the project from the github repository.
* Open up Xcode and select 'open an existing project'.
* Connect an IOS Device and build the project.

# Usage
* Open the application and you will see the home screen.

![2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614897777246/IDO-Dorfa.png)

* There are two options at the top-right corner of the navigation bar.: (1) Photogallery: If you want to choose image from photo-gallery, and (2) Camera: If you choose to take an new image.
* After choosing an image either from camera or photogallery, click "Begin Button" to start foreground extraction


![3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614897803863/j1Tyr1W_L.png)

* Original Image will be processed to extract the foreground.

![4.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614897812135/z9kqKhh5v.png)


* If you wish to upload image to the echoAR console, click on the upload button at the top left corner. There will be a prompt to enter API key.

![5.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614897829396/lVdXBvNBu.png)

* After entering API-Key, enter the name of the image and click on Upload.


![6.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614897840747/tRxlG3aUT.png)

* Image will be available at the console and can be used for different projects.


![7.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614897862025/XCFSqDyEl.png)

# Creating APIRequest

If you want to add image upload functionality to your custom application, add *APIRequest.swift* and *Media.swift* to your project folder.To make an http-post request create an *APIRequest* object:

     let postRequest = APIRequest()

To send the post request use *send()* instance method of *APIRequest*, which accepts these parameters:
* *image* (type: UIimage): The UIImage you need to upload to the console
* *imageName* (type: String): The name of the image
* *echoARApiKey* (type: String) : Your API key
* *completion* (type: closure): Escaping Closure which is passed as an arguement to the function but is called after function returns. It return Result type that has two cases: success and failure, where the success case will return a string and the failure case will be some sort of API Error.

        let postRequest = APIRequest()
        postRequest.send(imageToPost: UIimage,fileName: String, APIKey: String, completion: {
            result in switch result {
            case .success(_):
                print("Success Uploading")
            case .failure(.incorrectKeyProblem):
                self.createErrorAlert("Incorrect Key", "Please Check key and Try again")
                    print("Incorrect Key")
            case .failure(let error):
                print("Error Occurred \(error)")
    
                }
            })   

Post Request performs *multipart/form-data* httpRequest, where key-value pair are seperated with the randomnly generated boundary string.

Post Request only works for *target_type = 2*, *hologram_type = 1 *and image is sent as *file_image_hologram*.

# APIErrors:

Closure which is passed as an *send(_)* request return *.success*or *.failure*, where .failure is type of *APIError*:
* responseProblem: Problem with the API Response
* decodingProblem: Error decoding API Response data
* incorrectKeyProblem: Incorrect API Key


![8.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614898360024/dR8nNfa2-.png)

![9.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614898361722/qgvoUVXgV.png)

# Learn more
Refer to our [documentation](https://docs.echoar.xyz/unity/) to learn more about how to use Unity, AR Foundation, and echoAR.

# Support

Feel free to reach out at support@echoAR.xyz or join our [support channel on Slack] (https://join.slack.com/t/echoar/shared_invite/enQtNTg4NjI5NjM3OTc1LWU1M2M2MTNlNTM3NGY1YTUxYmY3ZDNjNTc3YjA5M2QyNGZiOTgzMjVmZWZmZmFjNGJjYTcxZjhhNzk3YjNhNjE).

----------
> echoAR (http://www.echoAR.xyz; Techstars '19) is a cloud platform for augmented reality (AR) and virtual reality (VR) that provides tools and server-side infrastructure to help developers & companies quickly build and deploy AR/VR apps and experiences

![10.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614898418691/MJsJ_nkW5.png)