diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f94aa5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +### Python template +# Byte-compiled / optimized / DLL files + +.idea/ +__pycache__/ +moil_fisheye_param/ +__cached.txt + +# Distribution / packaging +example_source/videos/* +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea + diff --git a/README.md b/README.md index 4c5a8b2..48513d4 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,55 @@ # RTSP streaming using GStreamer +===================================================== -Python implementation to stream camera feed from OpenCV videoCapture via RTSP server using GStreamer 1.0. +Modify by: Heru-sp + +Date: 2023/05/12 + +Title project: Python implementation to stream camera feed from OpenCV videoCapture via RTSP server using GStreamer 1.0. ## Installation -This implementation has been developed and tested on Ubuntu 16.04 and 18.04. So the installation steps are specific to debian based linux distros. +This implementation has been developed and tested on Ubuntu 20.04. So the installation steps are specific to debian based linux distros. ### Step-1 Install GStreamer-1.0 and related plugins - sudo apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio + +```commandline + $ sudo apt-get install gstreamer1.0-tools gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav +``` + ### Step-2 Install RTSP server - sudo apt-get install libglib2.0-dev libgstrtspserver-1.0-dev gstreamer1.0-rtsp +```commandline + $ sudo apt-get update + + $ sudo apt-get install gstreamer1.0-tools gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly libgstrtspserver-1.0-dev +``` + ### Requirement -- Python 3.x -- Opencv 3.x or above ( pip install opencv-python ) +Python 3.10 + +```commandline + $ pip install opencv-python + + $ sudo apt install libcairo2-dev libxt-dev libgirepository1.0-dev + + $ pip install pycairo PyGObject +``` ### Usage > Run stream.py with required arguments to start the rtsp server -##### Sample - python stream.py --device_id 0 --fps 30 --image_width 640 --image_height 480 --port 8554 --stream_uri /video_stream + + +##### Sample +```commandline + + $ python stream.py --device_id 0 --fps 30 --image_width 640 --image_height 480 --port 8554 --stream_uri /video_stream +``` ### Visualization -You can view the video feed on `rtsp://server-ip-address:8554/stream_uri` +- You can view the video feed on `rtsp://server-ip-address:8554/stream_uri` -e.g: `rtsp://192.168.1.12:8554/video_stream` + e.g: `rtsp://192.168.103.75:8554/video_stream` -You can either use any video player which supports rtsp streaming like VLC player or you can use the `open-rtsp.py` script to view the video feed. +- You can either use any video player which supports rtsp streaming like VLC player or you can use the `open-rtsp.py` script to view the video feed. diff --git a/open_rtsp.py b/open_rtsp.py index 4b6b2ea..8bf6abf 100644 --- a/open_rtsp.py +++ b/open_rtsp.py @@ -9,7 +9,7 @@ import cv2 cv2.namedWindow("RTSP View", cv2.WINDOW_NORMAL) -cap = cv2.VideoCapture("rtsp://192.168.1.12:8554/video_stream") +cap = cv2.VideoCapture("rtsp://192.168.103.75:8554/video_stream") while True: ret, frame = cap.read() diff --git a/server_stream.py b/server_stream.py new file mode 100644 index 0000000..9b106ec --- /dev/null +++ b/server_stream.py @@ -0,0 +1,41 @@ +import cv2 +import zmq +import numpy as np + + +def display_stream(): + # Create a ZeroMQ context and socket + context = zmq.Context() + socket = context.socket(zmq.SUB) + + # Connect the socket to the RTSP server address + socket.connect("tcp://192.168.103.228:5555") # Replace "rtsp_server_address" with the actual address + + # Set the subscription filter + socket.setsockopt_string(zmq.SUBSCRIBE, "") + + while True: + # Receive the encoded frame from the serverq + encoded_frame = socket.recv() + + # Decode the frame + frame = np.frombuffer(encoded_frame, dtype=np.uint8) + frame = cv2.imdecode(frame, 1) + + # Display the frame + cv2.imshow('RTSP Stream', frame) + + # Check for 'q' key press to exit + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +print("now is running, However server off") +# Release the resources +cv2.destroyAllWindows() + + + +if __name__ == '__main__': + display_stream() + print("camera show") +