2016-12-07 3 views
2

그래서 Laravel 5.3에서 python 스크립트를 실행하려고합니다.Laravel의 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; 
} 

내 파이썬 스크립트는 작은 거리와 질의 이미지에 기차 이미지를 비교 한 후 해당 ID를 반환하는 ORB 알고리즘에 데이터를 전달합니다. 그래서, 이것은 내 파이썬 스크립트입니다 :

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"; 

답변

7

사용 심포니 프로세스처럼 할 수있는 변경합니다. https://symfony.com/doc/current/components/process.html

설치 :

composer require symfony/process 

코드 :

내가 위에서 내 질문을 편집
use Symfony\Component\Process\Process; 
use Symfony\Component\Process\Exception\ProcessFailedException; 

$process = new Process('python /path/to/your_script.py'); 
$process->run(); 

// executes after the command finishes 
if (!$process->isSuccessful()) { 
    throw new ProcessFailedException($process); 
} 

echo $process->getOutput(); 
+0

. 나는 당신이 제안한 Symfony Process를 더 이상 사용할 수 없었습니다. 어쨌든 고마워! –