Ich versuche also, in meinem Laravel 5.3 ein Python-Skript auszuführen.
Diese Funktion befindet sich in meinem Controller. Dadurch werden Daten einfach an mein Python-Skript übergeben
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;
}
Mein Python-Skript ist ein ORB-Algorithmus, bei dem der kleinste Abstand und seine ID nach dem Vergleich der Zugbilder mit dem Abfragebild zurückgegeben wird. Das ist also mein Python-Skript:
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)
Ich habe bereits versucht, beide Dateien auszuführen, ohne Laravel zu verwenden, und es funktioniert gut. Als ich jedoch versuchte, den PHP-Code in meinen Laravel zu integrieren, wird nichts angezeigt. Der Statuscode ist 200 OK.
EDIT: Problem gelöst. In PHP Code ändern Sie einfach
$result = Shell_exec("C:\Python27\python c:\xampp\htdocs\identificare_api\app\http\controllers\ORB\orb.py " . escapeshellarg($string));
zu
$result = Shell_exec("python " . app_path(). "\http\controllers\ORB\orb.py " . escapeshellarg($string));
dann kannst du auch so machen
$queryImage = public_path() . "\gallery\herbs\query.png";
Verwenden Sie Symfony Process. https://symfony.com/doc/current/components/process.html
Installieren:
composer require symfony/process
Code:
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();
Die Lösung, die für mich funktionierte, bestand darin, am Ende 2> & 1 hinzuzufügen.
Shell_exec("python path/to/script.py 2>&1");
Das Problem, das ich hatte, war kein Fehler und keine Antwort, ich hatte den falschen Pfad zum Skript, aber keine Möglichkeit, es zu wissen. 2> & 1 leitet die Debug-Informationen als Ergebnis um.
Es wäre praktisch, Symfony Process zu verwenden. https://symfony.com/doc/current/components/process.html
Stellen Sie sicher, dass Symphony für Ihr Projekt verfügbar ist
composer show symphony/process
Wenn nicht installiert, composer require symfony/process
Und dann mache etwas wie
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
//$process = new Process('python /path/to/your_script.py'); //This won't be handy when going to pass argument
$process = new Process(['python','/path/to/your_script.py',$arg(optional)]);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();