|
一、下载MVS客户端海康机器人-机器视觉-下载中心二、解压并安装MVS客户端三、找到MVS示例代码(代码在MVS的安装位置)工业相机只允许单条连接,也就是说MVS如果连接了相机,python代码就无法获取数据,此时必须退出MVS客户端。四、使用Pycharm打开Python代码示例文件夹如果觉得爆红难受,将MvImport文件夹中的五个文件,拷贝到BasicDemo文件夹下即可。博主这里出现了一个警告:获取不到相机参数。忽略掉即可。五、获取相机图像安装一下opencvpip3installopencv-python-ihttps://pypi.tuna.tsinghua.edu.cn/simple相机工具类(读者直接复制)HKCamera_class.pyimportsysfromctypesimport*importnumpyasnpimportcv2#这里要根据个人情况进行修改TODOsys.path.append("E:\MVS_STD_4.2.1_230921\MVS\Development\Samples\Python\MvImport")#打开MVS中的MvImport文件,对于不同系统打开的文件路径跟随实际文件路径变化即可fromMvCameraControl_classimport*#调用了MvCameraControl_class.py文件classHKCamera():def__init__(self,CameraIdx=0,log_path=None,CameraIp=""):self.name="HKCamera"deviceList=self.enum_devices()strModeName=""TargetCamera=Noneforiinrange(0,deviceList.nDeviceNum):mvcc_dev_info=cast(deviceList.pDeviceInfo[i],POINTER(MV_CC_DEVICE_INFO)).contentsnip1=((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)Devip="%d.%d.%d.%d"%(nip1,nip2,nip3,nip4)ifDevip==CameraIp:TargetCamera=iself.camera=self.open_camera(deviceList=deviceList,CameraIdx=TargetCamera,log_path=log_path)#self.start_camera()def__del__(self):ifself.cameraisNone:return#停止取流#ret=self.camera.MV_CC_StopGrabbing()#ifret!=0:#raiseException("stopgrabbingfail!ret[0x%x]"%ret)#关闭设备ret=self.camera.MV_CC_CloseDevice()ifret!=0:raiseException("closedeivcefail!ret[0x%x]"%ret)#销毁句柄ret=self.camera.MV_CC_DestroyHandle()ifret!=0:raiseException("destroyhandlefail!ret[0x%x]"%ret)@staticmethoddefenum_devices(device=0,device_way=False):"""device=0枚举网口、USB口、未知设备、cameralink设备device=1枚举GenTL设备"""ifdevice_way==False:ifdevice==0:cameraType=MV_GIGE_DEVICE|MV_USB_DEVICE|MV_UNKNOW_DEVICE|MV_1394_DEVICE|MV_CAMERALINK_DEVICEdeviceList=MV_CC_DEVICE_INFO_LIST()#枚举设备ret=MvCamera.MV_CC_EnumDevices(cameraType,deviceList)ifret!=0:raiseException("enumdevicesfail!ret[0x%x]"%ret)returndeviceListelse:passelifdevice_way==True:passdefopen_camera(self,deviceList,CameraIdx,log_path):#generateacamerainstancecamera=MvCamera()#选择设备并创建句柄stDeviceList=cast(deviceList.pDeviceInfo[CameraIdx],POINTER(MV_CC_DEVICE_INFO)).contentsiflog_pathisnotNone:ret=self.camera.MV_CC_SetSDKLogPath(log_path)ifret!=0:raiseException("setLogpathfail!ret[0x%x]"%ret)#创建句柄,生成日志ret=camera.MV_CC_CreateHandle(stDeviceList)ifret!=0:raiseException("createhandlefail!ret[0x%x]"%ret)else:#创建句柄,不生成日志ret=camera.MV_CC_CreateHandleWithoutLog(stDeviceList)ifret!=0:raiseException("createhandlefail!ret[0x%x]"%ret)#打开相机ret=camera.MV_CC_OpenDevice(MV_ACCESS_Exclusive,0)ifret!=0:raiseException("opendevicefail!ret[0x%x]"%ret)returncameradefstart_camera(self):stParam=MVCC_INTVALUE()memset(byref(stParam),0,sizeof(MVCC_INTVALUE))ret=self.camera.MV_CC_GetIntValue("PayloadSize",stParam)ifret!=0:raiseException("getpayloadsizefail!ret[0x%x]"%ret)self.nDataSize=stParam.nCurValueself.pData=(c_ubyte*self.nDataSize)()self.stFrameInfo=MV_FRAME_OUT_INFO_EX()memset(byref(self.stFrameInfo),0,sizeof(self.stFrameInfo))self.camera.MV_CC_StartGrabbing()defget_Value(self,param_type,node_name):""":paramcam:相机实例:param_type:获取节点值得类型:paramnode_name:节点名可选int、float、enum、bool、string型节点:return:节点值"""ifparam_type=="int_value":stParam=MVCC_INTVALUE_EX()memset(byref(stParam),0,sizeof(MVCC_INTVALUE_EX))ret=self.camera.MV_CC_GetIntValueEx(node_name,stParam)ifret!=0:raiseException("获取int型数据%s失败!报错码ret[0x%x]"%(node_name,ret))returnstParam.nCurValueelifparam_type=="float_value":stFloatValue=MVCC_FLOATVALUE()memset(byref(stFloatValue),0,sizeof(MVCC_FLOATVALUE))ret=self.camera.MV_CC_GetFloatValue(node_name,stFloatValue)ifret!=0:raiseException("获取float型数据%s失败!报错码ret[0x%x]"%(node_name,ret))returnstFloatValue.fCurValueelifparam_type=="enum_value":stEnumValue=MVCC_ENUMVALUE()memset(byref(stEnumValue),0,sizeof(MVCC_ENUMVALUE))ret=self.camera.MV_CC_GetEnumValue(node_name,stEnumValue)ifret!=0:raiseException("获取enum型数据%s失败!报错码ret[0x%x]"%(node_name,ret))returnstEnumValue.nCurValueelifparam_type=="bool_value":stBool=c_bool(False)ret=self.camera.MV_CC_GetBoolValue(node_name,stBool)ifret!=0:raiseException("获取bool型数据%s失败!报错码ret[0x%x]"%(node_name,ret))returnstBool.valueelifparam_type=="string_value":stStringValue=MVCC_STRINGVALUE()memset(byref(stStringValue),0,sizeof(MVCC_STRINGVALUE))ret=self.camera.MV_CC_GetStringValue(node_name,stStringValue)ifret!=0:raiseException("获取string型数据%s失败!报错码ret[0x%x]"%(node_name,ret))returnstStringValue.chCurValueelse:returnNonedefset_Value(self,param_type,node_name,node_value):""":paramcam:相机实例:paramparam_type:需要设置的节点值得类型int:float:enum:参考于客户端中该选项的EnumEntryValue值即可bool:对应0为关,1为开string:输入值为数字或者英文字符,不能为汉字:paramnode_name:需要设置的节点名:paramnode_value:设置给节点的值:return:"""ifparam_type=="int_value":ret=self.camera.MV_CC_SetIntValueEx(node_name,int(node_value))ifret!=0:raiseException("设置int型数据节点%s失败!报错码ret[0x%x]"%(node_name,ret))elifparam_type=="float_value":ret=self.camera.MV_CC_SetFloatValue(node_name,float(node_value))ifret!=0:raiseException("设置float型数据节点%s失败!报错码ret[0x%x]"%(node_name,ret))elifparam_type=="enum_value":ret=self.camera.MV_CC_SetEnumValueByString(node_name,node_value)ifret!=0:raiseException("设置enum型数据节点%s失败!报错码ret[0x%x]"%(node_name,ret))elifparam_type=="bool_value":ret=self.camera.MV_CC_SetBoolValue(node_name,node_value)ifret!=0:raiseException("设置bool型数据节点%s失败!报错码ret[0x%x]"%(node_name,ret))elifparam_type=="string_value":ret=self.camera.MV_CC_SetStringValue(node_name,str(node_value))ifret!=0:raiseException("设置string型数据节点%s失败!报错码ret[0x%x]"%(node_name,ret))defset_exposure_time(self,exp_time):self.set_Value(param_type="float_value",node_name="ExposureTime",node_value=exp_time)defget_exposure_time(self):returnself.get_Value(param_type="float_value",node_name="ExposureTime")defget_image(self,width=None):""":paramcam:相机实例:active_way:主动取流方式的不同方法分别是(getImagebuffer)(getoneframetimeout):return:"""#ret=self.camera.MV_CC_SaveImageEx2()ret=self.camera.MV_CC_GetOneFrameTimeout(self.pData,self.nDataSize,self.stFrameInfo,5000)ifret==0:image=np.asarray(self.pData).reshape((self.stFrameInfo.nHeight,self.stFrameInfo.nWidth,3))#ifwidthisnotNone:#image=cv2.resize(image,(width,int(self.stFrameInfo.nHeight*width/self.stFrameInfo.nWidth)))#passimage=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)returnimageelse:returnNonedefshow_runtime_info(self,image):exp_time=self.get_exposure_time()cv2.putText(image,("exposuretime=%1.1fms"%(exp_time*0.001)),(20,50),cv2.FONT_HERSHEY_SIMPLEX,0.5,255,1)获取照片数据Main.pyimportosimportcv2fromnumpyimportndarrayfromHKCamera_classimportHKCameraif__name__=='__main__':camera=HKCamera(CameraIp='192.168.1.191')#对摄像头配置进行设置try:camera.set_Value(param_type="enum_value",node_name="PixelFormat",node_value='RGB8Packed')camera.set_Value(param_type="enum_value",node_name="ExposureAuto",node_value='Off')camera.set_Value(param_type="float_value",node_name="ExposureTime",node_value='400')exceptExceptionase:print(e)os._exit(0)camera.start_camera()whileTrue:try:image:ndarray=camera.get_image()ifimageisnotNone:#这里拿到图片的ndarray数据后,可以转Image、可以保存图片....等等操作print(image.shape)key=cv2.waitKey(50)&0xFFifkey==ord('e')orkey==ord('E'):cv2.destroyAllWindows()breakexceptExceptionase:print(e)
|
|