This repository was archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathPose.swift
152 lines (138 loc) · 4.92 KB
/
Pose.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2020 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import TensorFlow
struct Keypoint {
var y: Float
var x: Float
let index: KeypointIndex
let score: Float
init(
heatmapY: Int, heatmapX: Int, index: Int, score: Float, offsets: CPUTensor<Float>,
outputStride: Int
) {
self.y = Float(heatmapY) * Float(outputStride) + offsets[heatmapY, heatmapX, index]
self.x =
Float(heatmapX) * Float(outputStride)
+ offsets[heatmapY, heatmapX, index + KeypointIndex.allCases.count]
self.index = KeypointIndex(rawValue: index)!
self.score = score
}
init(y: Float, x: Float, index: KeypointIndex, score: Float) {
self.y = y
self.x = x
self.index = index
self.score = score
}
func isWithinRadiusOfCorrespondingKeypoints(in poses: [Pose], radius: Float) -> Bool {
return poses.contains { pose in
let correspondingKeypoint = pose.getKeypoint(self.index)!
let dy = correspondingKeypoint.y - self.y
let dx = correspondingKeypoint.x - self.x
let squaredDistance = dy * dy + dx * dx
return squaredDistance <= radius * radius
}
}
}
enum KeypointIndex: Int, CaseIterable {
case nose = 0
case leftEye
case rightEye
case leftEar
case rightEar
case leftShoulder
case rightShoulder
case leftElbow
case rightElbow
case leftWrist
case rightWrist
case leftHip
case rightHip
case leftKnee
case rightKnee
case leftAnkle
case rightAnkle
}
enum Direction { case fwd, bwd }
func getNextKeypointIndexAndDirection(_ keypointId: KeypointIndex) -> [(KeypointIndex, Direction)] {
switch keypointId {
case .nose:
return [(.leftEye, .fwd), (.rightEye, .fwd), (.leftShoulder, .fwd), (.rightShoulder, .fwd)]
case .leftEye: return [(.nose, .bwd), (.leftEar, .fwd)]
case .rightEye: return [(.nose, .bwd), (.rightEar, .fwd)]
case .leftEar: return [(.leftEye, .bwd)]
case .rightEar: return [(.rightEye, .bwd)]
case .leftShoulder: return [(.leftHip, .fwd), (.leftElbow, .fwd), (.nose, .bwd)]
case .rightShoulder: return [(.rightHip, .fwd), (.rightElbow, .fwd), (.nose, .bwd)]
case .leftElbow: return [(.leftWrist, .fwd), (.leftShoulder, .bwd)]
case .rightElbow: return [(.rightWrist, .fwd), (.rightShoulder, .bwd)]
case .leftWrist: return [(.leftElbow, .bwd)]
case .rightWrist: return [(.rightElbow, .bwd)]
case .leftHip: return [(.leftKnee, .fwd), (.leftShoulder, .bwd)]
case .rightHip: return [(.rightKnee, .fwd), (.rightShoulder, .bwd)]
case .leftKnee: return [(.leftAnkle, .fwd), (.leftHip, .bwd)]
case .rightKnee: return [(.rightAnkle, .fwd), (.rightHip, .bwd)]
case .leftAnkle: return [(.leftKnee, .bwd)]
case .rightAnkle: return [(.rightKnee, .bwd)]
}
}
/// Maps a pair of keypoint indexes to the appropiate index to be used
/// in the displacement forward and backward tensors.
let keypointPairToDisplacementIndexMap: [Set<KeypointIndex>: Int] = [
Set([.nose, .leftEye]): 0,
Set([.leftEye, .leftEar]): 1,
Set([.nose, .rightEye]): 2,
Set([.rightEye, .rightEar]): 3,
Set([.nose, .leftShoulder]): 4,
Set([.leftShoulder, .leftElbow]): 5,
Set([.leftElbow, .leftWrist]): 6,
Set([.leftShoulder, .leftHip]): 7,
Set([.leftHip, .leftKnee]): 8,
Set([.leftKnee, .leftAnkle]): 9,
Set([.nose, .rightShoulder]): 10,
Set([.rightShoulder, .rightElbow]): 11,
Set([.rightElbow, .rightWrist]): 12,
Set([.rightShoulder, .rightHip]): 13,
Set([.rightHip, .rightKnee]): 14,
Set([.rightKnee, .rightAnkle]): 15,
]
public struct Pose {
var keypoints: [Keypoint?] = Array(repeating: nil, count: KeypointIndex.allCases.count)
var resolution: (height: Int, width: Int)
mutating func add(_ keypoint: Keypoint) {
keypoints[keypoint.index.rawValue] = keypoint
}
func getKeypoint(_ index: KeypointIndex) -> Keypoint? {
return keypoints[index.rawValue]
}
mutating func rescale(to newResolution: (height: Int, width: Int)) {
for i in 0..<keypoints.count {
if var k = keypoints[i] {
k.y *= Float(newResolution.height) / Float(resolution.height)
k.x *= Float(newResolution.width) / Float(resolution.width)
self.keypoints[i] = k
}
}
self.resolution = newResolution
}
}
extension Pose: CustomStringConvertible {
public var description: String {
var description = ""
for keypoint in keypoints {
description.append(
"\(keypoint!.index) - \(keypoint!.score) | \(keypoint!.y) - \(keypoint!.x)\n")
}
return description
}
}