我尝试以不同的方式进行人脸检测,现在正在尝试使用Swift翻译的Apples Sqaure Cam。 https://github.com/wayn/SquareCam-Swift
我想要做的是使用CIDetector中的坐标,以便我可以使用该位置覆盖左眼,右眼或嘴上的图层。但是当我使用CIDetector提供的坐标时,我想要附加到面部特征的图层没有正确定位。我的问题特别是这个代码。因为它似乎处理方向和旋转以正确地在脸部周围定位红色框。
以下是代码:
func drawFaceBoxesForFeatures(features : NSArray, clap : CGRect, orientation : UIDeviceOrientation) {
let sublayers : NSArray = previewLayer.sublayers!
let sublayersCount : Int = sublayers.count
var currentSublayer : Int = 0
// var featuresCount : Int = features.count
var currentFeature : Int = 0
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
// hide all the face layers
for layer in sublayers as! [CALayer] {
if (layer.name != nil && layer.name == "FaceLayer") {
layer.hidden = true
}
}
if ( features.count == 0 || !detectFaces ) {
CATransaction.commit()
return
}
let parentFrameSize : CGSize = previewView.frame.size
let gravity : NSString = previewLayer.videoGravity
let previewBox : CGRect = ViewController.videoPreviewBoxForGravity(gravity, frameSize: parentFrameSize, apertureSize: clap.size)
for ff in features as! [CIFaceFeature] {
// set text on label
var x : CGFloat = 0.0, y : CGFloat = 0.0
if ff.hasLeftEyePosition {
x = ff.leftEyePosition.x
y = ff.leftEyePosition.y
eyeLeftLabel.text = ff.leftEyeClosed ? "(\(x) \(y))" : "(\(x) \(y))" + ""
}
if ff.hasRightEyePosition {
x = ff.rightEyePosition.x
y = ff.rightEyePosition.y
eyeRightLabel.text = ff.rightEyeClosed ? "(\(x) \(y))" : "(\(x) \(y))" + ""
}
if ff.hasMouthPosition {
x = ff.mouthPosition.x
y = ff.mouthPosition.y
mouthLabel.text = ff.hasSmile ? "\(x) \(y)" + "" : "(\(x) \(y))"
}
// find the correct position for the square layer within the previewLayer
// the feature box originates in the bottom left of the video frame.
// (Bottom right if mirroring is turned on)
var faceRect : CGRect = ff.bounds
// flip preview width and height
var temp : CGFloat = faceRect.width
faceRect.size.width = faceRect.height
faceRect.size.height = temp
temp = faceRect.origin.x
faceRect.origin.x = faceRect.origin.y
faceRect.origin.y = temp
// scale coordinates so they fit in the preview box, which may be scaled
let widthScaleBy = previewBox.size.width / clap.size.height
let heightScaleBy = previewBox.size.height / clap.size.width
faceRect.size.width *= widthScaleBy
faceRect.size.height *= heightScaleBy
faceRect.origin.x *= widthScaleBy
faceRect.origin.y *= heightScaleBy
faceRect = CGRectOffset(faceRect, previewBox.origin.x, previewBox.origin.y)
var featureLayer : CALayer? = nil
// re-use an existing layer if possible
while (featureLayer == nil) && (currentSublayer < sublayersCount) {
let currentLayer : CALayer = sublayers.objectAtIndex(currentSublayer++) as! CALayer
if currentLayer.name == nil {
continue
}
let name : NSString = currentLayer.name!
if name.isEqualToString("FaceLayer") {
featureLayer = currentLayer;
currentLayer.hidden = false
}
}
// create a new one if necessary
if featureLayer == nil {
featureLayer = CALayer()
featureLayer?.contents = square.CGImage
featureLayer?.name = "FaceLayer"
previewLayer.addSublayer(featureLayer!)
}
featureLayer?.frame = faceRect
currentFeature++
}
CATransaction.commit()
}
`
我需要采取哪些步骤才能将覆盖层正确地放在眼睛和嘴巴上?
答案 0 :(得分:1)
我遇到了这个问题,我使用将videoGravity更改为AVLayerVideoGravityResize
previewLayer?.videoGravity = AVLayerVideoGravityResize