|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +import math |
| 5 | +from keypoints import poseChain, partIds, partNames |
| 6 | +from decode_utils import clamp, addVectors, getImageCoords, getOffsetPoint |
| 7 | + |
| 8 | +parentChildrenTuples = [] |
| 9 | + |
| 10 | +for joint_name in poseChain: |
| 11 | + parent_joint_name = joint_name[0] |
| 12 | + child_joint_name = joint_name[1] |
| 13 | + parentChildrenTuples.append([partIds[parent_joint_name], \ |
| 14 | + partIds[child_joint_name]]) |
| 15 | +'''' |
| 16 | +parentChildrenTuples = poseChain.map( |
| 17 | + ([parentJoinName, childJoinName]): NumberTuple => |
| 18 | + ([partIds[parentJoinName], partIds[childJoinName]])); |
| 19 | +''' |
| 20 | + |
| 21 | +parentToChildEdges = [] |
| 22 | +for joint_id in parentChildrenTuples: |
| 23 | + parentToChildEdges.append(joint_id[1]) #child_joint_id |
| 24 | + |
| 25 | +''' |
| 26 | +const parentToChildEdges: number[] = |
| 27 | + parentChildrenTuples.map(([, childJointId]) => childJointId); |
| 28 | +''' |
| 29 | + |
| 30 | +childToParentEdges = [] |
| 31 | +for joint_id in parentChildrenTuples: |
| 32 | + childToParentEdges.append(joint_id[0]) #child_joint_id |
| 33 | + |
| 34 | +''' |
| 35 | +const childToParentEdges: number[] = |
| 36 | + parentChildrenTuples.map(([ |
| 37 | + parentJointId, |
| 38 | + ]) => parentJointId); |
| 39 | +''' |
| 40 | + |
| 41 | +def getDisplacement(edgeId, point, displacements): |
| 42 | + numEdges = int(displacements.shape[2] / 2) |
| 43 | + point_x = int(point['x']) |
| 44 | + point_y = int(point['y']) |
| 45 | + y = displacements[point_y][point_x][edgeId] |
| 46 | + x = displacements[point_y][point_x][numEdges+edgeId] |
| 47 | + return {'y':y, 'x':x} |
| 48 | + |
| 49 | +def getStridedIndexNearPoint(point, outputStride, height, width): |
| 50 | + return {'y':clamp(round(float(point['y']) / float(outputStride)), 0, height - 1), \ |
| 51 | + 'x':clamp(round(float(point['x']) / float(outputStride)), 0, width - 1)} |
| 52 | + |
| 53 | +''' |
| 54 | + * We get a new keypoint along the `edgeId` for the pose instance, assuming |
| 55 | + * that the position of the `idSource` part is already known. For this, we |
| 56 | + * follow the displacement vector from the source to target part (stored in |
| 57 | + * the `i`-t channel of the displacement tensor). |
| 58 | +''' |
| 59 | +def traverseToTargetKeypoint(edgeId, sourceKeypoint, targetKeypointId, |
| 60 | + scoresBuffer, offsets, outputStride, displacements): |
| 61 | + height, width, numberScores = scoresBuffer.shape |
| 62 | + #Nearest neighbor interpolation for the source->target displacements. |
| 63 | + #最近邻插值(Nearest Neighbor interpolation) |
| 64 | + sourceKeypointIndices = getStridedIndexNearPoint(sourceKeypoint['position'], \ |
| 65 | + outputStride, \ |
| 66 | + height, \ |
| 67 | + width) |
| 68 | + displacement = getDisplacement(edgeId, sourceKeypointIndices, displacements) |
| 69 | + displacedPoint = addVectors(sourceKeypoint['position'], displacement) |
| 70 | + displacedPointIndices = getStridedIndexNearPoint(displacedPoint, \ |
| 71 | + outputStride, \ |
| 72 | + height, \ |
| 73 | + width) |
| 74 | + x = int(displacedPointIndices['x']) |
| 75 | + y = int(displacedPointIndices['y']) |
| 76 | + offsetPoint = getOffsetPoint(y, x, targetKeypointId, offsets) |
| 77 | + score = scoresBuffer[y][x][targetKeypointId] |
| 78 | + targetKeypoint = addVectors( \ |
| 79 | + {'x': displacedPointIndices['x'] * outputStride, 'y': displacedPointIndices['y'] * outputStride}, \ |
| 80 | + {'x': offsetPoint['x'], 'y': offsetPoint['y']}) |
| 81 | + |
| 82 | + return {"position": targetKeypoint, \ |
| 83 | + "part": partNames[targetKeypointId], \ |
| 84 | + "score":score} |
| 85 | + |
| 86 | +''' |
| 87 | + * Follows the displacement fields to decode the full pose of the object |
| 88 | + * instance given the position of a part that acts as root. |
| 89 | + * |
| 90 | + * @return An array of decoded keypoints and their scores for a single pose |
| 91 | +''' |
| 92 | +def decodePose(root, scores, offsets, outputStride, displacementsFwd, displacementsBwd): |
| 93 | + numParts = scores.shape[2] |
| 94 | + numEdges = len(parentToChildEdges) |
| 95 | + |
| 96 | + #const instanceKeypoints: Keypoint[] = new Array(numParts); |
| 97 | + instanceKeypoints = [] |
| 98 | + for i in range(numParts): |
| 99 | + keypoint = {'position':{'x':0, 'y':0}, 'part':'null', 'score':0.0} |
| 100 | + instanceKeypoints.append(keypoint) |
| 101 | + #Start a new detection instance at the position of the root. |
| 102 | + rootPart, rootScore = root['part'], root['score'] |
| 103 | + rootPoint = getImageCoords(rootPart, outputStride, offsets) |
| 104 | + id = rootPart['id'] |
| 105 | + instanceKeypoints[id] = {'position':rootPoint, 'part':partNames[id], 'score':rootScore} |
| 106 | + |
| 107 | + #Decode the part positions upwards in the tree, following the backward displacements. |
| 108 | + for edge in reversed(range(numEdges)): |
| 109 | + sourceKeypointId = parentToChildEdges[edge] |
| 110 | + targetKeypointId = childToParentEdges[edge] |
| 111 | + if instanceKeypoints[sourceKeypointId]['score'] > 0.0 \ |
| 112 | + and math.isclose(instanceKeypoints[targetKeypointId]['score'], 0.0, rel_tol=1e-9) is True: |
| 113 | + instanceKeypoints[targetKeypointId] = traverseToTargetKeypoint(edge, |
| 114 | + instanceKeypoints[sourceKeypointId], |
| 115 | + targetKeypointId, |
| 116 | + scores, |
| 117 | + offsets, |
| 118 | + outputStride, |
| 119 | + displacementsBwd) |
| 120 | + |
| 121 | + # Decode the part positions downwards in the tree, following the forward |
| 122 | + #displacements. |
| 123 | + for edge in range(numEdges): |
| 124 | + sourceKeypointId = childToParentEdges[edge] |
| 125 | + targetKeypointId = parentToChildEdges[edge] |
| 126 | + #if instanceKeypoints[sourceKeypointId] is not None and instanceKeypoints[targetKeypointId] is None: |
| 127 | + if instanceKeypoints[sourceKeypointId]['score'] > 0.0 \ |
| 128 | + and math.isclose(instanceKeypoints[targetKeypointId]['score'], 0.0, rel_tol=1e-9) is True: |
| 129 | + instanceKeypoints[targetKeypointId] = traverseToTargetKeypoint(edge, \ |
| 130 | + instanceKeypoints[sourceKeypointId], \ |
| 131 | + targetKeypointId, \ |
| 132 | + scores, \ |
| 133 | + offsets, \ |
| 134 | + outputStride, \ |
| 135 | + displacementsFwd) |
| 136 | + return instanceKeypoints |
0 commit comments