我正在练习使用一些 swift 2,并且在使用 CIDetector 时遇到了一些困难。
我有一个包含一系列图片的应用程序;三个不同的矩形和三张不同的人/组照片。我只是在这些图像上尝试使用 CIDetector 来查看识别的内容。我取得的最大成功是在人脸方面——但是它识别的人脸在图像上非常奇怪的地方。
这是我的检测代码:
///does the detecting of features
func detectFeatures() -> [CIFeature]? {
print("detecting")
let detectorOptions: [String: AnyObject] = [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorImageOrientation: 6]
var detectorType : String
switch currentPhoto.currentType {
case .RECTANGLES:
detectorType = CIDetectorTypeRectangle
case .FACES:
detectorType = CIDetectorTypeFace
default:
print("error in phototype")
return nil
}
let detector = CIDetector(ofType: detectorType, context: nil, options: detectorOptions)
let image = CIImage(image: imageViewer.image!)
guard image != nil else{
print("image not cast to CIImage")
return nil
}
return detector.featuresInImage(image!)
}
添加星星的位置:
for f in features!{
print(f.bounds.origin)
imageViewer.layer.addSublayer(createMarker(f.bounds.size, location: f.bounds.origin))
}
以及星星的诞生地:
///Creates a star on a layer and puts it over the point passed in
func createMarker(size: CGSize, location: CGPoint) -> CALayer{
print("The marker will be at [\(location.x),\(location.y)]")
//doesn't appear in correct place on the screen
let newLayer = CALayer()
newLayer.bounds = imageViewer.bounds
newLayer.frame = CGRect(origin: location, size: size)
newLayer.contents = UIImage(named: "star")?.CGImage
newLayer.backgroundColor = UIColor(colorLiteralRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).CGColor
newLayer.masksToBounds = false
return newLayer
}
有没有人遇到过这样的事情?