Rx-Ver-ID 1.5.0

Rx-Ver-ID 1.5.0

Jakub Dolejs 维护。



 
依赖
RxSwift~> 5
RxCocoa~> 5
 

Rx-Ver-ID 1.5.0

  • 作者
  • Jakub Dolejs

Cocoapods CI

Rx-Ver-ID-Apple

为 iOS 实现的 Ver-ID 响应式实现

安装

  1. 注册您的应用,您将需要您的应用的包标识符。

  2. 注册您的应用将为您的应用生成一个评估许可证。许可证有效期为 30 天。如果您需要生产许可证,请 联系 Applied Recognition

  3. 注册完成时,您将收到一个名为 Ver-ID identity.p12 的文件和一个密码。将密码复制到安全位置,并将 Ver-ID identity.p12 文件添加到您的应用程序中

    • 在 Xcode 中打开您的项目。
    • 从菜单栏选择 文件/将文件添加到 “[您的项目名称]”... 或按 ⌥⌘A 并浏览以选择下载的 Ver-ID identity.p12 文件。
    • 通过点击对话框左下角的 选项 按钮来显示选项。
    • 目标 下选择 如果需要则复制项目
    • 添加到目标 下选择您的应用程序目标。
  4. Ver-ID 需要您在注册时收到的密码。

    • 您可以在创建 RxVerID 实例时指定密码

      let rxVerID = RxVerID(veridPassword: "your password goes here")
    • 或者您可以在应用程序的 Info.plist 中添加密码

      <key>com.appliedrec.verid.password</key>
      <string>your password goes here</string>
  5. 如果您的项目使用 CocoaPods 进行依赖管理,请打开项目的 Podfile。否则,请确保已安装 CocoaPods 并在项目文件夹中创建名为 Podfile 的文件(无扩展名)。

  6. 假设您的项目名为 MyProject,它有一个名为 MyApp 的应用程序目标。在文本编辑器中打开 Podfile 并输入以下内容

    project 'MyProject.xcodeproj'
    workspace 'MyProject.xcworkspace'
    platform :ios, '10.3'
    target 'MyApp' do
    	use_frameworks!
    	pod 'Rx-Ver-ID'
    end
  7. 保存 Podfile。打开 终端 并导航到您的项目文件夹。然后输入

    pod install
  8. 现在您可以在 Xcode 中打开 MyProject.xcworkspace 文件,Rx-Ver-ID 将可在您的应用 MyApp 中使用。

示例

在图像中检测人脸并将其裁剪至人脸边界

import RxVerID
import RxSwift

// Create an instance of RxVerID
let rxVerID = RxVerID()
// Set this to a file URL of an image with a face
let url = URL(fileURLWithPath: "test.jpg")
rxVerID.detectFacesInImageURL(url, limit: 1) // Detect one face
    .single() // Convert observable to single
    .flatMap { face in
        rxVerID.cropImageURL(url, toFace: face) // Crop the image
    }
    .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .default)) // Subscribe on a background thread
    .observeOn(MainScheduler()) // Observe on main thread
    .subscribe(onNext: { image in
      // The image is an instance of UIImage. You can display the image in an image view, save it, etc.
    }, onError: { error in
      // Something went wrong, inspect error
    }, onCompleted: nil, onDisposed: nil)

在图像中检测人脸并将其分配给用户

import RxVerID
import RxSwift

// Create an instance of RxVerID
let rxVerID = RxVerID()
// Set this to a file URL of an image with a face
let url = URL(fileURLWithPath: "test.jpg")
// Set this to an identifier for your user
let userId = "testUserId"
rxVerID.detectRecognizableFacesInImageURL(url, limit: 1) // Detect one face
    .single() // Convert observable to single to ensure one face was found
    .flatMap { face in
        rxVerID.assignFace(face, toUser: userId) // Assign the detected face to user
    }
    .asCompletable()
    .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .default)) // Subscribe on a background thread
    .observeOn(MainScheduler()) // Observe on main thread
    .subscribe(onCompleted: {
      // The face has been assigned to user "testUserId"
    }, onError: { error in
      // Something went wrong, inspect error
    })

在图像中验证用户

import RxVerID
import RxSwift

// Create an instance of RxVerID
let rxVerID = RxVerID()
// Set this to a file URL of an image with a face
let url = URL(fileURLWithPath: "test.jpg")
// Set this to an identifier for your user
let userId = "testUserId"
rxVerID.authenticateUser(userId, inImageURL: url) // Detect one face
    .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .default)) // Subscribe on a background thread
    .observeOn(MainScheduler()) // Observe on main thread
    .subscribe(onSuccess: { authenticated in
    	if authenticated {
      		// The image has been authenticated as user "testUserId"
      }
    }, onError: { error in
      // Something went wrong, inspect error
    })

在图像中识别用户

import RxVerID
import RxSwift

// Create an instance of RxVerID
let rxVerID = RxVerID()
// Set this to a file URL of an image with a face
let url = URL(fileURLWithPath: "test.jpg")
rxVerID.identifyUsersInImageURL(url) // Identify users
	.single() // Fail if no users or more than one user are identified
	.map { userScoreTuple in
		userScorePair.0 // We only need the user ID without the score
	}
	.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .default)) // Subscribe on a background thread
	.observeOn(MainScheduler()) // Observe on main thread
	.subscribe(onNext: { userId in
		// Identified userId
	}, onError: { error in
		// Something went wrong, inspect error
	}, onCompleted: nil, onDisposed: nil)

运行 Ver-ID 会话

import RxVerID
import RxSwift

// Create an instance of RxVerID
let rxVerID = RxVerID()
// Create a dispose bag
let disposeBag = DisposeBag()
// Create session settings
let settings = LivenessDetectionSessionSettings()
// Get a window in which to run the session
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow}).first else {
    return
}
rxVerID.sessionInWindow(window, settings: settings)
    .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .default))
    .observeOn(MainScheduler.instance)
    .subscribe(onSuccess: { result in
        // Session succeeded 
    }, onError: { error in
        // Session failed
    }, onCompleted: {
        // Session was cancelled
    })
    .disposed(by: disposeBag)

高级选项

仅使用低级API

如果您不打算使用RxVerID运行Ver-ID会话,您可以通过仅包含库的核心部分来减小您应用程序的体积。为此,将pod spec更改为

pod 'Rx-Ver-ID/Core'

从URL加载 Ver-ID identity.p12 文件

如果您希望从不同于您应用程序主包的位置使用 Ver-ID identity.p12 文件,您可以构建一个 VerIDIdentity 实例并将其传递给 RxVerID 初始化器。此选项仅适用于iOS 10.3或更高版本。

do {
    let url: URL // Set this to the URL pointing to your 'Ver-ID identity.p12' file
    let identity = try VerIDIdentity(url: url)
    let rxVerID = RxVerID(identity: identity)
} catch {
    // Failed to create Ver-ID identity
}

参考文档