Pose Estimation: Draw legs Keypoints only

Hi there!

I’m having a crash when I try to draw just the legs keypoints:

Crash says: Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444

My code:

private func setupFritz() {
    poseModel = FritzVisionHumanPoseModelFast()

    image = UIImage(named: "model")!

    modelImage = FritzVisionImage(image: image).then {
        $0.metadata = FritzVisionImageMetadata()
        $0.metadata?.orientation = FritzImageOrientation(image.imageOrientation)
    }

    let options = FritzVisionPoseModelOptions().then {
        $0.minPartThreshold = 0.3
        $0.minPoseThreshold = 0.3
    }

    guard let poseResult = try? poseModel.predict(modelImage, options: options),
          let pose = poseResult.pose() else { return }


    var modelLegsKeypoints: [Keypoint<HumanSkeleton>] = []
    let rightLegParts: [HumanSkeleton] = [.rightHip, .rightKnee, .rightAnkle]
    let leftLegParts: [HumanSkeleton] = [.leftHip, .leftKnee, .leftAnkle]

    for keypoint in pose.keypoints {
        if rightLegParts.contains(keypoint.part) || leftLegParts.contains(keypoint.part) {
            modelLegsKeypoints.append(keypoint)
        }
    }

    let legsPose = HumanPose(keypoints: modelLegsKeypoints, score: pose.score, bounds: pose.bounds)

    let imageWithPose = modelImage.draw(pose: legsPose) **HERE CRASHES**

    DispatchQueue.main.async { [weak self] in
        guard let strongSelf = self else { return }
        strongSelf.imageView.image = imageWithPose
    }
}

Is there a way to draw just the legs keypoints?

The HumanPose object expected to receive all keypoints in the HumanSkeleton. The code above initializes legPose with a filtered list of keypoints that do not include every point expected. When you call the draw function, it attempts to find all keypoints in the HumanSkeleton in the legPose object you created and cannot, thus causing the crash.

If you want to show only keypoints related to legs, you’ll need to write your own custom draw function. For example, you could extend FritzVisionImage to have a method called drawLegs that would take a full HumanPose with all keypoints, filter them, and only draw the ones you want. The drawing can be done with an Image Context.