gpt4 book ai didi

javascript - 在不单击任何按钮的情况下从 Web 使用 Flask 运行 python 脚本

转载 作者:行者123 更新时间:2023-11-30 06:12:36 24 4
gpt4 key购买 nike

在我的应用程序中,我需要在网页内打开网络摄像头,还需要处理网络摄像头框架并将结果再次返回到网页。为此,我没有使用按钮。我在这里找到了如何在网页中使用网络摄像头

web cam in a webpage using flask and python

但是我无法将结果传递给网页。如何将结果传递到同一网页?无需单击任何按钮。就我而言,如何从网络调用 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: $ 未定义 在情节((指数):30) 在(索引):38

问题出在哪里?

最佳答案

您是否包含了对 jQuery 的引用?

(index):30 Uncaught ReferenceError: $ is not defined at plot ((index):30) at (index):38

关于javascript - 在不单击任何按钮的情况下从 Web 使用 Flask 运行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com