opencv c ++,cvSaveImage 从 php 执行程序时不保存


opencv c++, cvSaveImage not saving when program is executed from php

我正在OS X(狮子)上使用opencv c ++(eclipse)编写一个人脸检测应用程序。 该程序加载图像文件,检测人脸,然后将每个人脸保存到单独的文件中。 该程序 100% 工作,因为它是从命令行或在 eclipse 中。 然后我编写了一个用于文件上传的 php 脚本,以便有人可以上传 jpg 或 png 文件,然后脚本使用唯一的文件名保存照片。 然后在 php 脚本结束时,我让它执行人脸检测程序。

我不确定从命令行运行应用程序并让 php 调用应用程序运行有什么区别。 这是我下面的代码。

C++ 中的函数:

    cvNamedWindow ("ROI", CV_WINDOW_AUTOSIZE);
    cvCvtColor( clone, gray, CV_RGB2GRAY );

    cvSetImageROI ( gray, *r);
    //// * rectangle  = cvGetImageROI ( clone );
    *r = cvGetImageROI ( gray );
    cvShowImage ("ROI", gray);
    k++;
    char *name=0;
    name=(char*) calloc(512, 1);
    sprintf(name, "/Users/jason/Sites/jason.dev/images/proc_images/Image%d.png", k);
    cvSaveImage(name, gray);

PHP脚本:

<?php
mysql_connect("127.0.0.1:3306","xxxx","xxxx");
mysql_select_db("opencv_development");

$uploaddir = "images/";
$file = basename($_FILES['uploadedfile']['name']);
$stamp = date("YmdHis");
$random = rand(0, 999);
$newName = $uploaddir . $stamp . $random . $file;

if ($_FILES['uploadedfile']['size']> 300000)     //Limiting image at 300K
{
exit("Your file is too large."); 
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $newName)) {
echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
mysql_query("INSERT INTO recv_img (photo_name) VALUES ('$newName')");

shell_exec("./opencv '$newName'");
?>

正如我上面所说,如果我从终端运行应用程序,那么它会输出单个人脸文件。当它从php脚本执行时,图像加载到应用程序中,它执行人脸检测,但不保存单个人脸文件。

如果需要任何进一步的信息,或者我不清楚某事,请告诉我。 任何帮助将不胜感激!

cvSaveImage返回的整数可以帮助您查找是否存在错误。
在测试和查看OpenCV源代码后,似乎最新版本不使用cvSetErrStatus来处理文件错误,但errno似乎设置正确。

if(!cvSaveImage(name, gray)) {
    int error = cvGetErrStatus();
    const char * errorMessage = 0;
    if (error) {
        errorMessage = cvErrorStr(error);
    } else {
        error = errno;                   // needs #include <cerrno>
        errorMessage = strerror(error);  //       #include <cstring>        
    }
    std::cout << errorMessage << std::endl;  
    // with "echo shell_exec("./opencv '$newName'");" in php
}

如果返回的错误是"权限被拒绝",您可以检查用户是否真的是您认为的用户,然后echo shell_exec('whoami');