在带有视频流的烧瓶中呈现页面

时间:2015-08-12 07:03:43

标签: python-2.7 video flask

我正在尝试从我的笔记本电脑的相机实现一个实时相机输入到this tutorial之后的Flask网站。

如果我在没有Flask的任何MTV框架的情况下运行应用程序,应用程序运行时没有错误,我可以看到流。但是当我将这个应用程序包含在我的MTV Flask项目中时,我看不到提要和应用程序出现以下错误:

    (niravflask) C:\flaskproj\niravflask\svsflask>python svs.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
127.0.0.1 - - [12/Aug/2015 12:16:21] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2015 12:16:28] "GET /reguser HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2015 12:16:36] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2015 12:16:44] "GET /reguser HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2015 12:16:59] "GET /stream HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 20347)
Traceback (most recent call last):
  File "C:\Users\Nirav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\Lib\SocketServer.py", line 295, in _handle_request_nobl
ock
    self.process_request(request, client_address)
  File "C:\Users\Nirav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\Lib\SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "C:\Users\Nirav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\Lib\SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Nirav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\Lib\SocketServer.py", line 657, in __init__
    self.finish()
  File "C:\Users\Nirav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\Lib\SocketServer.py", line 716, in finish
    self.wfile.close()
  File "C:\Users\Nirav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\Lib\socket.py", line 279, in close
    self.flush()
  File "C:\Users\Nirav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\Lib\socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine

我不确定我的Flask MTY项目有什么问题。

这是我的Flask svs.py文件,它是主文件。即使我在流路由中删除r ender_template`调用并仅保留响应它仍然无效。

__author__ = 'Nirav'
#
from flask import Flask,render_template, url_for,request,redirect,flash,Response
from datetime import datetime
#from logging import DEBUG
from camera import VideoCamera

#Calls flask constructor for global application object
app = Flask(__name__)
#app.logger.setLevel(DEBUG)
app.config['SECRET_KEY']='\x98.\x80\xba1\xcc\x0cU\xd0\xdb\xd8\x9c8\x0e\xb1EgA\xb6\xde\x84\xcby\xf8\xb5\xed\xe5E\xaav\xed\x16'
userregister = []

def storuser(em,pass1,pass2,fname,lname):
    userregister.append(dict(email=em,password1=pass1,password2=pass2,FirstName=fname,LastName=lname,datet = datetime.utcnow()
                             )
                        )

#This is view function
@app.route('/')
@app.route('/index')
@app.route('/Index')
#This your view
def index():
    #render template render HTML template.
     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('/stream')
def stream():
    return Response(gen(VideoCamera()),mimetype='multipart/x-mixed-replace; boundary=frame')
    return render_template('stream.html')


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


@app.route('/reguser',methods = ['GET','POST'])
def reguser():
    if request.method == "POST":
        femail = request.form['txtemail']
        fpass1 = request.form['txtpass1']
        fpass2 = request.form['txtpass2']
        ffname = request.form['txtfname']
        flname = request.form['txtlname']
        storuser(femail,fpass1,fpass2,ffname,flname)
        flash("User Registration Done:{}".format(femail))
        #app.logger.debug("New register user detail:-"+fpass1)
        return redirect(url_for('sucess'))
    return render_template('UserRegister.html')

@app.errorhandler(500)
def server_err(e):
    return  render_template("500.html"),500

@app.errorhandler(404)
def pagenot_found(e):
    return  render_template("404.html"),400


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



# class user:
#     def __init__(self,fname,lname):
#         self.fname = fname
#         self.lname =lname
#
#     def __str__(self):
#         return "{}.{}.".format(self.fname[0],self.lname[0])
#
#     def initials(self):
#         return "{}.{}.".format(self.fname[0],self.lname[0])

这是我从上面提到的博客中复制的相机文件。

import cv2

class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        # self.video = cv2.VideoCapture('video.mp4')

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, image = self.video.read()
        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()

这是我希望在用户点击实时Feed时显示的stream.html文件。

{% extends "base.html" %}
{% block title %}
SVS Streaming Result.
{% endblock %}
{%block content%}
<article>

                        <h1>Your Live Streaming is done...</h1>
                        <img id="bg" src="{{ url_for('stream') }}">

</article>
{%endblock%}

请在这里帮助我。

1 个答案:

答案 0 :(得分:0)

我终于可以使用简单的app装饰器来解决这个问题:

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

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