How to stream rtsp videos or mp4 files throught python/flask + opencv

First, you need to download the code to generate your site with flask. To do this, you can use the following @miguelgrinberg project to stream your webcam or image files with flask.

git clone https://github.com/miguelgrinberg/flask-video-streaming

In app.py you need replace:

from camera import Camera
by
from camera_opencv import Camera

Now, you must edit your video source from camera_opencv.py.
video_source = 0, means that you want to stream your webcam (/dev/video0)
You can edit it, by a file or a rstp url

If you replace 0 by 'video.mp4' you will probably get the following error:

Starting camera thread.
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/nboettcher/Downloads/rtsp/flask-video-streaming/base_camera.py", line 93, in _thread
for frame in frames_iterator:
File "/home/nboettcher/Downloads/rtsp/flask-video-streaming/camera_opencv.py", line 17, in frames
raise RuntimeError('Could not start camera.')
RuntimeError: Could not start camera.


This is a opencv ffmpeg configuration problem. 
Other common issue is:

Unable to stop the stream: Inappropriate ioctl for device

To avoid it, install the lastest version from github:

git clone https://github.com/Itseez/opencv.git opencv
cd opencv
mkdir build

cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_OPENGL=ON -D WITH_FFMPEG=ON -D WITH_V4L=ON -D WITH_LIBV4L=ON ..

--     FFMPEG:                      NO
--       avcodec:                   NO
--       avformat:                  NO
--       avutil:                    NO
--       swscale:                   NO
--       avresample:                NO

As you see, you need to install the ffmpeg libraries:

sudo apt-get install ffmpeg libavresample-dev libavutil-dev libavcodec-dev libavformat-dev libswscale-dev

Now, retry:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_OPENGL=ON -D WITH_FFMPEG=ON -D WITH_V4L=ON -D WITH_LIBV4L=ON ..
make -j8
sudo make install

Now, if you try import opencv from python2.x you will probably get a core dumped:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
[libprotobuf FATAL opencv/3rdparty/protobuf/src/google/protobuf/stubs/common.cc:78] This program was compiled against version 2.6.1 of the Protocol Buffer runtime library, which is not compatible with the installed version (3.1.0).  Contact the program author for an update.  If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library.  (Version verification failed in "/build/mir-pkdHET/mir-0.21.0+16.04.20160330/obj-x86_64-linux-gnu/src/protobuf/mir_protobuf.pb.cc".)
terminate called after throwing an instance of 'google::protobuf::FatalException'
  what():  This program was compiled against version 2.6.1 of the Protocol Buffer runtime library, which is not compatible with the installed version (3.1.0).  Contact the program author for an update.  If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library.  (Version verification failed in "/build/mir-pkdHET/mir-0.21.0+16.04.20160330/obj-x86_64-linux-gnu/src/protobuf/mir_protobuf.pb.cc".)
[1]    21703 abort (core dumped)  python

To fix it, you must compile with qt mode option enabled

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_FFMPEG=ON -D WITH_V4L=ON -D WITH_LIBV4L=ON ..
make -j8
sudo make install

Now, edit your source from camera_opencv.py like this:
video_source = 'rtsp://video.crearchile.com:8080/live/canal2.mp4'

Execute python app.py from your terminal


Enjoy it!

Comments

Popular posts from this blog

How to fix Android when developer options are not available for this user

Exception: Could not find a default OpenFlow controller in Mininet

v4l2: open /dev/video0: Permission denied