海康工业相机Python调用实现拍照
条件:Python+海康官方的mvs文件下的development/samples下的python文件夹
注意:相机连接后不要用官方app打开相机,不然python代码检测不到设备,代码在pycharm会提示报错,亲测能跑并能截取到图片
# -- coding: utf-8 --import sys
import copy
import msvcrtfrom ctypes import *
import timesys.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 deviceret = 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)).contentsif 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:breakstrModeName = strModeName + chr(per)print ("device model name: %s" % strModeName)strSerialNumber = ""for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber:if per == 0:breakstrSerialNumber = 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 Objectcam = MvCamera()# ch:选择设备并创建句柄 | en:Select device and create handlestDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contentsret = cam.MV_CC_CreateHandle(stDeviceList)if ret != 0:print ("create handle fail! ret[0x%x]" % ret)sys.exit()# ch:打开设备 | en:Open deviceret = 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 offret = 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 sizestParam = 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 imagestart_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.nWidthstConvertParam.nHeight = stDeviceList.nHeightstConvertParam.pData = data_bufstConvertParam.nDataLen = stDeviceList.nFrameLenstConvertParam.enPixelType = stDeviceList.enPixelType# MV_Image_Undefined = 0, //未定义# MV_Image_Bmp = 1, //BMP图片# MV_Image_Jpeg = 2, //JPEG图片# MV_Image_Png = 3, //PNG图片,暂不支持# MV_Image_Tif = 4, //TIF图片,暂不支持# jpg参数# stConvertParam.nJpgQuality = 99 # 压缩质量选择范围[50-99]# file_path = "save.jpg"# stConvertParam.enImageType = MV_Image_Jpeg# bmpsize = nPayloadSizefile_path = "save.bmp"stConvertParam.enImageType = MV_Image_Bmpbmpsize = stDeviceList.nWidth * stDeviceList.nHeight * 3 + 54stConvertParam.nBufferSize = bmpsizebmp_buf = (c_ubyte * bmpsize)()stConvertParam.pImageBuffer = bmp_bufret = cam.MV_CC_SaveImageEx2(stConvertParam)if ret != 0:print ("save file executed failed0:! ret[0x%x]" % ret)del data_bufsys.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 imageret = cam.MV_CC_StopGrabbing()if ret != 0:print ("stop grabbing fail! ret[0x%x]" % ret)del data_bufsys.exit()# ch:关闭设备 | Close deviceret = cam.MV_CC_CloseDevice()if ret != 0:print ("close deivce fail! ret[0x%x]" % ret)del data_bufsys.exit()# ch:销毁句柄 | Destroy handleret = cam.MV_CC_DestroyHandle()if ret != 0:print ("destroy handle fail! ret[0x%x]" % ret)del data_bufsys.exit()del data_buf
海康工业相机Python调用实现拍照相关推荐
- 海康工业相机Python调用实现连续实时拍照
海康工业相机Python调用实现连续实时拍照 文章目录 海康工业相机Python调用实现连续实时拍照 前言: 参考链接: 提前设置: 图片数据流的获取: 代码: 前言: 海康的相机调用简直太复杂了.当 ...
- 海康工业相机 python 图像数据转opencv格式
python调用海康工业相机,官方给出的SDK里没有转到opencv格式的例子,网上找了下,只有一篇,转换部分的代码如下: temp = np.asarray(pData2) # 将c_ubyte_A ...
- python调用海康工业相机并用opencv显示(整体实现)
python语言调用海康机器人(hikrobotics)工业相机 系列文章目录 第一章 python调用海康工业相机并用opencv显示(整体实现) 第二章 python 调用海康工业相机图像获取方式 ...
- Opencv如何打开使用海康工业相机
文章转载:python语言下使用opencv接口cv2.VideoCapture()接口调用海康机器人工业相机 一.Opencv接口 使用 opencv 的官方接口 VideoCapture() 接口 ...
- 海康工业相机的使用遇到的一些问题
2021-5.24 现在是测试软件的稳定性,我就一直打开相机,发现6个机位同时显示,在检测缺陷到随缘数量的时候界面就会卡住.我最后检查出是网卡的缘故,我现在的电脑是双网卡的,我一个网口接了交换机,交换 ...
- 海康工业摄像头调用(linux基于python和opencv)
1.下载官网客户端(其中包含SDK) 官方网站 海康机器人-机器视觉-下载中心 安装deb文件 sudo dpkg -i deb文件名 2.运行客户端 cd /opt/MVS/bin/ ./MVS. ...
- python语言下使用pyqt中的QImage对海康工业相机获取到的图像进行显示
系列文章目录 文章目录 目录 系列文章目录 文章目录 前言 二.使用海康工业相机API接口GetImagebuffer配合pyqt中的QImage类进行界面显示 1.海康工业相机API接口GetIma ...
- 海康威视工业相机海康机器人 Python开发采集数据、保存照片PyQt显示
目前缺芯的大环境也影响到工业相机上面了,使用支持国产海康机器人的工业相机进行视觉开发是不错的替代方案.价格交期很是感人呐! 参考文章python调用海康工业相机并用opencv显示(整体实现) 博主写 ...
- 2022-05-16 Python OpenCV 海康工业相机
背景 Python OpenCV连接海康工业相机做图像处理 环境 Python:3.9.9 OpenCV:4.5.5 numpy:1.19.3 2022-05-16_112138.png 相机 海康: ...
最新文章
- window下实现在线预览功能
- [POJ1741]Tree
- python3 获取当前目录和上级目录
- 用python画月亮的代码-用python画月亮的代码是什么?
- python 用while输出数字金字塔_用Python实现一个Dual Thrust数字货币量化交易策略
- 导出Oracle数据库字典
- JS-内置对象内置构造函数事件-拖拽轮播图无缝滚动
- TensorFlow笔记(8) LeNet-5卷积神经网络
- 在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味
- div 和table
- hadoop工作原理
- MCDC — 修正判定条件覆盖
- 《正念领导力》承诺14:创造共赢
- linux systemd.service说明
- 实现JSON在线美化(格式化)、JSON转CSV、CSV转XML工具-toolfk程序员工具网
- 2021中国市场十大IT热点
- Youtube Data API使用简介
- python自然语言分析_Python自然语言用金庸的武侠小说做分析和处理
- Java插件自动保存浏览器书签_EverSync插件,浏览器书签同步插件,支持Chrome和Firefox书签同步...
- python用glob遍历文件_python中用glob快速获取文件列表