【发布时间】:2016-10-16 19:23:57
【问题描述】:
我在 OpenCV C++ 中编写了一个代码,该代码从命令行获取视频和时间戳(采用 HH:MM:SS 格式)作为输入,并检测该帧中的人脸。检测到的人脸被裁剪并写入 PNG 文件。
$ ./executable video.mp4 00:00:01
现在我想在 NodeJS 中为 executable 构建一个 Web API。我使用了child_process.spawn 和命令行参数。该应用程序运行没有任何错误,但我没有得到裁剪的图像作为输出。看来我的应用程序要么无法将输入视频传递给executable,要么无法从中读取输出。这是我的第一个 NodeJS 应用程序。这是我的代码。
/**
* app.js
* make sure to install express (npm install express)
* and place the executable beside this file
* or set executablePath to the correct path.
* run the app and open 'http://0.0.0.0:3000' in your browser.
*/
var child_process = require('child_process');
var path = require('path');
var express = require('express');
var app = express();
var executable = null;
app.get('/', function (req, res) {
const args = process.argv;
console.log(args);
//console.log(process.argv[3]);
if (process.argv.length <= 3) {
console.log("Usage: nodemon " + __filename + " <path/to/video> <HH:MM:SS>");
process.exit(-1);
}
if (executable !== null) {
res.end('executable is already running.');
return;
}
var executablePath = path.join(__dirname, './executable');
executable = child_process.spawn(executablePath, [process.argv[2], process.argv[3]]);
executable.on('exit', function (signal, code) {
console.log('executable exited with signal: %s and code: %d', signal, code);
executable = null;
});
res.end('Done.');
});
app.listen(3000);
console.log('Listening on port 3000');
我正在运行它
$ nodemon app.js video.mp4 00:00:01
我还需要知道如何从 API 本身传递参数 video.mp4 和 00:00:01。
【问题讨论】:
标签: c++ node.js rest opencv video