# -- coding: utf-8 --
import sys
import copy
import msvcrt
from ctypes import *
import time
sys.path.append("../MvImport")
from MvCameraControl_class import *
if __name__ == "__main__":
deviceList = MV_CC_DEVICE_INFO_LIST()
tlayerType = MV_GIGE_DEVICE | MV_USB_DEVICE
# ch:枚举设备 | en:Enum device
ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
if ret != 0:
print ("enum devices fail! ret[0x%x]" % ret)
sys.exit()
if deviceList.nDeviceNum == 0:
print ("find no device!")
sys.exit()
print ("find %d devices!" % deviceList.nDeviceNum)
for i in range(0, deviceList.nDeviceNum):
mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE:
print ("\ngige device: [%d]" % i)
strModeName = ""
for per in mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName:
strModeName = strModeName + chr(per)
print ("device model name: %s" % strModeName)
nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
print ("current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
print ("\nu3v device: [%d]" % i)
strModeName = ""
for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName:
if per == 0:
break
strModeName = strModeName + chr(per)
print ("device model name: %s" % strModeName)
strSerialNumber = ""
for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber:
if per == 0:
break
strSerialNumber = strSerialNumber + chr(per)
print ("user serial number: %s" % strSerialNumber)
nConnectionNum = input("please input the number of the device to connect:")
if int(nConnectionNum) >= deviceList.nDeviceNum:
print ("intput error!")
sys.exit()
# ch:创建相机实例 | en:Creat Camera Object
cam = MvCamera()
# ch:选择设备并创建句柄 | en:Select device and create handle
stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
ret = cam.MV_CC_CreateHandle(stDeviceList)
if ret != 0:
print ("create handle fail! ret[0x%x]" % ret)
sys.exit()
# ch:打开设备 | en:Open device
ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
if ret != 0:
print ("open device fail! ret[0x%x]" % ret)
sys.exit()
# ch:探测网络最f佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if stDeviceList.nTLayerType == MV_GIGE_DEVICE:
nPacketSize = cam.MV_CC_GetOptimalPacketSize()
if int(nPacketSize) > 0:
ret = cam.MV_CC_SetIntValue("GevSCPSPacketSize",nPacketSize)
if ret != 0:
print ("Warning: Set Packet Size fail! ret[0x%x]" % ret)
else:
print ("Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
# ch:设置触发模式为off | en:Set trigger mode as off
ret = cam.MV_CC_SetEnumValue("TriggerMode", MV_TRIGGER_MODE_OFF)
if ret != 0:
print ("set trigger mode fail! ret[0x%x]" % ret)
sys.exit()
# ch:获取数据包大小 | en:Get payload size
stParam = MVCC_INTVALUE()
memset(byref(stParam), 0, sizeof(MVCC_INTVALUE))
ret = cam.MV_CC_GetIntValue("PayloadSize", stParam)
if ret != 0:
print ("get payload size fail! ret[0x%x]" % ret)
sys.exit()
nPayloadSize = stParam.nCurValue
# ch:开始取流 | en:Start grab image
start_time = time.time()
ret = cam.MV_CC_StartGrabbing()
if ret != 0:
print ("start grabbing fail! ret[0x%x]" % ret)
sys.exit()
stDeviceList = MV_FRAME_OUT_INFO_EX()
memset(byref(stDeviceList), 0, sizeof(stDeviceList))
data_buf = (c_ubyte * nPayloadSize)()
ret = cam.MV_CC_GetOneFrameTimeout(byref(data_buf), nPayloadSize, stDeviceList, 1000)
if ret == 0:
# Stop = time()
# print(Stop - start)
print ("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (stDeviceList.nWidth, stDeviceList.nHeight, stDeviceList.nFrameNum))
stConvertParam = MV_SAVE_IMAGE_PARAM_EX()
stConvertParam.nWidth = stDeviceList.nWidth
stConvertParam.nHeight = stDeviceList.nHeight
stConvertParam.pData = data_buf
stConvertParam.nDataLen = stDeviceList.nFrameLen
stConvertParam.enPixelType = stDeviceList.enPixelType
# MV_Image_Undefined = 0,
# MV_Image_Bmp = 1,
# MV_Image_Jpeg = 2,
# MV_Image_Png = 3,
# MV_Image_Tif = 4,
# jpg参数
# stConvertParam.nJpgQuality = 99 # 压缩质量选择范围[50-99]
# file_path = "save.jpg"
# stConvertParam.enImageType = MV_Image_Jpeg
# bmpsize = nPayloadSize
file_path = "save.bmp"
stConvertParam.enImageType = MV_Image_Bmp
bmpsize = stDeviceList.nWidth * stDeviceList.nHeight * 3 + 54
stConvertParam.nBufferSize = bmpsize
bmp_buf = (c_ubyte * bmpsize)()
stConvertParam.pImageBuffer = bmp_buf
ret = cam.MV_CC_SaveImageEx2(stConvertParam)
if ret != 0:
print ("save file executed failed0:! ret[0x%x]" % ret)
del data_buf
sys.exit()
end_time = time.time()
print (start_time - end_time)
# print(stop - start)
file_open = open(file_path.encode('ascii'), 'wb+')
try:
img_buff = (c_ubyte * stConvertParam.nImageLen)()
cdll.msvcrt.memcpy(byref(img_buff), stConvertParam.pImageBuffer, stConvertParam.nImageLen)
file_open.write(img_buff, )
print (img_buff)
except:
raise Exception("save file executed failed1::%s" % e.message)
finally:
file_open.close()
else:
print ("get one frame fail, ret[0x%x]" % ret)
# ch:停止取流 | en:Stop grab image
ret = cam.MV_CC_StopGrabbing()
if ret != 0:
print ("stop grabbing fail! ret[0x%x]" % ret)
del data_buf
sys.exit()
# ch:关闭设备 | Close device
ret = cam.MV_CC_CloseDevice()
if ret != 0:
print ("close deivce fail! ret[0x%x]" % ret)
del data_buf
sys.exit()
# ch:销毁句柄 | Destroy handle
ret = cam.MV_CC_DestroyHandle()
if ret != 0:
print ("destroy handle fail! ret[0x%x]" % ret)
del data_buf
sys.exit()
del data_buf
条件:Python+海康官方的mvs文件下的development/samples下的python文件夹注意:相机连接后不要用官方app打开相机,不然python代码检测不到设备,代码在pycharm会提示报错,亲测能跑并能截取到图片# -- coding: utf-8 --import sysimport copyimport msvcrtfrom ctypes import *...
linux版本环境比较麻烦,要注意自己的硬件系统是x86还是ARM等等,
我个人是ubuntu18
https://www.hikrobotics.com/cn/machinevision/service/download?module=0在海康官方下载其linux版本的MVS,下载完成后有很多版本的,根据自己电脑是x86或者arm来选择解压,同时在其中文文档中根据要求需要配置好环境变量!!!
文件夹中的海康文档给出了命令行
可以在终端中输入export查看环境变量是否导入了路径
第一次添加环境变量后需要重启(这点可能因人而异)
压缩包中有两个文件夹
一个是直接运行的脚本
一个是封装成类
根据需要自取,运行hik2cv.py即可
文章目录海康工业相机Python调用实现连续实时拍照前言:参考链接:提前设置:代码:
海康的相机调用简直太复杂了。当然相比point gray fly的相机无法python调用要好很多。
为了将照片调用打包成类,我竟然花了两天时间?
这里做一个简单的记录。希望能给大伙儿一个分享。
参考链接:
海康工业相机Python调用实现拍照
提前设置:
条件:Python+海康官方的mvs文件下的development/samples下的python文件夹
易出现的问题
在使用python多线程对图像进行处理时,可能会出现要显示的图像一闪而逝的问题,而又没有任何报错。这应该是线程阻塞导致的。也许是你在循环指令下面建立了线程,从而会导致开启太多的线程。别说加锁了,似乎就算不加锁也没法解决这个问题,我感觉可能是太多线程同时竞争资源导致的死锁。
不要在循环内建立线程,把循环放在整个线程里面。也许你最初想要显示图像并处理图像,那么他们本该在同一个循环下。如果按照我说的那样,那把代码拆成两个线程的话,似乎就变成两个循环了。你可能认为这
目前缺芯的大环境也影响到工业相机上面了,使用支持国产海康机器人的工业相机进行视觉开发是不错的替代方案。价格交期很是感人呐!
参考文章python调用海康工业相机并用opencv显示(整体实现)
博主写的很全面非常好。
PyQt显示相机图像本文推荐另一种方法“将numpy array 先转化为 Pillow image 再转化为Pixmap” 并用来显示。
主要步骤:
(1)HikRobot 官网下载安装“机器视觉工业相机客户端MVS V3.3.1(Windows)”, 下载链接。
(2)安装后参考官网自.
# 绘制矩形框
x, y, w, h = 100, 100, 200, 200
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 提取 ROI
roi = img[y:y+h, x:x+w]
# 显示结果
cv2.imshow('image', img)
cv2.imshow('roi', roi)
cv2.waitKey(0)
cv2.destroyAllWindows()