问题
I want to use a USB webcam (in a small C++/OpenCV application) and call it from a PHP script. My test app looks like this:
int main( int argc, char **argv )
{
cv::VideoCapture C( 0 );
cout << "C.isOpened() = " << boolalpha << C.isOpened() << endl;
return 0;
}
The PHP script looks like this:
<?php
exec( escapeshellcmd( '/thepath/theapp' ), $output, $result );
var_dump( $output );
?>
When I call this app directly from the command line, it returns true.
When I call the app via php like this php -f /the_php_script.php
it returns true.
When I call the app via php called by AJAX from a html file (with a button), it passes through Apache and then returns false.
So, my app works fine. My php script works fine too. But there is a permission or something like this that prevents php, via Apache to access the webcam (to initialize it with OpenCV library), but the permission is sufficient to allow the app to run.
Could someone help me? Any idea?
Thanks!
EDIT 1:
Following a suggestion, I tried to used a php script to start my app without the use of AJAX. I obtained the same results, i.e. OpenCV cannot initialized the USB webcam on the server.
回答1:
For those whom may be interested by the problem...
The problem I was facing was two-fold: initializing the USB webcam via a php script called from the client and starting an application from this php script that could write images to disk in the folder /var/www/images
.
- To enable the USB camera, I just had to add the user www-data (Apache) to the group video. I found it by looking at my own account: I saw that I was a member of this group so it seemed natural to add Apache to it too!
- To allows the application (that captures images from the camera) to store them in the folder
/var/www/images
I observed that the subfolderimages
belonged to user root and was a member of group root. Since it was Apache that ran the php script that called my application, I switched the owner ofimages
to www-data.
As we say in French... Voilà!
Thanks to all of you who gave me some hints!
来源:https://stackoverflow.com/questions/20234449/accessing-usb-webcam-via-a-php-script-on-linux-apache