HandNN : Hand and finger detection and recognition

更新时间:
2024-05-15
下载文档

HandNN : Hand and finger detection and recognition

The handnn module provides hand detection and recognition function.

User can use the following code to import the handnn module.

var handnn = require('handnn');

Support

The following shows handnn module APIs available for each permissions.

 User ModePrivilege Mode
handnn.detect
handnn.identify

handnn Object

handnn.detect(videoBuf, attribute)

  • videoBuf {Buffer} Video buffer.
  • attribute {Object} Video attribute.
  • Returns: {Array} Hand info objects array which detectd.

Detect hand infos in given video buffer.

The video attribute attribute object contains the following members:

  • width {Integer} Video width.
  • height {Integer} Video height.
  • pixelFormat {Integer} Pixel format.

pixelFormat is a integer, can be:

ValueDescription
handnn.PIX_FMT_BGR24BGR24 pixel format.
handnn.PIX_FMT_RGB2BGR24RGB24 to BGR24 pixel format.
handnn.PIX_FMT_GRAY2BGR24Grayscale to BGR24 pixel format.
handnn.PIX_FMT_RGBA2BGR24RGBA to BGR24 pixel format.

The returned hand info object contains the following members:

  • prob {Number} Probability 0.0 ~ 1.0.
  • x0 {Integer} x position of upper left corner.
  • y0 {Integer} y position of upper left corner.
  • x1 {Integer} x position of lower right corner.
  • y1 {Integer} y position of lower right corner.

handnn.identify(videoBuf, attribute, handInfo)

  • videoBuf {Buffer} Video buffer.
  • attribute {Object} Video attribute.
  • handInfo {Object} Hand info object.
  • Returns: {Object} Hand feature objects which identified.

Identify the hand feature of given hand info.

The return value object contains the following information:

  • base {Object} Hand base information.
    • x {Integer} x position of upper left corner.
    • y {Integer} y position of upper left corner.
  • fingers {Array} An array of feature objects for each finger.

Each finger featrue object has the following members:

  • points {Array} Finger four point coordinates.
  • curl {Boolean} Whether the fingers are curled up.

Each finger points object has the following members:

  • x {Integer} x position of upper left corner.
  • y {Integer} y position of upper left corner.

Example

{
  base: {
    x: ...,
    y: ...
  },
  fingers: [
    {
      points: [
        { x: ..., y: ... },
        { x: ..., y: ... },
        { x: ..., y: ... },
        { x: ..., y: ... }
      ],
      curl: true or false
    },
    ... // five fingers
  ]
}

Example

This example show how to detect and identify hand.

var mediadecoder = require('mediadecoder');
var iosched = require('iosched');
var handnn = require('handnn');

var netcam =  mediadecoder.open('rtsp://admin:admin@10.4.0.12');
if (netcam == undefined) {
  console.error('Can not connect camera!');
  return;
}

netcam.destVideoFormat({width: 640, height: 360, fps: 1, pixelFormat: mediadecoder.PIX_FMT_BGR24, noDrop: false, disable: false});
netcam.destAudioFormat({disable: true});
netcam.previewFormat({enable: true, fb: 0, fps: 25, fullscreen: false});

var ol = netcam.overlay();
var quited = false;
var colors = [mediadecoder.C_GREEN, mediadecoder.C_BLUE, mediadecoder.C_YELLOW, mediadecoder.C_WHITE, mediadecoder.C_MAGENTA];

netcam.on('video', (video) => {
  var buf = new Buffer(video.arrayBuffer);
  var hands = handnn.detect(buf, {width: 640, height: 360, pixelFormat: handnn.PIX_FMT_BGR24});

  ol.clear();
  if (hands.length) {
    for (var i = 0; i < hands.length; i++) {
      var info = hands[i];
      if (info.prob > 0.9) {
        // draw rectangle
        ol.rect(info.x0, info.y0, info.x1, info.y1, mediadecoder.C_YELLOW, 2, 0, false);

        var hand = handnn.identify(buf, {width: 640, height: 360, pixelFormat: handnn.PIX_FMT_BGR24}, info);
        if (hand) {
          // draw base point
          ol.circle(hand.base.x, hand.base.y, 2, mediadecoder.C_RED, 1, true);

          // draw five fingers
          for (var j = 0; j < 5; j++) {
            var lcolor = colors[j];
            var pcolor = hand.fingers[j].curl ? mediadecoder.C_RED : mediadecoder.C_GRAY;

            // draw finger line
            for (var k = 0; k < 3; k++) {
              var pstart = { x : hand.fingers[j].points[k].x, y: hand.fingers[j].points[k].y };
              var pend   = { x : hand.fingers[j].points[k + 1].x, y: hand.fingers[j].points[k + 1].y };
              ol.line(pstart.x, pstart.y, pend.x, pend.y, lcolor, 2);
            }

            // draw finger point
            for (var k = 0; k < 4; k++) {
              ol.circle(hand.fingers[j].points[k].x, hand.fingers[j].points[k].y, 2, pcolor, 1, true);
            }
          }
        }
      }
    }
  }
});

netcam.on('eof', () => {
  quited = true;
});

netcam.start();

while (!quited) {
  iosched.poll(); // Event poll.
}

netcam.close();
文档内容是否对您有所帮助?
有帮助
没帮助