douzhang1926
2018-06-08 10:44
浏览 215
已采纳

从php执行的python脚本将无法读取已保存的模型

Here is what I'm trying to do: I'm sending an Ajax request with an image as data to a php script that uploads it and execute a python script with the image name the python script loads a gender detection model and predict the gender on the person on the image.

The problem is the model doesn't load, I tried it with the php and python script in the same directory and it worked fine but I'm trying to use it in laravel so I can't do that anymore.

when I try running the python script manually like that :

python path/to/script.py 

It throws this error : OpenCV(3.4.1) Error: Unspecified error (File can't be opened for reading!)

but when I do this it works:-

cd path/to/script.py ; python script.py

Here is my php:-

    $file = $this->upload_Image($request->get('image'));
    $path =public_path().'/app/Http/Controllers';
    $command='cd '.$path.';';
    $path= 'python '.$path.'/facifier.py '.storage_path('app').$file;

    $command .= $path;
    return exec($command);

    Storage::disk('local')->delete($file);
    return json_encode($orderedData);

The command ends up like this : cd absolute/path/to/script.py ; python script.py absolute/path/to/image.jpg when I use it on a command prompt it works fine. Here is where I get the error on my python script:-

fisher_face_gender = cv2.face.FisherFaceRecognizer_create()
fisher_face_gender.read('models/gender_classifier_model_kdef.xml')

does anyone know whats causing this, I'm on windows by the way.

图片转代码服务由CSDN问答提供 功能建议

这是我正在尝试做的事情: 我发送带有图像作为数据的Ajax请求 一个php脚本,上传它并执行带有图像名称的python脚本,python脚本加载性别检测模型并预测图像上人物的性别。

问题是模型 不加载,我尝试使用相同的目录中的PHP和python脚本,它工作正常,但我试图在laravel中使用它,所以我不能再这样做了。 < p>当我尝试手动运行python脚本时:

  python path / to / script.py 
   
 
 <  p>它抛出此错误: OpenCV(3.4.1)错误:未指定错误(无法打开文件进行读取!)  
 
 

但是当我这样做时 这有效: -

  cd path / to / script.py;  python script.py 
   
 
 

这是我的php: -

  $ file = $ this-&gt; upload_Image  ($ request-&gt; get('image')); 
 $ path = public_path()。'/ app / Http / Controllers'; 
 $ command ='cd'。$ path。';'; 
  $ path ='python'。$ path。'/ facifier.py'.storage_path('app')。$ file; 
 
 $ command。= $ path; 
 return exec($ command); 
 \  n存储::磁盘('本地') - &gt;删除($ file); 
返回json_encode($ orderedData); 
   
 
 

该命令最终会像 这: cd absolute / path / to / script.py; python script.py absolute / path / to / image.jpg 当我在命令提示符下使用它时它工作正常。 这是我在python脚本上出错的地方: -

  fisher_face_gender = cv2.face.FisherFaceRecognizer_create()
fisher_face_gender.read('models / gender_classifier_model_kdef.xml')  
   
 
 

有谁知道造成这种情况的原因,顺便说一句,我在Windows上。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题