hikrobotcamlib 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,163 @@
1
+ Metadata-Version: 2.1
2
+ Name: hikrobotcamlib
3
+ Version: 1.0.0
4
+ Summary: Package HIK Robotics camera drivers and some helpers
5
+ Home-page: https://gitlab.com/advian-oss/python-hikrobotcamlib/
6
+ License: MIT
7
+ Author: Eero af Heurlin
8
+ Author-email: eero.afheurlin@iki.fi
9
+ Requires-Python: >=3.8,<4.0
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Dist: click (>=8.0,<9.0)
18
+ Requires-Dist: libadvian (>=1.0,<2.0)
19
+ Project-URL: Repository, https://gitlab.com/advian-oss/python-hikrobotcamlib/
20
+ Description-Content-Type: text/x-rst
21
+
22
+ ==============
23
+ hikrobotcamlib
24
+ ==============
25
+
26
+ Package HIK Robotics camera drivers and a Pythonic wrappers to the data. You still need to install the SDK to get the
27
+ drivers/libraries.
28
+
29
+ This supports a subset of the platforms HIKRobotics' supports:
30
+
31
+ - Linux: glibc (*not* MUSL, ie not Alpine) x86_64, aarch64 and armhf (probably, only tested on aarch64)
32
+ - MacOS (idk if the dynlibs from vendor are both for Apple silicon and x86_64 but at least x86_64 works)
33
+
34
+ I've tried to make nice wrappers that should mostly handle things cleanly but this uses ctypes and calls external libraries
35
+ that are not real Python bindings (via cddl). One wrong move and everything crashes hard.
36
+
37
+ Usage
38
+ -----
39
+
40
+ The wrappers are documented and type-hinted, enjoy. TLDR:
41
+
42
+ .. code-block:: python3
43
+
44
+ from typing import Optional
45
+ import time
46
+
47
+ from hikrobotcamlib Camera, DeviceList, Frame, DeviceTransport
48
+
49
+ def frame_callback(frame: Frame, cam: Camera) -> None:
50
+ """Handle frames from camera"""
51
+ # TODO: Do something with the frame
52
+
53
+ cam: Optional[Camera] = None
54
+ for devinfo in DeviceList(DeviceTransport.GIGE | DeviceTransport.USB):
55
+ cam = Camera(devinfo)
56
+ break
57
+
58
+ if not cam:
59
+ raise RuntimeError("No camera")
60
+
61
+ cam.open()
62
+ cam.frame_callback = frame_callback
63
+ cam.trigger_enable(False)
64
+ cam.set_framerate(10.0)
65
+ cam.start()
66
+ time.sleep(2.5)
67
+ cam.stop()
68
+ cam.close()
69
+
70
+ Docker
71
+ ------
72
+
73
+ For more controlled deployments and to get rid of "works on my computer" -syndrome, we always
74
+ make sure our software works under docker.
75
+
76
+ It's also a quick way to get started with a standard development environment.
77
+
78
+ SSH agent forwarding
79
+ ^^^^^^^^^^^^^^^^^^^^
80
+
81
+ We need buildkit_::
82
+
83
+ export DOCKER_BUILDKIT=1
84
+
85
+ .. _buildkit: https://docs.docker.com/develop/develop-images/build_enhancements/
86
+
87
+ And also the exact way for forwarding agent to running instance is different on OSX::
88
+
89
+ export DOCKER_SSHAGENT="-v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"
90
+
91
+ and Linux::
92
+
93
+ export DOCKER_SSHAGENT="-v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -e SSH_AUTH_SOCK"
94
+
95
+ Creating a development container
96
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97
+
98
+ Build image, create container and start it::
99
+
100
+ docker build --ssh default --target devel_shell -t hikrobotcamlib:devel_shell .
101
+ docker create --name hikrobotcamlib_devel -v `pwd`":/app" -it `echo $DOCKER_SSHAGENT` hikrobotcamlib:devel_shell
102
+ docker start -i hikrobotcamlib_devel
103
+
104
+ pre-commit considerations
105
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
106
+
107
+ If working in Docker instead of native env you need to run the pre-commit checks in docker too::
108
+
109
+ docker exec -i hikrobotcamlib_devel /bin/bash -c "pre-commit install"
110
+ docker exec -i hikrobotcamlib_devel /bin/bash -c "pre-commit run --all-files"
111
+
112
+ You need to have the container running, see above. Or alternatively use the docker run syntax but using
113
+ the running container is faster::
114
+
115
+ docker run --rm -it -v `pwd`":/app" hikrobotcamlib:devel_shell -c "pre-commit run --all-files"
116
+
117
+ Test suite
118
+ ^^^^^^^^^^
119
+
120
+ You can use the devel shell to run py.test when doing development, for CI use
121
+ the "tox" target in the Dockerfile::
122
+
123
+ docker build --ssh default --target tox -t hikrobotcamlib:tox .
124
+ docker run --rm -it -v `pwd`":/app" `echo $DOCKER_SSHAGENT` hikrobotcamlib:tox
125
+
126
+ Production docker
127
+ ^^^^^^^^^^^^^^^^^
128
+
129
+ TODO: Remove this section if this is a library and not an application
130
+
131
+ There's a "production" target as well for running the application, remember to change that
132
+ architecture tag to arm64 if building on ARM::
133
+
134
+ docker build --ssh default --target production -t hikrobotcamlib:amd64-latest .
135
+ docker run --rm --network host -it --name hikrobotcamlib hikrobotcamlib:amd64-latest
136
+
137
+ Development
138
+ -----------
139
+
140
+ TODO: Remove the repo init from this document after you have done it.
141
+
142
+ TLDR:
143
+
144
+ - Create and activate a Python 3.11 virtualenv (assuming virtualenvwrapper)::
145
+
146
+ mkvirtualenv -p `which python3.11` my_virtualenv
147
+
148
+ - change to a branch::
149
+
150
+ git checkout -b my_branch
151
+
152
+ - install Poetry: https://python-poetry.org/docs/#installation
153
+ - Install project deps and pre-commit hooks::
154
+
155
+ poetry install
156
+ pre-commit install
157
+ pre-commit run --all-files
158
+
159
+ - Ready to go.
160
+
161
+ Remember to activate your virtualenv whenever working on the repo, this is needed
162
+ because pylint and mypy pre-commit hooks use the "system" python for now (because reasons).
163
+
@@ -0,0 +1,141 @@
1
+ ==============
2
+ hikrobotcamlib
3
+ ==============
4
+
5
+ Package HIK Robotics camera drivers and a Pythonic wrappers to the data. You still need to install the SDK to get the
6
+ drivers/libraries.
7
+
8
+ This supports a subset of the platforms HIKRobotics' supports:
9
+
10
+ - Linux: glibc (*not* MUSL, ie not Alpine) x86_64, aarch64 and armhf (probably, only tested on aarch64)
11
+ - MacOS (idk if the dynlibs from vendor are both for Apple silicon and x86_64 but at least x86_64 works)
12
+
13
+ I've tried to make nice wrappers that should mostly handle things cleanly but this uses ctypes and calls external libraries
14
+ that are not real Python bindings (via cddl). One wrong move and everything crashes hard.
15
+
16
+ Usage
17
+ -----
18
+
19
+ The wrappers are documented and type-hinted, enjoy. TLDR:
20
+
21
+ .. code-block:: python3
22
+
23
+ from typing import Optional
24
+ import time
25
+
26
+ from hikrobotcamlib Camera, DeviceList, Frame, DeviceTransport
27
+
28
+ def frame_callback(frame: Frame, cam: Camera) -> None:
29
+ """Handle frames from camera"""
30
+ # TODO: Do something with the frame
31
+
32
+ cam: Optional[Camera] = None
33
+ for devinfo in DeviceList(DeviceTransport.GIGE | DeviceTransport.USB):
34
+ cam = Camera(devinfo)
35
+ break
36
+
37
+ if not cam:
38
+ raise RuntimeError("No camera")
39
+
40
+ cam.open()
41
+ cam.frame_callback = frame_callback
42
+ cam.trigger_enable(False)
43
+ cam.set_framerate(10.0)
44
+ cam.start()
45
+ time.sleep(2.5)
46
+ cam.stop()
47
+ cam.close()
48
+
49
+ Docker
50
+ ------
51
+
52
+ For more controlled deployments and to get rid of "works on my computer" -syndrome, we always
53
+ make sure our software works under docker.
54
+
55
+ It's also a quick way to get started with a standard development environment.
56
+
57
+ SSH agent forwarding
58
+ ^^^^^^^^^^^^^^^^^^^^
59
+
60
+ We need buildkit_::
61
+
62
+ export DOCKER_BUILDKIT=1
63
+
64
+ .. _buildkit: https://docs.docker.com/develop/develop-images/build_enhancements/
65
+
66
+ And also the exact way for forwarding agent to running instance is different on OSX::
67
+
68
+ export DOCKER_SSHAGENT="-v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"
69
+
70
+ and Linux::
71
+
72
+ export DOCKER_SSHAGENT="-v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -e SSH_AUTH_SOCK"
73
+
74
+ Creating a development container
75
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76
+
77
+ Build image, create container and start it::
78
+
79
+ docker build --ssh default --target devel_shell -t hikrobotcamlib:devel_shell .
80
+ docker create --name hikrobotcamlib_devel -v `pwd`":/app" -it `echo $DOCKER_SSHAGENT` hikrobotcamlib:devel_shell
81
+ docker start -i hikrobotcamlib_devel
82
+
83
+ pre-commit considerations
84
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
85
+
86
+ If working in Docker instead of native env you need to run the pre-commit checks in docker too::
87
+
88
+ docker exec -i hikrobotcamlib_devel /bin/bash -c "pre-commit install"
89
+ docker exec -i hikrobotcamlib_devel /bin/bash -c "pre-commit run --all-files"
90
+
91
+ You need to have the container running, see above. Or alternatively use the docker run syntax but using
92
+ the running container is faster::
93
+
94
+ docker run --rm -it -v `pwd`":/app" hikrobotcamlib:devel_shell -c "pre-commit run --all-files"
95
+
96
+ Test suite
97
+ ^^^^^^^^^^
98
+
99
+ You can use the devel shell to run py.test when doing development, for CI use
100
+ the "tox" target in the Dockerfile::
101
+
102
+ docker build --ssh default --target tox -t hikrobotcamlib:tox .
103
+ docker run --rm -it -v `pwd`":/app" `echo $DOCKER_SSHAGENT` hikrobotcamlib:tox
104
+
105
+ Production docker
106
+ ^^^^^^^^^^^^^^^^^
107
+
108
+ TODO: Remove this section if this is a library and not an application
109
+
110
+ There's a "production" target as well for running the application, remember to change that
111
+ architecture tag to arm64 if building on ARM::
112
+
113
+ docker build --ssh default --target production -t hikrobotcamlib:amd64-latest .
114
+ docker run --rm --network host -it --name hikrobotcamlib hikrobotcamlib:amd64-latest
115
+
116
+ Development
117
+ -----------
118
+
119
+ TODO: Remove the repo init from this document after you have done it.
120
+
121
+ TLDR:
122
+
123
+ - Create and activate a Python 3.11 virtualenv (assuming virtualenvwrapper)::
124
+
125
+ mkvirtualenv -p `which python3.11` my_virtualenv
126
+
127
+ - change to a branch::
128
+
129
+ git checkout -b my_branch
130
+
131
+ - install Poetry: https://python-poetry.org/docs/#installation
132
+ - Install project deps and pre-commit hooks::
133
+
134
+ poetry install
135
+ pre-commit install
136
+ pre-commit run --all-files
137
+
138
+ - Ready to go.
139
+
140
+ Remember to activate your virtualenv whenever working on the repo, this is needed
141
+ because pylint and mypy pre-commit hooks use the "system" python for now (because reasons).
@@ -0,0 +1,87 @@
1
+ [tool.poetry]
2
+ name = "hikrobotcamlib"
3
+ version = "1.0.0"
4
+ description = "Package HIK Robotics camera drivers and some helpers"
5
+ authors = ["Eero af Heurlin <eero.afheurlin@iki.fi>"]
6
+ homepage = "https://gitlab.com/advian-oss/python-hikrobotcamlib/"
7
+ repository = "https://gitlab.com/advian-oss/python-hikrobotcamlib/"
8
+ license = "MIT"
9
+ readme = "README.rst"
10
+
11
+ [tool.poetry.scripts]
12
+ hikrobotcamlib = "hikrobotcamlib.console:hikrobotcamlib_cli"
13
+
14
+
15
+ [tool.black]
16
+ line-length = 120
17
+ target-version = ['py38']
18
+ exclude = '''
19
+ (
20
+ /(
21
+ \.eggs # exclude a few common directories in the
22
+ | \.git # root of the project
23
+ | \.hg
24
+ | \.mypy_cache
25
+ | \.tox
26
+ | \.venv
27
+ | _build
28
+ | buck-out
29
+ | build
30
+ | dist
31
+ )/
32
+ | __pycache__
33
+ )
34
+ '''
35
+
36
+ [tool.mypy]
37
+ strict=true
38
+
39
+ [[tool.mypy.overrides]]
40
+ module = "hikrobotcamlib.hiklibs.*"
41
+ ignore_errors = true
42
+
43
+ [tool.pytest.ini_options]
44
+ junit_family="xunit2"
45
+ addopts="--cov=hikrobotcamlib --cov-fail-under=65 --cov-branch"
46
+ asyncio_mode="strict"
47
+
48
+ [tool.pylint.format]
49
+ max-line-length = 120
50
+
51
+ [tool.pylint.design]
52
+ max-parents = 10
53
+ max-attributes = 10
54
+
55
+
56
+ [tool.pylint.messages_control]
57
+ disable=["fixme", "W1202", "C0209"]
58
+
59
+ [tool.pylint.similarities]
60
+ min-similarity-lines = 8
61
+ ignore-imports = true
62
+
63
+ [tool.coverage.run]
64
+ omit = ["tests/*"]
65
+ branch = true
66
+
67
+ [tool.poetry.dependencies]
68
+ python = "^3.8"
69
+ libadvian = "^1.0"
70
+ click = "^8.0"
71
+
72
+ [tool.poetry.group.dev.dependencies]
73
+ pytest = "^7.4"
74
+ coverage = "^7.3"
75
+ pytest-cov = "^4.1"
76
+ pylint = "^2.17"
77
+ black = "^23.7"
78
+ bandit = "^1.7"
79
+ mypy = "^1.5"
80
+ pre-commit = "^3.3"
81
+ pytest-asyncio = ">=0.21,<1.0" # caret behaviour on 0.x is to lock to 0.x.*
82
+ bump2version = "^1.0"
83
+ detect-secrets = "^1.2"
84
+
85
+ [build-system]
86
+ requires = ["poetry-core>=1.2.0"]
87
+ build-backend = "poetry.core.masonry.api"
@@ -0,0 +1,8 @@
1
+ """ Package HIK Robotics camera drivers and some helpers"""
2
+ from .types import Frame, DeviceList, DeviceTransport
3
+ from .wrapper import Camera
4
+
5
+ __version__ = "1.0.0" # NOTE Use `bump2version --config-file patch` to bump versions correctly
6
+
7
+
8
+ __all__ = ["Frame", "Camera", "DeviceList", "__version__", "DeviceTransport"]
@@ -0,0 +1,34 @@
1
+ """CLI entrypoints for hikrobotcamlib"""
2
+ import logging
3
+
4
+ import click
5
+ from libadvian.logging import init_logging
6
+
7
+ from hikrobotcamlib import __version__, DeviceList
8
+ from hikrobotcamlib.types import DeviceTransport
9
+
10
+ LOGGER = logging.getLogger(__name__)
11
+
12
+
13
+ @click.command()
14
+ @click.version_option(version=__version__)
15
+ @click.option("-l", "--loglevel", help="Python log level, 10=DEBUG, 20=INFO, 30=WARNING, 40=CRITICAL", default=30)
16
+ @click.option("-v", "--verbose", count=True, help="Shorthand for info/debug loglevel (-v/-vv)")
17
+ @click.option("--usb", is_flag=True, help="Use USB transport")
18
+ @click.option("--gige/--no-gige", is_flag=True, help="Use GigE transport", default=True)
19
+ def hikrobotcamlib_cli(loglevel: int, verbose: int, usb: bool, gige: bool) -> None:
20
+ """List cameras"""
21
+ if verbose == 1:
22
+ loglevel = 20
23
+ if verbose >= 2:
24
+ loglevel = 10
25
+ init_logging(loglevel)
26
+ LOGGER.setLevel(loglevel)
27
+ transports = 0
28
+ if usb:
29
+ transports |= DeviceTransport.USB
30
+ if gige:
31
+ transports |= DeviceTransport.GIGE
32
+
33
+ for dev in DeviceList(transports):
34
+ click.echo("Found {}".format(dev))
@@ -0,0 +1,71 @@
1
+ """Errors"""
2
+ from typing import Optional
3
+
4
+ from .hiklibs import MvErrorDefine_const
5
+
6
+ MV_ERROR_CODES = {
7
+ getattr(MvErrorDefine_const, name): name for name in dir(MvErrorDefine_const) if name.startswith("MV_")
8
+ }
9
+
10
+
11
+ class HIKException(Exception):
12
+ """Generic baseclass for catching things"""
13
+
14
+
15
+ class EnumerationError(HIKException, RuntimeError):
16
+ """Failure to enumerate cameras"""
17
+
18
+
19
+ class CamHandleError(HIKException, RuntimeError):
20
+ """Errors related to camera handles"""
21
+
22
+
23
+ class HandleCreateError(CamHandleError):
24
+ """Failure to create a handle for the camera"""
25
+
26
+
27
+ class HandleDestroyError(CamHandleError):
28
+ """Failure to destroy a handle for the camera"""
29
+
30
+
31
+ class CamStateError(HIKException, RuntimeError):
32
+ """Errors related to camera state, like open/close"""
33
+
34
+
35
+ class CamOpenError(CamStateError):
36
+ """Could not open camera"""
37
+
38
+
39
+ class CamCloseError(CamStateError):
40
+ """Could not close camera"""
41
+
42
+
43
+ class CamSettingsError(CamStateError):
44
+ """Could not not set some camera value"""
45
+
46
+
47
+ class CamClosed(CamStateError):
48
+ """Camera is closed and action needs it to be open"""
49
+
50
+
51
+ class InvalidTransport(HIKException, ValueError):
52
+ """A problem with the transport value"""
53
+
54
+
55
+ class FrameError(HIKException, RuntimeError):
56
+ """A problem with frames"""
57
+
58
+
59
+ class CamCommandError(HIKException, RuntimeError):
60
+ """Errors related to sending commands to camera, like SW trigger"""
61
+
62
+
63
+ class CamReadError(HIKException, RuntimeError):
64
+ """Errors related to reading values from the camera"""
65
+
66
+
67
+ def mv_errstr(code: int) -> Optional[str]:
68
+ """Return the constant name that matches the error code"""
69
+ if code not in MV_ERROR_CODES:
70
+ return None
71
+ return MV_ERROR_CODES[code]
@@ -0,0 +1,21 @@
1
+ """helpers"""
2
+ from typing import Tuple, Iterable
3
+
4
+
5
+ def get_octets(src: int) -> Tuple[int, int, int, int]:
6
+ """Get the octets from an integer, useful for ips etc. returned highest first"""
7
+ nip1 = (src & 0xFF000000) >> 24
8
+ nip2 = (src & 0x00FF0000) >> 16
9
+ nip3 = (src & 0x0000FF00) >> 8
10
+ nip4 = src & 0x000000FF
11
+ return nip1, nip2, nip3, nip4
12
+
13
+
14
+ def get_str(cstr: Iterable[int]) -> str:
15
+ """Convert C string from the camera structs into str"""
16
+ ret = ""
17
+ for val in cstr:
18
+ if val == 0x0:
19
+ break
20
+ ret += chr(val)
21
+ return ret
@@ -0,0 +1,39 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Don't change this file, it was auto generated by Const_file_Generater.py.
3
+ # pylint: skip-file
4
+
5
+ MV_UNKNOW_DEVICE = 0x00000000 # ch:未知设备类型,保留意义 | en:Unknown Device Type, Reserved
6
+ MV_GIGE_DEVICE = 0x00000001 # ch:GigE设备 | en:GigE Device
7
+ MV_1394_DEVICE = 0x00000002 # ch:1394-a/b 设备 | en:1394-a/b Device
8
+ MV_USB_DEVICE = 0x00000004 # ch:USB3.0 设备 | en:USB3.0 Device
9
+ MV_CAMERALINK_DEVICE = 0x00000008 # ch:CameraLink设备 | en:CameraLink Device
10
+ INFO_MAX_BUFFER_SIZE = 64
11
+ MV_MAX_TLS_NUM = 8
12
+ MV_MAX_DEVICE_NUM = 256
13
+ MV_IP_CFG_STATIC = 0x05000000
14
+ MV_IP_CFG_DHCP = 0x06000000
15
+ MV_IP_CFG_LLA = 0x04000000
16
+ MV_NET_TRANS_DRIVER = 0x00000001
17
+ MV_NET_TRANS_SOCKET = 0x00000002
18
+ MV_MATCH_TYPE_NET_DETECT = 0x00000001 # ch:网络流量和丢包信息 | en:Network traffic and packet loss information
19
+ MV_MATCH_TYPE_USB_DETECT = (
20
+ 0x00000002 # ch:host接收到来自U3V设备的字节总数 | en:The total number of bytes host received from U3V device
21
+ )
22
+ MV_MAX_XML_NODE_NUM_C = 128
23
+ MV_MAX_XML_NODE_STRLEN_C = 64
24
+ MV_MAX_XML_STRVALUE_STRLEN_C = 64
25
+ MV_MAX_XML_DISC_STRLEN_C = 512
26
+ MV_MAX_XML_ENTRY_NUM = 10
27
+ MV_MAX_XML_PARENTS_NUM = 8
28
+ MV_MAX_XML_SYMBOLIC_STRLEN_C = 64
29
+ MV_MAX_XML_SYMBOLIC_NUM = 64
30
+ MV_EXCEPTION_DEV_DISCONNECT = 0x00008001 # ch:设备断开连接 | en:The device is disconnected
31
+ MV_EXCEPTION_VERSION_CHECK = 0x00008002 # ch:SDK与驱动版本不匹配 | en:SDK does not match the driver version
32
+ MV_ACCESS_Exclusive = 1
33
+ MV_ACCESS_ExclusiveWithSwitch = 2
34
+ MV_ACCESS_Control = 3
35
+ MV_ACCESS_ControlWithSwitch = 4
36
+ MV_ACCESS_ControlSwitchEnable = 5
37
+ MV_ACCESS_ControlSwitchEnableWithKey = 6
38
+ MV_ACCESS_Monitor = 7
39
+ MAX_EVENT_NAME_SIZE = 128 # 相机Event事件名称最大长度 | en:Max length of event name