dongwen7730
2017-04-21 03:21
浏览 95

从PHP运行Python openCV代码无法正常工作

I have some OpenCV python code to capture images and save it to disk. This code is working fine when I run it from cmd or from PowerShell it works fine. But when I run it from PHP it runs but not works properly. Here is my python code:

import cv2, sys, json
import numpy as np

faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)    

Id = 1
i = 1

while (True):
    ret, img = cam.read()
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceDetect.detectMultiScale(grayImg, 1.3, 5)

    for (x, y, w, h) in faces:
        cv2.imwrite("dataset/user_" + str(Id) + "_" + str(i) + ".jpg", grayImg[y : y + h, x : x + w])
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        i += 1
        cv2.waitKey(100)

    cv2.imshow("Camera", img)
    cv2.waitKey(1)

    if (i > 20):
        break

cam.release()
cv2.destroyAllWindows()

Here is my PHP code:

<?php
    exec('C:\\Python27\\python.exe C:\\xampp\\htdocs\\atmp\\face_recognition\\dataset_creator.py');
?>

Is there any specific reason to not working properly? Any answer will be appreciated. Thanks in advance :)

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

我有一些OpenCV python代码来捕获图像并将其保存到磁盘。 当我从cmd或PowerShell运行它时,此代码工作正常它工作正常。 但是当我从PHP运行它时,它运行但不能正常工作。 这是我的python代码:

  import cv2,sys,json 
import numpy as np 
 
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam =  cv2.VideoCapture(0)
 
Id = 1 
i = 1 
 
while(True):
 ret,img = cam.read()
 grayImg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\  n faces = faceDetect.detectMultiScale(grayImg,1.3,5)
 
表示面中的(x,y,w,h):
 cv2.imwrite(“dataset / user _”+ str(Id)+“_”  + str(i)+“.jpg”,grayImg [y:y + h,x:x + w])
 cv2.rectangle(img,(x,y),(x + w,y + h),  (0,255,0),2)
i + = 1 
 cv2.waitKey(100)
 
 cv2.imshow(“Camera”,img)
 cv2.waitKey(1)
 
 if  (i> 20):
 break 
 
cam.release()
cv2.destroyAllWindows()
   
 
 

这是我的PHP代码:

 &lt;?php 
 exec('C:\\ Python27 \\ python.exe C:\\ xampp \\ htdocs \\ atmp \\ face_recognition \\ dataset_creator.py'  ); 
?&gt; 
   
 
 

有什么特殊原因导致无法正常工作吗? 任何答案将不胜感激。 在此先感谢:)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpao5127 2017-04-21 06:30
    已采纳

    You should say what went wrong. But I bet it is

    faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    

    that doesn't run. When you use relative path, it will try to search in the current directory for the xml file.

    Another thing that can go wrong is

    cv2.imwrite("dataset/user_" + str(Id) + "_" + str(i) + ".jpg", grayImg[y : y + h, x : x + w])
    

    It will fail if php/web user doesn't have write privilege. But I guess it should be OK on Windows.

    打赏 评论

相关推荐 更多相似问题