justadudewhohacks / opencv4nodejs

Nodejs bindings to OpenCV 3 and OpenCV 4
MIT License
4.94k stars 821 forks source link

Natural feature tracking(NFT)/ Feature Matching in videostream. VERY SLOW SPEED #637

Open SystemDiagnosticss opened 4 years ago

SystemDiagnosticss commented 4 years ago

Hello. I want to run Feature Matching example with video stream, it is possible? This lib work fine with videostream in examples Object Tracking, Face Recognition, Face Detection eth .. But what about Natural feature tracking(NFT) in video stream ??

SystemDiagnosticss commented 4 years ago

Ok .. I do it. I use socket.io for streaming video to web page. But video streaming speed is to low. When I use SIFT with one frame per second its work. But If I use higher speed like 5 frame/second video stream has big lags and eventually stops. Also I tested ORB detector it is a bit faster but not enough.
I show it in video

Thats my code for index.js:

const cv = require('opencv4nodejs');
const path = require('path');
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

const matchFeatures = ({ img1, img2, detector, matchFunc }) => {
  // detect keypoints
  const keyPoints1 = detector.detect(img1);
  const keyPoints2 = detector.detect(img2);
  // compute feature descriptors
  const descriptors1 = detector.compute(img1, keyPoints1);
  const descriptors2 = detector.compute(img2, keyPoints2);
  // match the feature descriptors
  const matches = matchFunc(descriptors1, descriptors2);
  // only keep good matches
  const bestN = 40;
  const bestMatches = matches.sort(
    (match1, match2) => match1.distance - match2.distance
  ).slice(0, bestN);

  return cv.drawMatches(
    img1,
    img2,
    keyPoints1,
    keyPoints2,
    bestMatches
  );
};

FPS = 1;

const img1 = cv.imread('data/s0.jpg');
const vCap = new cv.VideoCapture('data/bodybook.mp4');

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'page/index.html'));
});

setInterval(() => {
    const frame = vCap.read();
  if (frame.empty) {
      vCap.reset();
      frame = vCap.read();
  }
  //const bf = new cv.BFMatcher(cv.NORM_L2, true);
    const img2 = frame;
  const siftMatchesImg = matchFeatures({
    img1,
    img2,
    detector: new cv.SIFTDetector({ nFeatures: 2000 }),
    matchFunc: cv.matchFlannBased
  });   
    const outBase64 =  cv.imencode('.jpg', siftMatchesImg).toString('base64');
    io.emit('image', outBase64);
},1000)

server.listen(3000, () => console.log('Videostream app listening on port 3000!'));

@justadudewhohacks In your examples whit Object Tracking, Face Recognition, Face Detection eth .. videostreaming speed is much higer. I can't understand really SIFT/ORB computing are more harder than in these examples??? Any idea ?

YMZnew commented 4 years ago

@SystemDiagnosticss Hi, i have the same problem . i want to add homography for tracking the image and overlap some another image above it as an augmentes reality . but as I tested in android browser , speed was disappointing . how facial or other object trackings are faster ?! @justadudewhohacks have you find any solution ?