[英]How to run a python script from clicking a button on a HTML webpage?
[英]Run python script with flask from web without clicking any button
在我的应用程序中,我需要打开网页内的网络摄像头,还需要处理网络摄像头框架并将结果再次返回到网页。 为此,我不使用按钮。 我在这里找到了如何在网页中使用网络摄像头
但是,我无法将结果传递给网页。 如何将结果传递到同一网页? 无需单击任何按钮。 就我而言,如何从Web调用index2()函数?
from flask import Flask, render_template, Response, jsonify
from camera import VideoCamera
import cv2
app = Flask(__name__)
video_stream = VideoCamera()
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(video_stream),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/print2')
def index2():
print2 = video_stream.get_print()
print("zzzz",print2)
return render_template('gui.html', printTo=print2)
if __name__ == '__main__':
app.run(host='127.0.0.1', debug=True,port="5000")
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
ret, frame = self.video.read()
# DO WHAT YOU WANT WITH TENSORFLOW / KERAS AND OPENCV
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
<!DOCTYPE html>
<html lang="en">
<head>
<title>Video Stream</title>
</head>
<body>
<img src="{{ url_for('video_feed') }}" />
</body>
</html>
在camera.py脚本中,我可以处理框架,并且可以打印结果以进行记录,但是我需要将这些结果传递给网页。
我做了很多事情,没有一个为我工作!
请帮我。
谢谢。
据评论所言,据我所知我可以使用AJAX做到这一点,我添加了此脚本,但是现在脚本给出了错误
<script type="text/javascript" >
function plot() {
$.ajax({
url: '/print2',
success: function(data) {
console.log('get info');
$('#description').html(data['description']);
}
});
}
plot()
</script>
它说
(index):30 Uncaught ReferenceError:未在(index):38的图((index):30)上定义$
问题出在哪儿 ?
您是否包含对jQuery的引用?
(index):30 Uncaught ReferenceError: $ is not defined at plot ((index):30) at (index):38
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.