Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move streaming dependencies to their own "extra" #46

Merged
merged 2 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Install the package via PyPi
Install from GitHub

pip install git+https://github.com/ReolinkCameraAPI/reolinkapipy.git

If you want to include the video streaming functionality you need to include the streaming "extra" dependencies

pip install 'reolinkapi[streaming]'

## Contributors

Expand Down
101 changes: 56 additions & 45 deletions reolinkapi/mixins/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,59 @@
from io import BytesIO

import requests
from PIL.Image import Image, open as open_image

from reolinkapi.utils.rtsp_client import RtspClient


class StreamAPIMixin:
""" API calls for opening a video stream or capturing an image from the camera."""

def open_video_stream(self, callback: Any = None, proxies: Any = None) -> Any:
"""
'https://support.reolink.com/hc/en-us/articles/360007010473-How-to-Live-View-Reolink-Cameras-via-VLC-Media-Player'
Blocking function creates a generator and returns the frames as it is spawned
:param callback:
:param proxies: Default is none, example: {"host": "localhost", "port": 8000}
"""
rtsp_client = RtspClient(
ip=self.ip, username=self.username, password=self.password, proxies=proxies, callback=callback)
return rtsp_client.open_stream()

def get_snap(self, timeout: float = 3, proxies: Any = None) -> Optional[Image]:
"""
Gets a "snap" of the current camera video data and returns a Pillow Image or None
:param timeout: Request timeout to camera in seconds
:param proxies: http/https proxies to pass to the request object.
:return: Image or None
"""
data = {
'cmd': 'Snap',
'channel': 0,
'rs': ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)),
'user': self.username,
'password': self.password,
}
parms = parse.urlencode(data).encode("utf-8")

try:
response = requests.get(self.url, proxies=proxies, params=parms, timeout=timeout)
if response.status_code == 200:
return open_image(BytesIO(response.content))
print("Could not retrieve data from camera successfully. Status:", response.status_code)
return None

except Exception as e:
print("Could not get Image data\n", e)
raise

try:
from PIL.Image import Image, open as open_image

from reolinkapi.utils.rtsp_client import RtspClient


class StreamAPIMixin:
""" API calls for opening a video stream or capturing an image from the camera."""

def open_video_stream(self, callback: Any = None, proxies: Any = None) -> Any:
"""
'https://support.reolink.com/hc/en-us/articles/360007010473-How-to-Live-View-Reolink-Cameras-via-VLC-Media-Player'
Blocking function creates a generator and returns the frames as it is spawned
:param callback:
:param proxies: Default is none, example: {"host": "localhost", "port": 8000}
"""
rtsp_client = RtspClient(
ip=self.ip, username=self.username, password=self.password, proxies=proxies, callback=callback)
return rtsp_client.open_stream()

def get_snap(self, timeout: float = 3, proxies: Any = None) -> Optional[Image]:
"""
Gets a "snap" of the current camera video data and returns a Pillow Image or None
:param timeout: Request timeout to camera in seconds
:param proxies: http/https proxies to pass to the request object.
:return: Image or None
"""
data = {
'cmd': 'Snap',
'channel': 0,
'rs': ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)),
'user': self.username,
'password': self.password,
}
parms = parse.urlencode(data).encode("utf-8")

try:
response = requests.get(self.url, proxies=proxies, params=parms, timeout=timeout)
if response.status_code == 200:
return open_image(BytesIO(response.content))
print("Could not retrieve data from camera successfully. Status:", response.status_code)
return None

except Exception as e:
print("Could not get Image data\n", e)
raise
except ImportError:
class StreamAPIMixin:
""" API calls for opening a video stream or capturing an image from the camera."""

def open_video_stream(self, callback: Any = None, proxies: Any = None) -> Any:
raise ImportError('''open_video_stream requires streaming extra dependencies\nFor instance "pip install reolinkapi[streaming]"''')

def get_snap(self, timeout: float = 3, proxies: Any = None) -> Optional['Image']:
raise ImportError('''open_video_stream requires streaming extra dependencies\nFor instance "pip install reolinkapi[streaming]"''')
13 changes: 9 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ def find_version(*file_paths):
AUTHOR = 'Benehiko'
LICENSE = 'GPL-3.0'
INSTALL_REQUIRES = [
'numpy==1.19.4',
'opencv-python==4.4.0.46',
'Pillow==8.0.1',
'PySocks==1.7.1',
'PyYaml==5.3.1',
'requests>=2.18.4',
]
EXTRAS_REQUIRE = {
'streaming': [
'numpy==1.19.4',
'opencv-python==4.4.0.46',
'Pillow==8.0.1',
],
}


here = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -53,5 +57,6 @@ def find_version(*file_paths):
url=URL,
license=LICENSE,
install_requires=INSTALL_REQUIRES,
packages=find_packages(exclude=['examples', 'tests'])
packages=find_packages(exclude=['examples', 'tests']),
extras_require=EXTRAS_REQUIRE
)