Hướng dẫn can laravel run python script? - laravel có thể chạy tập lệnh python không?

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

  1. Tên của kịch bản
  2. 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.

use Symfony\ Component\ Process\ Process;

use Symfony\ Component\ Process\ Exception\ ProcessFailedException;

public function testPythonScript()

{

$process = new Process(['python', '/path/to/your_script.py']);

$process->run();

if (!$process->isSuccessful()) {

throw new ProcessFailedException($process);

}

$data = $process->getOutput();

dd($data);

}

THẺ LIÊN QUAN

Laravel

PHP

Python

cộng đồng

Người đóng góp

Chinweuba Elijah Azubuike

Làm thế nào để Laravel tích hợp với Python?

Có một số cách bạn có thể đi về điều đó:..
Tìm kiếm một cổng Laravel hoặc PHP của gói Python. ....
Viết một cổng PHP của gói. ....
Bọc tập lệnh Python trong API REST REST chạy bằng Python bằng bình có thể được gọi từ ứng dụng Laravel. ....
Hãy để Laravel viết các văn bản để lưu trữ như A ..

Chúng ta có thể chạy mã python trong PHP không?

Thực thi lệnh qua shell và trả về đầu ra hoàn chỉnh dưới dạng chuỗi. Nó trả về đầu ra từ lệnh được thực thi hoặc null nếu xảy ra lỗi hoặc lệnh không tạo ra đầu ra.. It returns the output from the executed command or NULL if an error occurred or the command produces no output.

Tập lệnh Python có thể chạy trên Linux không?

Chạy một tập lệnh mở thiết bị đầu cuối bằng cách tìm kiếm nó trong bảng điều khiển hoặc nhấn Ctrl + Alt + T.Điều hướng thiết bị đầu cuối đến thư mục nơi tập lệnh được đặt bằng lệnh CD.Gõ python scriptName.py trong thiết bị đầu cuối để thực thi tập lệnh.Type python SCRIPTNAME.py in the terminal to execute the script.

Apache có thể chạy Python không?

Máy chủ Apache HTTP là một máy chủ web được triển khai rộng rãi có thể được sử dụng kết hợp với mô-đun WSGI, chẳng hạn như MOD_WSGI hoặc máy chủ WSGI độc lập để chạy các ứng dụng web Python.can be used in combination with a WSGI module, such as mod_wsgi or a stand-alone WSGI server to run Python web applications.