手册上是通过C语言实现的:
这些库文件在海康威视的MVS软件安装包里面,找一找就能找到。
通过MV_CC_EnumDevices(MV_USB_DEVICE, device_list)搜索存在的USB设备。
获取设备信息cast(device_list.pDeviceInfo[int(show_device_num)], POINTER(MV_CC_DEVICE_INFO)).contents
创建句柄cam = MvCamera()
获得设备的句柄cam.MV_CC_CreateHandle(device_info)
打开设备cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
获得设备的参数存在MVCC_INTVALUE()这个结构里面,cam.MV_CC_GetIntValue(“PayloadSize”, camera_int_param)
获取图片的存储大小,还有长度
image_data_size = (c_ubyte * camera_int_param.nCurValue)()image_data_length = camera_int_param.nCurValue
开始抓流
grab_ret = cam.MV_CC_StartGrabbing()
图片信息存在下面的结构中
frame_info = MV_FRAME_OUT_INFO_EX()
memset(byref(frame_info), 0, sizeof(frame_info))
获取流
cam.MV_CC_GetOneFrameTimeout(image_data_size, image_data_length, frame_info, 1000)
将图片格式转一转
nRGBSize = frame_info.nWidth * frame_info.nHeight
convert_param = MV_CC_PIXEL_CONVERT_PARAM()
memset(byref(convert_param), 0, sizeof(convert_param))
convert_param.nWidth = frame_info.nWidth
convert_param.nHeight = frame_info.nHeight
convert_param.pSrcData = image_data_size
convert_param.nSrcDataLen = frame_info.nFrameLen
convert_param.enSrcPixelType = frame_info.enPixelType
convert_param.enDstPixelType = frame_info.enPixelType
convert_param.pDstBuffer = (c_ubyte * nRGBSize)()
convert_param.nDstBufferSize = nRGBSize
ret = cam.MV_CC_ConvertPixelType(convert_param)
img_buff = (c_ubyte * convert_param.nDstLen)()
cdll.msvcrt.memcpy(byref(img_buff), convert_param.pDstBuffer, convert_param.nDstLen)
img = np.array(img_buff, dtype=np.int8)
Img = img.reshape(1024, 1280)
Img = cv.flip(Img, -1)
Img = cv.flip(Img, -1)
整体代码如下:
# _*_coding:UTF-8_*_
# 当前系统用户: LISICHENG
# 当前系统日期:2019/7/29
# 当前系统时间:13:55
# 创建文件的IDE名称:PyCharm
import cv2 as cv
import numpy as np
from MvImport.CameraParams_const import *
from MvImport.CameraParams_header import *
from MvImport.MvCameraControl_header import *
from MvImport.MvCameraControl_class import *
from MvImport.MvErrorDefine_const import *
from MvImport.PixelType_const import *
from MvImport.PixelType_header import *
class AccessToImages():
def FindDevices(self):
device_list = MV_CC_DEVICE_INFO_LIST()
find_device_ret = MvCamera.MV_CC_EnumDevices(MV_USB_DEVICE, device_list)
if find_device_ret == 0:
return device_list
else:
return False
def GetImages(self, show_device_num, device_list):
i = 0
if show_device_num <= device_list.nDeviceNum:
device_info = cast(device_list.pDeviceInfo[int(show_device_num)], POINTER(MV_CC_DEVICE_INFO)).contents
cam = MvCamera()
handle_ret = cam.MV_CC_CreateHandle(device_info)
if handle_ret != 0:
print("创建句柄失败")
# 打开设备
open_device = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
if open_device != 0:
print('开启失败')
# 获取相机int型节点值 "PayloadSize" 为当前节点得名称,一帧节点的大小
camera_int_param = MVCC_INTVALUE()
int_value_ret = cam.MV_CC_GetIntValue("PayloadSize", camera_int_param)
if int_value_ret != 0:
print('获取图像信息失败')
image_data_size = (c_ubyte * camera_int_param.nCurValue)()
image_data_length = camera_int_param.nCurValue
# 开始抓流
grab_ret = cam.MV_CC_StartGrabbing()
if grab_ret != 0:
print('抓起流失败')
# 获取数据大小
# 下面将获得每一帧的图像
while True:
frame_info = MV_FRAME_OUT_INFO_EX()
memset(byref(frame_info), 0, sizeof(frame_info))
frame_ret = cam.MV_CC_GetOneFrameTimeout(image_data_size, image_data_length, frame_info, 1000)
if frame_ret != 0:
print('获取帧信息失败')
nRGBSize = frame_info.nWidth * frame_info.nHeight
convert_param = MV_CC_PIXEL_CONVERT_PARAM()
memset(byref(convert_param), 0, sizeof(convert_param))
convert_param.nWidth = frame_info.nWidth
convert_param.nHeight = frame_info.nHeight
convert_param.pSrcData = image_data_size
convert_param.nSrcDataLen = frame_info.nFrameLen
convert_param.enSrcPixelType = frame_info.enPixelType
convert_param.enDstPixelType = frame_info.enPixelType
convert_param.pDstBuffer = (c_ubyte * nRGBSize)()
convert_param.nDstBufferSize = nRGBSize
ret = cam.MV_CC_ConvertPixelType(convert_param)
if ret != 0:
print("convert pixel fail! ret[0x%x]" % ret)
del image_data_size
sys.exit()
img_buff = (c_ubyte * convert_param.nDstLen)()
cdll.msvcrt.memcpy(byref(img_buff), convert_param.pDstBuffer, convert_param.nDstLen)
img = np.array(img_buff, dtype=np.int8)
Img = img.reshape(1024, 1280)
Img = cv.flip(Img, -1)
Img = cv.flip(Img, -1)
cv.imshow('img' + str(show_device_num), Img)
cv.waitKey(25)
if __name__ == '__main__':
a = AccessToImages()
device_list = a.FindDevices()
if a:
a.GetImages(0, device_list)
# print(device_list.nDeviceNum)