ฉันยังใหม่กับการใช้ Flask และฉันเพิ่งพยายามส่งตัวแปรไปยังหน้าเว็บ ฉันรู้ว่าตัวแปร pass ในเมธอด render_template เป็นอย่างไร
แต่ตอนนี้ฉันกำลังลองใช้แอปสตรีมมิ่งแบบเรียลไทม์ โดยใช้วิธีการที่แตกต่างกัน เช่น .responses และ allowances
รหัสขวดสำหรับการสตรีมเว็บแคมมีดังต่อไปนี้
@app.route('/')
def index():
return render_template('index.html')
def get_frame():
video=cv2.VideoCapture(0) #this makes a web cam object
while True:
ret, frame = video.read()
imgencode=cv2.imencode('.jpg',im)[1]
stringData=imgencode.tostring()
yield (b'--frame\r\n'
b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n')
del(video)
@app.route('/calc')
def calc():
return Response(get_frame(),mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='localhost', debug=True, threaded=True)
และเทมเพลตก็คือ
<html>
<head>
<title>Video Streaming Demonstration</title>
</head>
<body>
<h1>Video Streaming Demonstration</h1>
<img src="{{ url_for('calc') }}">
</body>
</html>
ตอนนี้ฉันต้องการเพิ่มป้ายกำกับบนเทมเพลตของฉันเช่น <label>{{ value }}</label>>
ฉันได้คำนวณค่าตัวแปรจากขวดทดลองแล้วและสามารถพิมพ์บนคอนโซลได้ แต่ตอนนี้ฉันต้องส่งค่านั้นไปยังป้ายกำกับบนเทมเพลตเดียวกันกับที่กำลังสตรีมวิดีโอ กรุณาช่วย