0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You mean I have to write full path of xml file?
Yes. I would try that.
Will windows do problem with imwrite function?
No idea. But you may want to use absolute path for imwrite too.
I got it. I'm trying it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.