How to live steam webcam using python-flask and opencv?
Question: I want to set up a web server using python-flask. I tried to follow the
tutorial form [chioka.in](http://www.chioka.in/python-live-video-streaming-
example/) , but when I run the app it initializes but when i try to access the
address from localhost I get this error.

    
    
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    127.0.0.1 - - [18/Aug/2016 02:15:46] "GET / HTTP/1.1" 200 -
    Traceback (most recent call last):
      File "main.py", line 22, in <module>
        app.run()
      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 843, in run
        run_simple(host, port, self, **options)
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 694, in run_simple
        inner()
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 659, in inner
        srv.serve_forever()
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 499, in serve_forever
        HTTPServer.serve_forever(self)
      File "/usr/lib/python2.7/SocketServer.py", line 238, in serve_forever
        self._handle_request_noblock()
      File "/usr/lib/python2.7/SocketServer.py", line 297, in _handle_request_noblock
        self.handle_error(request, client_address)
      File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
        self.process_request(request, client_address)
      File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
        self.finish_request(request, client_address)
      File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "/usr/lib/python2.7/SocketServer.py", line 655, in __init__
        self.handle()
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 216, in handle
        rv = BaseHTTPRequestHandler.handle(self)
      File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
        self.handle_one_request()
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 251, in handle_one_request
        return self.run_wsgi()
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 193, in run_wsgi
        execute(self.server.app)
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 183, in execute
        for data in application_iter:
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 703, in __next__
        return self._next()
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 81, in _iter_encoded
        for item in iterable:
      File "main.py", line 12, in gen
        frame = camera.get_frame()
      File "/media/moithil/STORAGE/PROJECTS/PROJECTS/webcam Server/camera.py", line 24, in get_frame
        return jpeg.tobytes()
    AttributeError: 'numpy.ndarray' object has no attribute 'tobytes'
    

How to I convert this jpeg into bytes to return it properly?

The code blocks with error are

> from camera.py file
    
    
    import cv2
    
    class VideoCamera(object):
        def __init__(self):
            self.video = cv2.VideoCapture(0)
    
    
        def __del__(self):
            self.video.release()
    
        def get_frame(self):
            success, image = self.video.read()
            ret, jpeg = cv2.imencode('.jpg', image)
            return jpeg.tobytes()
    

> from main.py file
    
    
    @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(VideoCamera()),
                        mimetype='multipart/x-mixed-replace; boundary=frame')
    


Answer: Your numpy version is probably < 1.9.

According to the release notes
(<http://docs.scipy.org/doc/numpy/release.html>):

> ndarray.tobytes and MaskedArray.tobytes have been added as aliases for
> tostring > which exports arrays as bytes. This is more consistent in Python
> 3 where str and > bytes are not the same.

Try using tostring instead, or upgrade numpy to latest version.

