|
| 1 | +// Copyright (c) 2018 ml5 |
| 2 | +// |
| 3 | +// This software is released under the MIT License. |
| 4 | +// https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +/* |
| 7 | +PoseDetection |
| 8 | +Ported from pose-detection at Tensorflow.js |
| 9 | +*/ |
| 10 | + |
| 11 | +import EventEmitter from "events"; |
| 12 | +import * as tf from "@tensorflow/tfjs"; |
| 13 | +import * as posenet from "@tensorflow-models/posenet"; |
| 14 | +import callCallback from "../utils/callcallback"; |
| 15 | +import handleArguments from "../utils/handleArguments"; |
| 16 | + |
| 17 | +const DEFAULTS = { |
| 18 | + architecture: "MobileNetV1", // 'MobileNetV1', 'ResNet50' |
| 19 | + outputStride: 16, // 8, 16, 32 |
| 20 | + flipHorizontal: false, // true, false |
| 21 | + minConfidence: 0.5, |
| 22 | + maxPoseDetections: 5, // any number > 1 |
| 23 | + scoreThreshold: 0.5, |
| 24 | + nmsRadius: 20, // any number > 0 |
| 25 | + detectionType: "multiple", // 'single' |
| 26 | + inputResolution: 256, // or { width: 257, height: 200 } |
| 27 | + multiplier: 0.75, // 1.01, 1.0, 0.75, or 0.50 -- only for MobileNet |
| 28 | + quantBytes: 2, // 4, 2, 1 |
| 29 | + modelUrl: null, // url path to model |
| 30 | +}; |
| 31 | + |
| 32 | +class PoseNet extends EventEmitter { |
| 33 | + /** |
| 34 | + * @typedef {Object} options |
| 35 | + * @property {string} architecture - default 'MobileNetV1', |
| 36 | + * @property {number} inputResolution - default 257, |
| 37 | + * @property {number} outputStride - default 16 |
| 38 | + * @property {boolean} flipHorizontal - default false |
| 39 | + * @property {number} minConfidence - default 0.5 |
| 40 | + * @property {number} maxPoseDetections - default 5 |
| 41 | + * @property {number} scoreThreshold - default 0.5 |
| 42 | + * @property {number} nmsRadius - default 20 |
| 43 | + * @property {String} detectionType - default single |
| 44 | + * @property {number} nmsRadius - default 0.75, |
| 45 | + * @property {number} quantBytes - default 2, |
| 46 | + * @property {string} modelUrl - default null |
| 47 | + */ |
| 48 | + /** |
| 49 | + * Create a PoseNet model. |
| 50 | + * @param {HTMLVideoElement || p5.Video} video - Optional. A HTML video element or a p5 video element. |
| 51 | + * @param {options} options - Optional. An object describing a model accuracy and performance. |
| 52 | + * @param {String} detectionType - Optional. A String value to run 'single' or 'multiple' estimation. |
| 53 | + * @param {function} callback Optional. A function to run once the model has been loaded. |
| 54 | + * If no callback is provided, it will return a promise that will be resolved once the |
| 55 | + * model has loaded. |
| 56 | + */ |
| 57 | + constructor(video, options, detectionType, callback) { |
| 58 | + super(); |
| 59 | + this.video = video; |
| 60 | + /** |
| 61 | + * The type of detection. 'single' or 'multiple' |
| 62 | + * @type {String} |
| 63 | + * @public |
| 64 | + */ |
| 65 | + this.modelUrl = options.modelUrl || null; |
| 66 | + this.architecture = options.architecture || DEFAULTS.architecture; |
| 67 | + this.detectionType = |
| 68 | + detectionType || options.detectionType || DEFAULTS.detectionType; |
| 69 | + this.outputStride = options.outputStride || DEFAULTS.outputStride; |
| 70 | + this.flipHorizontal = options.flipHorizontal || DEFAULTS.flipHorizontal; |
| 71 | + this.scoreThreshold = options.scoreThreshold || DEFAULTS.scoreThreshold; |
| 72 | + this.minConfidence = options.minConfidence || DEFAULTS.minConfidence; |
| 73 | + this.maxPoseDetections = |
| 74 | + options.maxPoseDetections || DEFAULTS.maxPoseDetections; |
| 75 | + this.multiplier = options.multiplier || DEFAULTS.multiplier; |
| 76 | + this.inputResolution = options.inputResolution || DEFAULTS.inputResolution; |
| 77 | + this.quantBytes = options.quantBytes || DEFAULTS.quantBytes; |
| 78 | + this.nmsRadius = options.nmsRadius || DEFAULTS.nmsRadius; |
| 79 | + this.ready = callCallback(this.load(), callback); |
| 80 | + // this.then = this.ready.then; |
| 81 | + } |
| 82 | + |
| 83 | + async load() { |
| 84 | + let modelJson; |
| 85 | + if (this.architecture.toLowerCase() === "mobilenetv1") { |
| 86 | + modelJson = { |
| 87 | + architecture: this.architecture, |
| 88 | + outputStride: this.outputStride, |
| 89 | + inputResolution: this.inputResolution, |
| 90 | + multiplier: this.multiplier, |
| 91 | + quantBytes: this.quantBytes, |
| 92 | + modelUrl: this.modelUrl, |
| 93 | + }; |
| 94 | + } else { |
| 95 | + modelJson = { |
| 96 | + architecture: this.architecture, |
| 97 | + outputStride: this.outputStride, |
| 98 | + inputResolution: this.inputResolution, |
| 99 | + quantBytes: this.quantBytes, |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + this.net = await posenet.load(modelJson); |
| 104 | + |
| 105 | + if (this.video) { |
| 106 | + if (this.video.readyState === 0) { |
| 107 | + await new Promise((resolve) => { |
| 108 | + this.video.onloadeddata = () => resolve(); |
| 109 | + }); |
| 110 | + } |
| 111 | + if (this.detectionType === "single") { |
| 112 | + this.singlePose(); |
| 113 | + } else { |
| 114 | + this.multiPose(); |
| 115 | + } |
| 116 | + } |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + skeleton(keypoints, confidence = this.minConfidence) { |
| 121 | + return posenet.getAdjacentKeyPoints(keypoints, confidence); |
| 122 | + } |
| 123 | + |
| 124 | + // eslint-disable-next-line class-methods-use-this |
| 125 | + mapParts(pose) { |
| 126 | + const newPose = JSON.parse(JSON.stringify(pose)); |
| 127 | + newPose.keypoints.forEach((keypoint) => { |
| 128 | + newPose[keypoint.part] = { |
| 129 | + x: keypoint.position.x, |
| 130 | + y: keypoint.position.y, |
| 131 | + confidence: keypoint.score, |
| 132 | + }; |
| 133 | + }); |
| 134 | + return newPose; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Given an image or video, returns an array of objects containing pose estimations |
| 139 | + * using single or multi-pose detection. |
| 140 | + * @param {HTMLVideoElement || p5.Video || function} inputOr |
| 141 | + * @param {function} cb |
| 142 | + */ |
| 143 | + async singlePose(inputOr, cb) { |
| 144 | + const { image: input, callback } = handleArguments(this.video, inputOr, cb); |
| 145 | + |
| 146 | + const pose = await this.net.estimateSinglePose(input, { |
| 147 | + flipHorizontal: this.flipHorizontal, |
| 148 | + }); |
| 149 | + const poseWithParts = this.mapParts(pose); |
| 150 | + const result = [ |
| 151 | + { pose: poseWithParts, skeleton: this.skeleton(pose.keypoints) }, |
| 152 | + ]; |
| 153 | + this.emit("pose", result); |
| 154 | + |
| 155 | + if (this.video) { |
| 156 | + return tf.nextFrame().then(() => this.singlePose()); |
| 157 | + } |
| 158 | + |
| 159 | + if (typeof callback === "function") { |
| 160 | + callback(result); |
| 161 | + } |
| 162 | + |
| 163 | + return result; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Given an image or video, returns an array of objects containing pose |
| 168 | + * estimations using single or multi-pose detection. |
| 169 | + * @param {HTMLVideoElement || p5.Video || function} inputOr |
| 170 | + * @param {function} cb |
| 171 | + */ |
| 172 | + async multiPose(inputOr, cb) { |
| 173 | + const { image: input, callback } = handleArguments(this.video, inputOr, cb); |
| 174 | + |
| 175 | + const poses = await this.net.estimateMultiplePoses(input, { |
| 176 | + flipHorizontal: this.flipHorizontal, |
| 177 | + maxDetections: this.maxPoseDetections, |
| 178 | + scoreThreshold: this.scoreThreshold, |
| 179 | + nmsRadius: this.nmsRadius, |
| 180 | + }); |
| 181 | + |
| 182 | + const posesWithParts = poses.map((pose) => this.mapParts(pose)); |
| 183 | + const result = posesWithParts.map((pose) => ({ |
| 184 | + pose, |
| 185 | + skeleton: this.skeleton(pose.keypoints), |
| 186 | + })); |
| 187 | + this.emit("pose", result); |
| 188 | + if (this.video) { |
| 189 | + return tf.nextFrame().then(() => this.multiPose()); |
| 190 | + } |
| 191 | + |
| 192 | + if (typeof callback === "function") { |
| 193 | + callback(result); |
| 194 | + } |
| 195 | + |
| 196 | + return result; |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +const poseDetection = (...inputs) => { |
| 201 | + const { |
| 202 | + video, |
| 203 | + options = {}, |
| 204 | + callback, |
| 205 | + string: detectionType, |
| 206 | + } = handleArguments(...inputs); |
| 207 | + return new PoseNet(video, options, detectionType, callback); |
| 208 | +}; |
| 209 | + |
| 210 | +export default poseDetection; |
0 commit comments