如何使用symfony进程在laravel中运行python函数?



我有一个python函数返回字符串数据,代码运行后运行良好

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="db_absensi"
)
mycursor = mydb.cursor() 

def example():
mycursor.execute("SELECT * FROM examples)
data = mycursor.fetchall()

return data

这是我的symfony代码

public function test()
{
$process = new Process(['python ../../../app/data.py']);
$process->setTimeout(3600);
$process->run();
if(!$process->isSuccessful())
{
throw new ProcessFailedException($process);
}
dd ($process->getOutput());

return view("testView");
}

还有另一个不返回数据的函数,我计划用flask

中的过程来调用这个函数
def face_recognition():  # generate frame by frame from camera
def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, clf):
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
features = classifier.detectMultiScale(gray_image, scaleFactor, minNeighbors)

global justscanned
global pause_cnt

pause_cnt += 1

coords = []

for (x, y, w, h) in features:
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
id, pred = clf.predict(gray_image[y:y + h, x:x + w])
confidence = int(100 * (1 - pred / 300))

if confidence > 70 and not justscanned:
global cnt
cnt += 1

n = (100 / 30) * cnt
w_filled = (cnt / 30) * w

cv2.putText(img, str(int(n))+' %', (x + 20, y + h + 28), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2, cv2.LINE_AA)

cv2.rectangle(img, (x, y + h + 40), (x + w, y + h + 50), color, 2)
cv2.rectangle(img, (x, y + h + 40), (x + int(w_filled), y + h + 50), (255,255,255), cv2.FILLED)

mycursor.execute("SELECT a.img_person, b.nama, b.kelas, b.tanggal_lahir "
" FROM images a "
" LEFT JOIN data_person b ON a.img_person = b.id_master "
" WHERE img_id = " + str(id))
row = mycursor.fetchone()
pnbr = row[0]
pname = row[1]
pkelas = row[2]

if int(cnt) == 30:
cnt = 0

mycursor.execute("INSERT INTO attendance_datamaster (attendance_date, attendance_person) VALUES('"+str(date.today())+"', '" + pnbr + "')")
mydb.commit()

cv2.putText(img, pname + ' | ' + pkelas, (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2, cv2.LINE_AA)
time.sleep(4)
# speech.say(pname + "successfully processed")
# speech.runAndWait()
justscanned = True
pause_cnt = 0

else:
if not justscanned:
cv2.putText(img, 'UNKNOWN', (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA)
else:
cv2.putText(img, ' ', (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2,cv2.LINE_AA)

if pause_cnt > 80:
justscanned = False

coords = [x, y, w, h]
return coords

def recognize(img, clf, faceCascade):
coords = draw_boundary(img, faceCascade, 1.1, 10, (255, 255, 255), "Face", clf)
return img

faceCascade = cv2.CascadeClassifier("resources/haarcascade_frontalface_default.xml")
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("classifier.xml")

wCam, hCam = 400, 400

cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)

while True:
ret, img = cap.read()
img = recognize(img, clf, faceCascade)

frame = cv2.imencode('.jpg', img)[1].tobytes()
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rnrn')

key = cv2.waitKey(1)
if key == 27:
break
def video_feed(): 
return Response(face_recognition(), mimetype='multipart/x-mixed-replace; boundary=frame')

我想把这个函数放在图像的src属性上(opencv函数)。这是我通常在flask

中做的
<div class="col-md-8 " style="margin-top: 10%;">
<img src="{{ url_for('video_feed') }}" width="100%" class="img-thumbnail">
</div>

是否有类似的方式,或者一种方式,可以运行一个python函数,不返回字符串数据在laravel环境?

对于你的第一个python脚本,我建议你简单地在php中重新创建mysql select。

对于def video_feed:就像Christoph在评论中提到的那样,这个返回值看起来像一个http响应。所以你把东西弄混了。可以简单地将return face_recognition()作为json并与python进程一起使用,或者将python程序作为http服务器启动,并从laravel向python发送http请求。

要使用symphony/process,最好使用多个参数和完整路径,如new Process(['/usr/bin/python3.6', '/var/www/app/data.py']);

最新更新