2
因此,我試圖在我的Laravel 5.3中運行python腳本。在Laravel中運行python腳本
這個功能在我的控制器裏面。這只是將數據傳遞給我python腳本
public function imageSearch(Request $request) {
$queryImage = 'c:\\\xampp\\\htdocs\\\identificare_api\\\public\\\gallery\\\herbs\\\query.png'; //queryImage
$trainImage = 'c:\\\xampp\\\htdocs\\\identificare_api\\\public\\\gallery\\\herbs\\\2nd.png'; //trainImage
$trainImage1 = 'c:\\\xampp\\\htdocs\\\identificare_api\\\public\\\gallery\\\herbs\\\3rd.png';
$trainImage2 = 'c:\\\xampp\\\htdocs\\\identificare_api\\\public\\\gallery\\\herbs\\\4th.jpg';
$trainImage3 = 'c:\\\xampp\\\htdocs\\\identificare_api\\\public\\\gallery\\\herbs\\\1st.jpg';
$data = array
(
array(0, $queryImage),
array(1, $trainImage),
array(3, $trainImage1),
array(5, $trainImage2),
array(7, $trainImage3),
);
$count= count($data);
$a = 1;
$string = "";
foreach($data as $d){
$string .= $d[0] . '-' . $d[1];
if($a < $count){
$string .= ",";
}
$a++;
}
$result = shell_exec("C:\Python27\python c:\xampp\htdocs\identificare_api\app\http\controllers\ORB\orb.py " . escapeshellarg($string));
echo $result;
}
我的Python腳本是一個ORB算法,其中返回的最小距離和列車圖像進行比較的查詢圖像後,其ID。所以,這是我的python腳本:
import cv2
import sys
import json
from matplotlib import pyplot as plt
arrayString = sys.argv[1].split(",")
final = []
for i in range(len(arrayString)):
final.append(arrayString[i].split("-"))
img1 = cv2.imread(final[0][1], 0)
for i in range(1, len(arrayString)):
img2 = cv2.imread(final[i][1], 0)
# Initiate STAR detector
orb = cv2.ORB_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)
# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], None, flags=2)
if i == 1:
distance = matches[0].distance
else:
if distance > matches[0].distance:
distance = matches[0].distance
smallestID = final[i][0]
print str(smallestID) + "-" + json.dumps(distance)
我已經嘗試過不使用Laravel同時運行的文件,它運作良好。但是當我嘗試將php代碼集成到我的Laravel時,它什麼也沒有顯示。狀態碼是200 OK。
編輯:問題解決。在PHP代碼,只需更改
$result = shell_exec("C:\Python27\python c:\xampp\htdocs\identificare_api\app\http\controllers\ORB\orb.py " . escapeshellarg($string));
到
$result = shell_exec("python " . app_path(). "\http\controllers\ORB\orb.py " . escapeshellarg($string));
那麼,你也可以這樣做
$queryImage = public_path() . "\gallery\herbs\query.png";
我編輯上面我的問題。我已經無法再使用Symfony Process了。不管怎麼說,還是要謝謝你! –