简体   繁体   中英

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

I am programming a face detection app using opencv c++(eclipse) on os X (lion). The program loads an image file, detects the faces, and then saves each individual face into separate files. The program works 100% as it is from a command line or in eclipse. I then wrote a php script for file upload so someone can upload a jpg or png file then the script saves the photo with a unique file name. Then at the end of the php script I have it execute the face detection program.

I am not sure what the difference is in running the application from a command line and having a php call the application to run. Here is my code below.

function in 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 script:

<?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'");

?>

As I said above, if I run the application from terminal then it outputs the individual face files. When it is executed from the php script, the image loads into the application, it does the face detection, but it does not save the individual face files.

Let me know if any further information is needed or if I was not clear about something. Any help is greatly appreciated!

The integer returned by cvSaveImage can help you find if there was an error.
After testing and looking at OpenCV source code, it seems that recent versions don't use cvSetErrStatus for file errors, but errno seems to be correctly set.

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
}

If the error returned is "Permission denied", you could check if the user is actually the one you think it is with and echo shell_exec('whoami'); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM