从web运行带有flask的python脚本,无需单击任何按钮

2024-04-23 11:14:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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

web cam in a webpage using flask and 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>

摄像头.py脚本我可以处理框架,我可以将结果打印到日志,但我需要将这些结果传递到网页。你知道吗

我做了很多事,但没有一件对我有用!你知道吗

请帮帮我。你知道吗

谢谢你。你知道吗

编辑

正如评论所说,据我所知,我可以用AJAX来实现它,我添加了这个脚本,但是现在这个脚本出现了错误

<script type="text/javascript" >
     function plot() {


    $.ajax({
        url: '/print2',
        success: function(data) {
            console.log('get info');

            $('#description').html(data['description']);
        }
    });


}

   plot()
</script>

上面写着

(索引):30未捕获的引用错误:$未定义 绘图时((索引):30) at(索引):38

问题出在哪里?你知道吗


Tags: importselfapp网页getreturndefhtml