Как получить ограниченное количество кадров при распознавании лица с помощью MLVision - PullRequest
0 голосов
/ 01 октября 2019

Я попытался выполнить обнаружение лица с помощью MLVision, и он работает нормально. Теперь я хочу получить только первые 10 кадров данных при обнаружении. Я также могу получить данные опорных точек и нормализованные значения. Можно любоепомогите мне сделать это. Я дал свой код, который я попробовал ниже. Спасибо заранее.

 private func detectFacesOnDevice(in image: VisionImage, width: CGFloat, height: CGFloat) {
    let options = VisionFaceDetectorOptions()

    // When performing latency tests to determine ideal detection settings,
    // run the app in 'release' mode to get accurate performance metrics
    options.landmarkMode = .none
    options.contourMode = .all
    options.classificationMode = .none

    options.performanceMode = .fast
    let faceDetector = vision.faceDetector(options: options)

    var detectedFaces: [VisionFace]? = nil
    do {
      detectedFaces = try faceDetector.results(in: image)
    } catch let error {
      print("Failed to detect faces with error: \(error.localizedDescription).")
    }
    guard var faces = detectedFaces, !faces.isEmpty else {
      print("On-Device face detector returned no results.")
      DispatchQueue.main.sync {
        self.updatePreviewOverlayView()
        self.removeDetectionAnnotations()
      }
      return
    }

    DispatchQueue.main.sync {
      self.updatePreviewOverlayView()
      self.removeDetectionAnnotations()
        faces = insertIntoArray(detectedFaces!, array: faces)
      for face in faces {
        let normalizedRect = CGRect(
          x: face.frame.origin.x / width,
          y: face.frame.origin.y / height,
          width: face.frame.size.width / width,
          height: face.frame.size.height / height
        )

        let standardizedRect =
          self.previewLayer.layerRectConverted(fromMetadataOutputRect: normalizedRect).standardized
        UIUtilities.addRectangle(
          standardizedRect,
          to: self.annotationOverlayView,
          color: UIColor.lightGray
        )
        self.addContours(for: face, width: width, height: height)
      }

    }
  }




 private func addContours(for face: VisionFace, width: CGFloat, height: CGFloat) {
    // Face

    if let faceContour = face.contour(ofType: .face) {

            for point in faceContour.points {
                let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
                UIUtilities.addCircle(
                    atPoint: cgPoint,
                    to: annotationOverlayView,
                    color: UIColor.white,
                    radius: Constant.smallDotRadius
                )
                faceContourPoint = cgPoint
               // print("faceContourPoint\(faceContourPoint)")
                print("FacecontourNV\(cgPoint)")
            }
        //print("Facecontour\(faceContour.points)")
    }

    // Eyebrows
    if let topLeftEyebrowContour = face.contour(ofType: .leftEyebrowTop) {
      for point in topLeftEyebrowContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        topLeftEyebrowContourPoint = cgPoint
        //print("topLeftEyebrowContourPoint\(topLeftEyebrowContourPoint)")
        print("toplefteyebrowcontourNV\(cgPoint)")
      }
        // print("topLeftEyebrowContour\(topLeftEyebrowContour.points)")

    }

    if let bottomLeftEyebrowContour = face.contour(ofType: .leftEyebrowBottom) {
      for point in bottomLeftEyebrowContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        bottomLeftEyebrowContourPoint = cgPoint
       // print("bottomLeftEyebrowContourPoint\(bottomLeftEyebrowContourPoint)")
         print("bottomlefteyebrowcontourNV\(cgPoint)")
      }
       // print("bottomLeftEyebrowContour\(bottomLeftEyebrowContour.points)")

    }
    if let topRightEyebrowContour = face.contour(ofType: .rightEyebrowTop) {
      for point in topRightEyebrowContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        topRightEyebrowContourPoint = cgPoint
         print("toprighteyebrowcontourNV\(cgPoint)")
      }

    }
    if let bottomRightEyebrowContour = face.contour(ofType: .rightEyebrowBottom) {
      for point in bottomRightEyebrowContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        bottomRightEyebrowContourPoint = cgPoint
         print("bottomrighteyebrowcontourNV\(cgPoint)")
      }

    }

    // Eyes
    if let leftEyeContour = face.contour(ofType: .leftEye) {
      for point in leftEyeContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        leftEyeContourPoint = cgPoint
        arrayOfLeftEyeContour.append(leftEyeContourPoint)
        print("leftEyeContourNV\(cgPoint)")
           }
    }
    if let rightEyeContour = face.contour(ofType: .rightEye) {
      for point in rightEyeContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        rightEyeContourPoint = cgPoint
        arrayOfRightEyeContour.append(rightEyeContourPoint)
         print("righteyecontourNV\(cgPoint)")
      }

    }

    // Lips
    if let topUpperLipContour = face.contour(ofType: .upperLipTop) {
      for point in topUpperLipContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        topUpperLipContourPoint = cgPoint
        print("topupperlipcontourNV\(cgPoint)")
      }

    }
    if let bottomUpperLipContour = face.contour(ofType: .upperLipBottom) {
      for point in bottomUpperLipContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        bottomUpperLipContourPoint = cgPoint
    print("bottomupperlipcontourNV\(cgPoint)")
      }

    }
    if let topLowerLipContour = face.contour(ofType: .lowerLipTop) {
      for point in topLowerLipContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        topLowerLipContourPoint = cgPoint
         print("toplowerlipcontourNV\(cgPoint)")
      }

    }
    if let bottomLowerLipContour = face.contour(ofType: .lowerLipBottom) {
      for point in bottomLowerLipContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        bottomLowerLipContourPoint = cgPoint
       print("bottomlowerlipcontourNV\(cgPoint)")

      }

    }

    // Nose
    if let noseBridgeContour = face.contour(ofType: .noseBridge) {
      for point in noseBridgeContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        noseBridgeContourPoint = cgPoint
        print("nosebridgecontourNV\(cgPoint)")
      }

    }
    if let noseBottomContour = face.contour(ofType: .noseBottom) {
      for point in noseBottomContour.points {
        let cgPoint = normalizedPoint(fromVisionPoint: point, width: width, height: height)
        UIUtilities.addCircle(
          atPoint: cgPoint,
          to: annotationOverlayView,
          color: UIColor.white,
          radius: Constant.smallDotRadius
        )
        noseBottomContourPoint = cgPoint
         print("nosebottomcontourNV\(cgPoint)")
      }
    }

  }
...