Accessing USB webcam via a PHP script on Linux/Apache

 ̄綄美尐妖づ 提交于 2019-12-13 04:42:46

问题


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.

  1. 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!
  2. To allows the application (that captures images from the camera) to store them in the folder /var/www/images I observed that the subfolder images 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 of images 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!