我用这个命令编译我的c++文件:-
g++ main.cpp -o main pkg-config --cflags --libs opencv
这运行良好。现在,当我使用此命令“./main”通过命令行运行主文件时,它运行良好,并盯着我的网络摄像头。
但是当我在 php 中编写代码以通过 php 代码执行此文件时,它也运行但我的网络摄像头没有找到,并给我一个错误。 启动摄像头 找不到网络摄像头
我不知道这背后的原因是什么。 我正在放置我的文件的一些代码。
主要.cpp
CvMemStorage *storage = cvCreateMemStorage(0);
CvHaarClassifierCascade *cascade = (CvHaarClassifierCascade*)cvLoad("data/haarcascade_frontalface_default.xml");
printf("starting cam\n");
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
if(capture == 0)
{
printf("No webcam where found");
return 1;
}
CvScalar red = CV_RGB(250,0,0);
// counters used for frame capture
int happy_count = 0;
int sad_count = 0;
printf("starting recognition\n");
输出.php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo shell_exec('./main');
运行 php 时出错:- starting cam No webcam where found.
注意:-在终端上工作正常
最佳答案
这只是我的建议,但是你能检查一下在使用 PHP exec 时哪个用户正在执行程序吗?可能 apache(您正在使用它吗?)有自己的用户,该用户无权访问网络摄像头。我建议尝试以普通用户身份运行 apache 以检查是否有帮助,或者为您的 apache 用户授予网络摄像头访问权限。
在这方面您可以得到一些帮助。 https://serverfault.com/questions/125865/finding-out-what-user-apache-is-running-as
关于php exec 函数运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24053773/