javascript - 使用来自 web 的烧瓶运行 python 脚本,而无需单击任何按钮
问题描述
在我的应用程序中,我需要在网页内打开网络摄像头,还需要处理网络摄像头框架并将结果再次返回到网页。为此,我没有使用按钮。我在这里找到了如何在网页中使用网络摄像头
但是我无法将结果传递给网页。如何将结果传递到同一个网页?无需点击任何按钮。就我而言,如何从 web 调用 index2() 函数?
Python
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")
Python 相机.py
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()
HTML
<!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: $ is not defined at plot ((index):30) at (index):38
问题出在哪里 ?
解决方案
您是否包含了对 jQuery 的引用?
(index):30 Uncaught ReferenceError: $ is not defined at plot ((index):30) at (index):38
推荐阅读
- makefile - 从 makefile 运行 python 脚本并将 python 脚本的输出返回到 makefile 中的变量
- python-3.x - Python SnowflakeOperator 设置 snowflake_default
- python - 如何在 Python 中使用 VirusTotal 中的 API 扫描文件
- python - 如何在循环中跳过 NaN 值?
- haskell - 有没有办法从 haskell 数据类型生成 dhall 模式?
- css - 动画 SVG 线
- linux-kernel - Linux 内核序列“show”例程的返回值
- java - Java通过浏览器导出Excel
- python - 无法让我的脚本向右滑动按钮
- python - 列表中列表的随机播放功能