Vì vậy, tôi đang cố gắng chạy một kịch bản Python trong Laravel 5.3 của tôi.
Hàm này nằm trong bộ điều khiển của tôi. Điều này chỉ đơn giản là truyền dữ liệu cho tập lệnh Python của tôi
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;
}
Kịch bản Python của tôi là một thuật toán quả cầu trong đó nó trả về khoảng cách nhỏ nhất và ID của nó sau khi so sánh hình ảnh tàu với hình ảnh truy vấn. Vì vậy, đây là kịch bản Python của tôi:
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]
Tôi đã thử chạy cả hai tệp mà không sử dụng Laravel và nó đang hoạt động tốt. Nhưng khi tôi cố gắng tích hợp mã PHP vào Laravel của tôi, nó không hiển thị gì. Mã trạng thái là 200 OK.
Chỉnh sửa: Vấn đề đã được giải quyết. Trong mã PHP, chỉ cần thay đổi: Problem Solved. In PHP code, just change
$result = shell_exec["C:\Python27\python c:\xampp\htdocs\identificare_api\app\http\controllers\ORB\orb.py " . escapeshellarg[$string]];
đến
$result = shell_exec["python " . app_path[]. "\http\controllers\ORB\orb.py " . escapeshellarg[$string]];
Sau đó, bạn cũng có thể làm như thế này
$queryImage = public_path[] . "\gallery\herbs\query.png";
Chinweuba Elijah Azubuike
Python cung cấp một số chức năng có thể giúp cải thiện tính hữu ích của các ứng dụng của bạn. Python giúp xử lý các chức năng như kiểm tra độ mờ hình ảnh.
Bước 1
Chạy lệnh Composer để cài đặt gói như sau:
composer require symfony / process
Bước 2
Nhận kịch bản Python của bạn. Trong ảnh này, chúng tôi giả sử Test.py là tập lệnh Python 3 mà chúng tôi muốn chạy.
Bước 3
Trong bức ảnh này, chúng tôi sẽ sử dụng lớp Process
. Laravel vận chuyển với một lớp Process
cho phép Laravel chạy tập lệnh.
Thí dụ
Mã bên dưới tạo ra một thể hiện mới của lớp Process
, lấy hai tham số:
- Tên của kịch bản
- Kịch bản chính nó
Tập lệnh sau đó được thực thi với phương thức
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]
0 trên 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]
1.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]
1 được kiểm tra nếu nó chạy thành công. Trong bức ảnh này, chúng tôi chỉ cần đổ đầu ra bằng phương pháp 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]
3.