0

I compile my c++ file by this command:-

g++ main.cpp -o main pkg-config --cflags --libs opencv

This runs fine. Now when i run main file by command line using this command './main', it's runs fine, and stared my webcam.

But when i write code in php to execute this file by php code, It's also runs but my webcam does not found, And gives me an error. starting cam No webcam where found

I don't know what's the reason behind this. I am putting some piece of code of my files.

main.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");

output.php

  error_reporting(E_ALL);
  ini_set('display_errors', 1);
  echo shell_exec('./main');

Error on run php:- starting cam No webcam where found.

Note:-Works fine on terminal

2 Answers 2

2

That's my suggestion only, but could you check which user is executing programs while using PHP exec? Probably apache (are you using that?) has own user, which has no access to webcam. I suggest to try running apache as regular user to check if that helps, or give access to webcam for your apache user.

Here you have some help in that matter. https://serverfault.com/questions/125865/finding-out-what-user-apache-is-running-as

Sign up to request clarification or add additional context in comments.

Comments

1

Seems like permissions issue to me. When you execute the program through php script, it is executed by the user that runs the server process. I am guessing you are using apache2 webs server to serve php content, which runs as user www-data in Ubuntu. If this is true, you just add the user www-data to the group video.

sudo usermod -a -G video www-data

If you are using another web server or a different linux distribution, or your web server runs with a different user, you need to modify the command accordingly.

6 Comments

Ya, Above both answers are right. Now showing 'starting cam starting recognition', But when i run on terminal its open a pop-up, In which webcam video shown, But when i run by php, No pop-up shown on my machine.
As mrarm suggested, did you try running the apache process as yourself, instead of the default www-data user?
Also, I see that you have error reporting enabled. Could you look into access and error logs for the server?
Ya, I run this command 'sudo adduser {username} www-data' but pop-up window of webcam not showing.
Ya, may be there is a issue of permission, When i run 'whoami' command on terminal its print 'east', But when i run this command by php code it's shown 'www-data'. Please tell me may be this is the reason my webcam not showing video pop-up.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.