im试图对由php脚本传递到python脚本的照片进行人脸检测,该脚本使用openCV检测人脸并返回包含坐标的json。但是,从php脚本调用但从CLI调用时,python脚本什么也不返回,因此python脚本可以完美地工作。
php脚本:
public function detectFaceOCV($photo){
$faces=[];
$currentFace=0;
$scriptLocation="/var/www/html/photos/face_detect_oCV.py";
$command="python ".$scriptLocation." ".$photo;
$exec = exec($command,$output,$status);
var_dump($output);
echo "<p/>";
}
$photo="/var/www/html/photos/ianmukunya.jpg";
detectFaceOCV($photo)
python脚本:
import sys
import numpy
import urllib
import cv2
import json
imagePath = sys.argv[1]
cascPath = "haarcascade_frontalface.xml"
snapArray=imagePath.split("/")
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
photograph=[]
faces=0
nyuso=[]
if urlimage == None:
print ("no faces")
else:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.4,minNeighbors=5, minSize=(130, 130),flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
for (x, y, w, h) in faces:
#cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
nyuso.append({"x":x,"y":y,"w":w,"h":h})
else:
print(nyuso)
输出:
[]
从photos文件夹中的CLI运行python脚本时的输出:
[imukunya@li165-244 photos]$ python face_detect_oCV.py ianmukunya.jpg
[{'y': 33, 'x': 120, 'w': 253, 'h': 253}]