Skip to main content

HikCamera

海康威视工业相机Python控制库 - 简单易用的海康威视工业相机(包括GigE网口和USB接口)Python控制库。

特性

  • ✅ 支持多种接口: 支持GigE网口相机和USB相机
  • 🚀 简单易用: 提供简洁的Python接口,易于集成
  • 🔍 自动设备枚举: 自动发现和识别连接的相机设备
  • 🎛️ 完整参数控制: 支持曝光时间、增益、ROI等参数控制
  • 🛡️ 异常处理: 完善的错误处理和资源管理
  • 📦 零依赖冲突: 只依赖numpy和opencv-python

安装

从PyPI安装(推荐)

pip install hikcamera

从源码安装

git clone https://github.com/yourusername/hikcamera.git
cd hikcamera
pip install -e .

系统要求

  • Python >= 3.9
  • Windows操作系统
  • 已安装海康威视MVS SDK
  • numpy >= 1.24.0
  • opencv-python >= 4.8.0

快速开始

基本使用

from hikcamera import HKCamera

# 创建相机实例(自动枚举所有设备)
camera = HKCamera()

# 启动采集
camera.start_camera()

# 设置曝光时间(10ms)
camera.set_exposure_time(10000)

# 获取图像
image = camera.get_image()

# 保存图像
import cv2
cv2.imwrite("output.png", image)

# 清理资源
del camera

指定设备

# 使用第一个发现的设备
camera = HKCamera(camera_idx=0)

# 对于GigE相机,可以通过IP地址指定
camera = HKCamera(camera_ip="192.168.1.100")

# 对于USB相机,可以通过序列号指定
camera = HKCamera(camera_ip="DA2728737")

连续采集

from hikcamera import HKCamera
import cv2

camera = HKCamera()
camera.start_camera()

while True:
    image = camera.get_image()
    if image is not None:
        cv2.imshow('Camera', image)
        if cv2.waitKey(1) & 0xFF == 27:  # ESC键退出
            break

cv2.destroyAllWindows()
del camera

高级功能

参数控制

# 获取参数
width = camera.get_value("int_value", "Width")
height = camera.get_value("int_value", "Height")
exposure = camera.get_exposure_time()

# 设置参数
camera.set_exposure_time(15000)  # 15ms
camera.set_value("float_value", "Gain", 10.0)

设备信息

# 枚举所有设备
device_list = HKCamera.enum_devices()
print(f"发现 {device_list.nDeviceNum} 个设备")

# 显示设备信息
for i in range(device_list.nDeviceNum):
    # 设备信息会自动打印
    pass

示例程序

库包含多个示例程序,展示各种使用场景:

  • basic_usage.py: 基本的相机操作
  • continuous_capture.py: 连续采集和实时显示
  • advanced_control.py: 高级参数控制

运行示例:

# 基本使用
python -m hikcamera.examples.basic_usage

# 连续采集
python -m hikcamera.examples.continuous_capture

# 高级控制
python -m hikcamera.examples.advanced_control

支持的相机型号

海康威视MV系列工业相机,包括但不限于:

  • MV-CA系列 (千万像素)
  • MV-CM系列 (百万像素)
  • MV-CS系列 (USB相机)
  • 其他兼容MVS SDK的型号

故障排除

找不到相机

  1. 检查相机是否正确连接
  2. 确认MVS SDK已正确安装
  3. 尝试以管理员身份运行程序
  4. 检查防火墙设置

依赖问题

# 重新安装依赖
pip install --upgrade numpy opencv-python

DLL加载错误

确保MVS SDK已正确安装,并且DLL文件在系统PATH中。

开发

运行测试

# 运行所有测试
python -m pytest src/hikcamera/tests/

# 运行特定测试
python -m pytest src/hikcamera/tests/test_usb_camera.py

代码格式化

# 使用black格式化代码
black src/hikcamera/

# 使用flake8检查代码风格
flake8 src/hikcamera/

许可证

MIT License - 详见 LICENSE 文件

贡献

欢迎提交Issue和Pull Request!

更新日志

0.1.0 (2026-04-09)

  • 🎉 首次发布
  • ✅ 支持GigE和USB相机
  • ✅ 基本的图像采集功能
  • ✅ 参数控制功能
  • ✅ 完整的示例代码

联系方式

致谢

  • 海康威视机器 vision (MVS) SDK
  • numpy和opencv-python项目

注意: 本库需要海康威视MVS SDK的支持,请确保在使用前已正确安装。

Metadata

Release files for mv-hikcamera 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mv-hikcamera 0.1.0
File Size Uploaded Provenance Actions
mv_hikcamera-0.1.0.tar.gz 57.4 kB Without provenance Details for mv_hikcamera-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mv-hikcamera 0.1.0
File Interpreter ABI Platform Actions
mv_hikcamera-0.1.0-py3-none-any.whl Python 3 none any Details for mv_hikcamera-0.1.0-py3-none-any.whl

Total release size: 121.5 kB

Release files / mv_hikcamera-0.1.0.tar.gz

Download URL mv_hikcamera-0.1.0.tar.gz
Size 57.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c555764c1adda9ebf5249228ff6f7eddfb94e58bb5cd4494d0ab8666c89fd2fd
BLAKE2b-256 checksum
How to use checksums
5a753059af2c29617174502cf293fb48107a82b4b0e66192bad6d319d166564e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / mv_hikcamera-0.1.0-py3-none-any.whl

Download URL mv_hikcamera-0.1.0-py3-none-any.whl
Size 64.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09fb753ebcffb538e1a68402addb4bc9e7bb961cb573efdd53a77f673848a89a
BLAKE2b-256 checksum
How to use checksums
82242c5a762e5f56048eb1cc66713d42be5df00c62971e75da9b46f16f9a7bba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for mv-hikcamera 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page