Flask Web服务,可以访问外部摄像机并发送视频流

时间:2018-10-01 22:18:06

标签: python web-services opencv flask video-streaming

首先,对于我英语水平不高的一切感到抱歉,我感谢所做的纠正。

我试图构建一个可以通过Flask Web服务访问摄像机的python程序,这是代码:

from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


def gen():
    i = 1
    while i < 10:
        yield (b'--frame\r\n'
               b'Content-Type: text/plain\r\n\r\n' + str(i) + b'\r\n')
        i += 1


def get_frame():
    camera_port = 0

    ramp_frames = 100

    camera = cv2.VideoCapture(camera_port)  # this makes a web cam object

    i = 1
    while True:
        retval, im = camera.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')
        i += 1

    del (camera)


@app.route('/calc')
def calc():
    return Response(get_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False, threaded=True)

和html代码:

<html>
  <head>
    <title>Video Streaming Demonstration</title>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
    <img src="{{ url_for('calc') }}">
    <!-- <h1>{{ url_for('calc') }}</h1> -->
  </body>
</html>

我的问题是当我在笔记本电脑中运行它并且Flask部署时。我从其他设备访问该服务,但不使用该设备的摄像头,而是激活了笔记本电脑的网络摄像头。

是否可以使服务使用我从中访问该设备(客户端)的设备的摄像头而不是笔记本电脑(服务器)的摄像头?

0 个答案:

没有答案