dongwen7730 2017-04-21 03:21
浏览 112
已采纳

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

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug