简体   繁体   中英

Running Python openCV code from PHP isn't working properly

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 :)

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.

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