简体   繁体   English

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

[英]Running Python openCV code from PHP isn't working properly

I have some OpenCV python code to capture images and save it to disk. 我有一些OpenCV python代码来捕获图像并将其保存到磁盘。 This code is working fine when I run it from cmd or from PowerShell it works fine. 当我从cmd或PowerShell中运行该代码时,它工作正常。 But when I run it from PHP it runs but not works properly. 但是,当我从PHP运行它时,它却可以正常运行。 Here is my python code: 这是我的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)
    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代码:

<?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 :) 提前致谢 :)

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. 当您使用相对路径时,它将尝试在当前目录中搜索xml文件。

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. 如果php / web用户没有写权限,它将失败。 But I guess it should be OK on Windows. 但是我想在Windows上应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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