首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于摄像机的人脸检测

问基于摄像机的人脸检测
EN

Stack Overflow用户
提问于 2016-12-28 02:46:36
回答 6查看 18.7K关注 0票数 40

我怎样才能像“摄像机”那样实时进行人脸检测呢?

​

​

我注意到10.0之后就不再推荐AVCaptureStillImageOutput了,所以我改用AVCapturePhotoOutput。但是,我发现我保存下来的用于面部检测的图像不是那么满意吗?有什么想法吗?

更新

在尝试了@提到的。目前,我使用AVCaptureMetadataOutput来检测没有CIFaceDetector的人脸。它如预期的那样工作。然而,当我试图划出脸的界限时,它似乎被错位了。有什么想法吗?

​

​

代码语言:javascript
复制
let metaDataOutput = AVCaptureMetadataOutput()

captureSession.sessionPreset = AVCaptureSessionPresetPhoto
    let backCamera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back)
    do {
        let input = try AVCaptureDeviceInput(device: backCamera)

        if (captureSession.canAddInput(input)) {
            captureSession.addInput(input)

            // MetadataOutput instead
            if(captureSession.canAddOutput(metaDataOutput)) {
                captureSession.addOutput(metaDataOutput)

                metaDataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
                metaDataOutput.metadataObjectTypes = [AVMetadataObjectTypeFace]

                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer?.frame = cameraView.bounds
                previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill

                cameraView.layer.addSublayer(previewLayer!)
                captureSession.startRunning()
            }

        }

    } catch {
        print(error.localizedDescription)
    }

和

代码语言:javascript
复制
extension CameraViewController: AVCaptureMetadataOutputObjectsDelegate {
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    if findFaceControl {
        findFaceControl = false
        for metadataObject in metadataObjects {
            if (metadataObject as AnyObject).type == AVMetadataObjectTypeFace {
                print("")
                print(metadataObject)
                let bounds = (metadataObject as! AVMetadataFaceObject).bounds
                print("origin x: \(bounds.origin.x)")
                print("origin y: \(bounds.origin.y)")
                print("size width: \(bounds.size.width)")
                print("size height: \(bounds.size.height)")
                print("cameraView width: \(self.cameraView.frame.width)")
                print("cameraView height: \(self.cameraView.frame.height)")
                var face = CGRect()
                face.origin.x = bounds.origin.x * self.cameraView.frame.width
                face.origin.y = bounds.origin.y * self.cameraView.frame.height
                face.size.width = bounds.size.width * self.cameraView.frame.width
                face.size.height = bounds.size.height * self.cameraView.frame.height
                print(face)

                showBounds(at: face)
            }
        }
    }

}
}

原始

见“吉突布”

代码语言:javascript
复制
var captureSession = AVCaptureSession()
var photoOutput = AVCapturePhotoOutput()
var previewLayer: AVCaptureVideoPreviewLayer?    

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    captureSession.sessionPreset = AVCaptureSessionPresetHigh

    let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    do {
        let input = try AVCaptureDeviceInput(device: backCamera)

        if (captureSession.canAddInput(input)) {
            captureSession.addInput(input)

            if(captureSession.canAddOutput(photoOutput)){
                captureSession.addOutput(photoOutput)
                captureSession.startRunning()

                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                previewLayer?.frame = cameraView.bounds

                cameraView.layer.addSublayer(previewLayer!)
            }
        }

    } catch {
        print(error.localizedDescription)
    }

}

func captureImage() {
    let settings = AVCapturePhotoSettings()
    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType
                         ]
    settings.previewPhotoFormat = previewFormat
    photoOutput.capturePhoto(with: settings, delegate: self)

}



func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
    if let error = error {
        print(error.localizedDescription)
    }
    // Not include previewPhotoSampleBuffer
    if let sampleBuffer = photoSampleBuffer,
        let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: nil) {
            self.imageView.image = UIImage(data: dataImage)
            self.imageView.isHidden = false
            self.previewLayer?.isHidden = true
            self.findFace(img: self.imageView.image!)
        }
}

findFace适用于正常图像。然而,我通过相机捕捉到的图像将无法工作,有时只能识别一张脸。

法线图像

​

​

捕获图像

​

​

代码语言:javascript
复制
func findFace(img: UIImage) {
    guard let faceImage = CIImage(image: img) else { return }
    let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
    let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy)


    // For converting the Core Image Coordinates to UIView Coordinates
    let detectedImageSize = faceImage.extent.size
    var transform = CGAffineTransform(scaleX: 1, y: -1)
    transform = transform.translatedBy(x: 0, y: -detectedImageSize.height)


    if let faces = faceDetector?.features(in: faceImage, options: [CIDetectorSmile: true, CIDetectorEyeBlink: true]) {
        for face in faces as! [CIFaceFeature] {

            // Apply the transform to convert the coordinates
            var faceViewBounds =  face.bounds.applying(transform)
            // Calculate the actual position and size of the rectangle in the image view
            let viewSize = imageView.bounds.size
            let scale = min(viewSize.width / detectedImageSize.width,
                            viewSize.height / detectedImageSize.height)
            let offsetX = (viewSize.width - detectedImageSize.width * scale) / 2
            let offsetY = (viewSize.height - detectedImageSize.height * scale) / 2

            faceViewBounds = faceViewBounds.applying(CGAffineTransform(scaleX: scale, y: scale))
            print("faceBounds = \(faceViewBounds)")
            faceViewBounds.origin.x += offsetX
            faceViewBounds.origin.y += offsetY

            showBounds(at: faceViewBounds)
        }

        if faces.count != 0 {
            print("Number of faces: \(faces.count)")
        } else {
            print("No faces ")
        }
    }


}

func showBounds(at bounds: CGRect) {
    let indicator = UIView(frame: bounds)
    indicator.frame =  bounds
    indicator.layer.borderWidth = 3
    indicator.layer.borderColor = UIColor.red.cgColor
    indicator.backgroundColor = .clear

    self.imageView.addSubview(indicator)
    faceBoxes.append(indicator)

}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2017-01-24 13:39:25

检测人脸的方法有两种: CIFaceDetector和AVCaptureMetadataOutput。根据您的需求,选择与您相关的内容。

CIFaceDetector有更多的功能,它给你眼睛和嘴巴的位置,一个微笑探测器等等。

另一方面,在帧上计算AVCaptureMetadataOutput,跟踪检测到的人脸,不需要添加额外的代码。我发现这是因为追踪。在此过程中,人脸检测更加可靠。这样做的缺点是,你只会检测到面孔,而不是眼睛或嘴巴的位置。此方法的另一个优点是,定位问题较小,因为您可以在设备方向发生变化时使用videoOrientation,而面向的方向将相对于该方向。

在我的例子中,我的应用程序使用YUV420作为所需的格式,因此实时使用CIDetector (与RGB一起工作)是不可行的。由于连续跟踪,使用AVCaptureMetadataOutput节省了大量的精力,并且执行得更可靠。

一旦我有了包围框的脸,我编码额外的特征,如皮肤检测,并将其应用于静止图像。

注意:当您捕获静态图像时,脸框信息将与元数据一起添加,因此不会出现同步问题。

您也可以使用两者的组合,以获得更好的结果。

根据您的应用程序探索和评估利弊。

人脸矩形是wrt图像的原点。所以,对于屏幕来说,它可能是不同的。使用:

代码语言:javascript
复制
for (AVMetadataFaceObject *faceFeatures in metadataObjects) {
    CGRect face = faceFeatures.bounds;
    CGRect facePreviewBounds = CGRectMake(face.origin.y * previewLayerRect.size.width,
                               face.origin.x * previewLayerRect.size.height,
                               face.size.width * previewLayerRect.size.height,
                               face.size.height * previewLayerRect.size.width);

    /* Draw rectangle facePreviewBounds on screen */
}
票数 13
EN

Stack Overflow用户

发布于 2017-03-06 08:06:07

要在iOS上执行人脸检测,可以使用CIDetector (苹果)或移动视觉 (Google)。

IMO,提供了更好的性能。

如果你感兴趣,这是你可以玩的项目。(iOS 10.2,Swift 3)

在2017年WWDC之后,苹果在iOS 11中引入了iOS。Vision框架使人脸检测更加准确:)

我做了一个演示项目。包含VisionV.S. CIDetector.同时,它还包含了人脸地标的实时检测。

票数 8
EN

Stack Overflow用户

发布于 2018-06-17 12:02:01

有点晚了,但这里是坐标问题的解决方案。可以在预览层上调用一种方法将元数据对象转换为您的坐标系:transformedMetadataObject(for:metadataObject)。

代码语言:javascript
复制
guard let transformedObject = previewLayer.transformedMetadataObject(for: metadataObject) else {
     continue
}
let bounds = transformedObject.bounds
showBounds(at: bounds)

来源:https://developer.apple.com/documentation/avfoundation/avcapturevideopreviewlayer/1623501-transformedmetadataobjectformeta

顺便说一句,如果您使用Swift 4 (或将项目升级到Swift 4),AVCaptureMetadataOutputsObject的委托方法已经更改为:

代码语言:javascript
复制
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)

亲切的问候

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41354698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档