{"id":20058640,"url":"https://github.com/roboflow/roboflow-swift","last_synced_at":"2026-04-02T01:53:55.835Z","repository":{"id":173112810,"uuid":"649467241","full_name":"roboflow/roboflow-swift","owner":"roboflow","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-25T19:10:42.000Z","size":5146,"stargazers_count":9,"open_issues_count":1,"forks_count":5,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-30T20:50:53.401Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/roboflow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-04T23:31:12.000Z","updated_at":"2025-04-18T18:57:20.000Z","dependencies_parsed_at":"2024-11-13T13:02:47.144Z","dependency_job_id":null,"html_url":"https://github.com/roboflow/roboflow-swift","commit_stats":null,"previous_names":["roboflow/roboflow-swift"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roboflow%2Froboflow-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roboflow%2Froboflow-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roboflow%2Froboflow-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roboflow%2Froboflow-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roboflow","download_url":"https://codeload.github.com/roboflow/roboflow-swift/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252514975,"owners_count":21760479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T13:02:40.395Z","updated_at":"2026-04-02T01:53:55.826Z","avatar_url":"https://github.com/roboflow.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# roboflow-swift-sdk\t\n\n\u003cp align=\"center\"\u003e\n    \u003c/br\u003e\n    \u003cimg width=\"100\" src=\"https://github.com/roboflow-ai/notebooks/raw/main/assets/roboflow_logomark_color.svg\" alt=\"roboflow logo\"\u003e\n    \u003c/br\u003e\n\u003c/p\u003e\n\nThis is the source code for the Roboflow Swift SDK. It allows you to run [Object Detection](https://blog.roboflow.com/object-detection/) or [Instance Segmentation](https://blog.roboflow.com/instance-segmentation/) models locally on your iOS device that you have trained or have been trained on [Roboflow Universe](https://universe.roboflow.com) by others. The SDK pulls down the CoreML version of the trained model and caches it lcoally for running inference on the edge. \n\n\n\n## Getting Started \n\nTo get started, import Roboflow into your project: \n\n`import Roboflow`\n\nand create an instance of `RoboflowMobile` that's initialzied with your API key: \n\n`let rf = RoboflowMobile(apiKey: API_KEY)`\n\nYou can find out how to access your API key [here](https://docs.roboflow.com/rest-api).\n\n\n\n## Loading a CoreML Model \n\nOnce you've initialized the SDK, you can load your model and configure it with the following code. \n\n```\nrf.load(model: model, modelVersion: modelVersion) { [self] model, error, modelName, modelType in\n    mlModel = model\n    if error != nil {\n        print(error?.localizedDescription as Any)\n    } else {\n        model?.configure(threshold: threshold, overlap: overlap, maxObjects: maxObjects) // object detection models\n        model?.configure(threshold: threshold, overlap: overlap, maxObjects: maxObjects, processingMode: .performance or .balanced or .quality, maxNumberPoints: maximum number of output polygon points) // instance segmentation models\n    }\n}\n```\n\n\n\n## Running Inference ## \n\n\n\n### Image Inference ### \n\nTo run inference on a single image, call: \n\n```\nmlModel.detect(image: imageToDetect) { detections, errorr in\n    let detectionResults: [RFObjectDetectionPrediction] = detections!\n}\n```\n\n\n\n### Video Frame Inference ###\n\nTo run inference on a video stream, you'll want to call the `detect(pixelBuffer: CVPixelBuffer, completion: **@escaping** (([RFObjectDetectionPrediction]?, Error?) -\u003e Void))` function inside of your app's `AVCaptureVideoDataOutputSampleBufferDelegate` `captureOutput` delegate method: \n\n```\nfunc captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {\n    guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {\n        return\n    }\n    currentPixelBuffer = pixelBuffer\n\n    mlModel?.detect(pixelBuffer: pixelBuffer, completion: { detections, error in\n        if error != nil {\n            print(error!)\n        } else {\n            let detectionResults: [RFObjectDetectionPrediction] = detections!\n            ...\n        }\n    })\n}\n```\n\nThe included example app shows a complete implemention illustrating this process of setting up and running an `AVCaptureSession`. \n\n\n\n## Inference Results ###\n\nYou'll have noticed that when an inference is complete, the SDK returns an array of `RFObjectDetectionPrediction` or `RFInstanceSegmentationPrediction` results. These are structs that contain data on what object was detected in the image, as well as information on the bounding box that encapsulates that object: \n\n```\nx: Float\ny: Float\nwidth: Float\nheight: Float\nclassName: String\nconfdience: Float \ncolor: UIColor\nbox: CGRect\npoints: [CGPoint] // only on instance segmentation models\n```\n\nCall `getValues` on the returned `RFObjectDetectionPrediction` or `RFInstanceSegmentationPrediction` to get these results. \n\n\n\n## Image Uploading \n\nIf you want to upload an image to a project for improving future versions of your model, you can do so with the `uploadImage` method. \n\n```\nrf.uploadImage(image: image, project: project) { result in\n\n    switch result {\n        case .Success:\n\t\tprint(\"Image uploaded successfully.\")\n        case .Duplicate:\n        \tprint(\"You attempted to upload a duplicate image.\")\n        case .Error:\n\t\tprint(\"You attempted to upload a duplicate image.\")\n        @unknown default:\n            return\n    }\n}\n```\n\n\n\n## Example App ##\n\nAn example app can be found [here](https://github.com/roboflow/roboflow-swift-examples) that illusrates how to use the Roboflow SDK on an iOS app. The app uses apre-trained model hosted on Roboflow Universe for detecting the actions in a round of rock-paper-scissors. You'll have to provide your own API key. \n\n\n\n## Installation \n\nYou can install the SDK either via Swift Package Manager or Cocoapods. \n\n\n\n#### Swift Package Manager ####\n\nThe [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.\n\nTo install the Roboflow Swift SDK package into your packages, add a reference to the Roboflow Swift SDK and a targeting release version in the dependencies section in `Package.swift` file:\n\n```\nimport PackageDescription\n\nlet package = Package(\n    name: \"YOUR_PROJECT_NAME\",\n    products: [],\n    dependencies: [\n        .package(url: \"https://github.com/roboflow/swift-sdk\", from: \"1.0.0\")\n    ]\n)\n```\n\nTo install the package via Xcode\n\n- Go to File -\u003e Swift Packages -\u003e Add Package Dependency...\n- Then add https://github.com/roboflow/swift-sdk\n\n\n\n#### Cocoapods ####\n\nTo install with Cocoapods, make sure you have Cocoapods already installed and added to your project, and then run `pod Roboflow` to your podfile: \n\nThen, run `pod install` in the root directory of your project. \n\nIf you've previously installed the Roboflow SDK via Cocoapods, you'll need to update your podfile to have an entry of `pod Roboflow`. \n\n\n\n## Running Tests\n\nThe SDK includes a comprehensive test suite that validates model loading and inference functionality. To run the tests:\n\n```bash\n# for swift only tests\nswift test\n\n# for iOS simulator tests\nxcodebuild test -scheme RoboflowTests -destination 'platform=macOS,variant=Mac Catalyst,arch=arm64'\n```\n\nThe test suite includes:\n- **Model Loading Tests**: Validates that object detection and segmentation models load correctly from the Roboflow API\n- **Inference Tests**: Tests inference on real images using both object detection and instance segmentation models\n- **Cross-platform Compatibility**: All tests run without UIKit dependencies, ensuring compatibility across Apple platforms\n\nTest images are located in `Tests/assets/` and the tests automatically download and cache models for testing. All tests are designed to run without requiring additional setup or configuration.\n\n\n\n## Publishing a New Version\n\nTo publish a new version to CocoaPods:\n\n1. Update the version number in `Roboflow.podspec`\n2. Issue a pull request and merge to the `main` branch\n3. The GitHub Action will automatically:\n   - Validate the podspec\n   - Create and push a git tag for the version\n   - Publish to CocoaPods trunk\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froboflow%2Froboflow-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froboflow%2Froboflow-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froboflow%2Froboflow-swift/lists"}